New Feature: Load & Register Custom IHandlebars Transformers from any assembly (dll) #200

Closed
opened 2025-12-29 14:24:57 +01:00 by adam · 12 comments
Owner

Originally created by @muzammilkm on GitHub (Aug 16, 2019).

WireMock StandAlone could accept dll paths to load & register custom IHandlebars Transformer from them. So that response template transformed output can easily tested out in browser without a lots for debugging.

Originally created by @muzammilkm on GitHub (Aug 16, 2019). WireMock StandAlone could accept dll paths to load & register custom IHandlebars Transformer from them. So that response template transformed output can easily tested out in browser without a lots for debugging.
adam added the question label 2025-12-29 14:24:57 +01:00
adam closed this issue 2025-12-29 14:24:57 +01:00
Author
Owner

@StefH commented on GitHub (Aug 16, 2019):

Why would you need IHandlebars transformers? The whole idea from Handlebars is that you can define the transformation in plain text.

@StefH commented on GitHub (Aug 16, 2019): Why would you need IHandlebars transformers? The whole idea from Handlebars is that you can define the transformation in plain text.
Author
Owner

@muzammilkm commented on GitHub (Aug 17, 2019):

Ahh.. sorry for confusion.

What i meant was, right now we have Register Transformation Helper via Settings & can only be used while running unit tests or Create a copy of "Standalone" & then copy these helpers. Working with multiple project would be difficult.

handlebarsContext.RegisterHelper("Some-Name", (writer, context, parameters) =>
{
     writer.WriteSafeString(SomeLogic(parameters));
});

If you could provide an interface that we can implement once & provide away for these libraries to be loaded in "Standalone", so that i would be easy for the QA's or anyone to run standalone and test json mapping.

@muzammilkm commented on GitHub (Aug 17, 2019): Ahh.. sorry for confusion. What i meant was, right now we have Register Transformation Helper via Settings & can only be used while running unit tests or Create a copy of "Standalone" & then copy these helpers. Working with multiple project would be difficult. ``` c# handlebarsContext.RegisterHelper("Some-Name", (writer, context, parameters) => { writer.WriteSafeString(SomeLogic(parameters)); }); ``` If you could provide an interface that we can implement once & provide away for these libraries to be loaded in "Standalone", so that i would be easy for the QA's or anyone to run standalone and test json mapping.
Author
Owner

@StefH commented on GitHub (Aug 17, 2019):

The StandAloneApp is just a simple library which converts/parses commandline arguments into settings, and then just starts the Mock Server.

You always need a console app (.net framework or .net core) to really start and run the server.

I think I makes more sense to create a WireMock.CommandLineParser NuGet which just converts arguments into a settings file.

And then you can just add the handlebars to that settings and then start using
public static FluentMockServer Start([NotNull] IFluentMockServerSettings settings)

Would that be an option?

@StefH commented on GitHub (Aug 17, 2019): The `StandAloneApp` is just a simple library which converts/parses commandline arguments into settings, and then just starts the Mock Server. You always need a console app (.net framework or .net core) to really start and run the server. I think I makes more sense to create a WireMock.CommandLineParser NuGet which just converts arguments into a settings file. And then you can just add the handlebars to that settings and then start using `public static FluentMockServer Start([NotNull] IFluentMockServerSettings settings)` Would that be an option?
Author
Owner

@muzammilkm commented on GitHub (Aug 24, 2019):

Yes, i have built CommandLine dotnet tool which read arguments like mapping folder and port. This tool can be installed from nuget & run via commad line. I made this tool generic so, that any team can install start writing json mapping files for their own testing.

I work well, but few teams will have there own Custom Handlebars part of the json mapping files which they want to see how it is rendering. That's where i was thinking of

If there was an interface within wiremock.net which contains method "register custom handlebars" and pass list of object of above type interface. This way i can take one more arguments for "xxx.dll" either with cmdline load & create instances of interface or passon the dll to WireMock.Net to load & instantiate.

Hope this add some clarity ???

@muzammilkm commented on GitHub (Aug 24, 2019): Yes, i have built CommandLine dotnet tool which read arguments like mapping folder and port. This tool can be installed from nuget & run via commad line. I made this tool generic so, that any team can install start writing json mapping files for their own testing. I work well, but few teams will have there own Custom Handlebars part of the json mapping files which they want to see how it is rendering. That's where i was thinking of If there was an interface within wiremock.net which contains method "register custom handlebars" and pass list of object of above type interface. This way i can take one more arguments for "xxx.dll" either with cmdline load & create instances of interface or passon the dll to WireMock.Net to load & instantiate. Hope this add some clarity ???
Author
Owner

@StefH commented on GitHub (Aug 24, 2019):

There is a HandlebarsRegistrationCallback method defined in the settings interface: https://github.com/WireMock-Net/WireMock.Net/blob/master/src/WireMock.Net/Settings/IFluentMockServerSettings.cs#L121

/// <summary>
/// Action which can be used to add additional is Handlebar registrations. [Optional]
/// </summary>HandlebarsRegistrationCallback 
[PublicAPI]
Action<IHandlebars, IFileSystemHandler> { get; set; }

With this callback you could load your own dll's and register the Custom Handlebars from the teams.

Would this help you?

@StefH commented on GitHub (Aug 24, 2019): There is a **HandlebarsRegistrationCallback** method defined in the settings interface: https://github.com/WireMock-Net/WireMock.Net/blob/master/src/WireMock.Net/Settings/IFluentMockServerSettings.cs#L121 ``` c# /// <summary> /// Action which can be used to add additional is Handlebar registrations. [Optional] /// </summary>HandlebarsRegistrationCallback [PublicAPI] Action<IHandlebars, IFileSystemHandler> { get; set; } ``` With this callback you could load your own dll's and register the Custom Handlebars from the teams. Would this help you?
Author
Owner

@muzammilkm commented on GitHub (Aug 24, 2019):

I guess you are asking to impl "IFluentMockServerSettings" in multiple application & load those dll in cmd,

Below what i am thinking should be & this sort of interface should be in Wiremock where any one can implement.

    interface IHandleBarTransformer
    {
        string Name { get; }

        void Render(TextWriter textWriter, dynamic context, object[] arguments);
    }

This is Impl within any ones test suit.

 public class CustomNameTransformer: IHandleBarTransformer
    {
        public string Name => "CustomName";

        public void Render(TextWriter writer, dynamic context, object[] parameters)
        {
           /* Handlebar logic to render */
        }
    }

Usage:

IHandleBarTransformer custom = new CustomNameTransformer();

var settings = new FluentMockServerSettings()
{
     HandlebarsRegistrationCallback = (a, b) =>
     {
          a.RegisterHelper(custom .Name, custom .Render);
     }
};

With this sort of setup, Now i can load given dll, Dynamically instantiate "IHandleBarTransformer" type within this assembly & pass on to settings. This way we would have one CmdLine tool any one can pass argument to control the behavior of the tool.

Hope i am making sense ?

I could also create separate package with above interface & ask everyone to implement & load. But i feel this interface should be part of WireMock.Net.

@muzammilkm commented on GitHub (Aug 24, 2019): I guess you are asking to impl "IFluentMockServerSettings" in multiple application & load those dll in cmd, Below what i am thinking should be & this sort of interface should be in Wiremock where any one can implement. ``` c# interface IHandleBarTransformer { string Name { get; } void Render(TextWriter textWriter, dynamic context, object[] arguments); } ``` This is Impl within any ones test suit. ```c# public class CustomNameTransformer: IHandleBarTransformer { public string Name => "CustomName"; public void Render(TextWriter writer, dynamic context, object[] parameters) { /* Handlebar logic to render */ } } ``` Usage: ```c# IHandleBarTransformer custom = new CustomNameTransformer(); var settings = new FluentMockServerSettings() { HandlebarsRegistrationCallback = (a, b) => { a.RegisterHelper(custom .Name, custom .Render); } }; ``` With this sort of setup, Now i can load given dll, Dynamically instantiate "IHandleBarTransformer" type within this assembly & pass on to settings. This way we would have one CmdLine tool any one can pass argument to control the behavior of the tool. Hope i am making sense ? I could also create separate package with above interface & ask everyone to implement & load. But i feel this interface should be part of WireMock.Net.
Author
Owner

@StefH commented on GitHub (Aug 25, 2019):

The HandlebarsRegistrationCallback can be used by anyone who wants to register it's own Handlebars helpers.

So in your case this should be enough for your requirements, see code sample below:

var settings = new FluentMockServerSettings()
{
     HandlebarsRegistrationCallback = (handlebarsContext, fileSystemHandler) =>
     {
          List<IHandleBarTransformer> list = ...; //  Load all dll's and find which types implement your IHandleBarTransformer interface

          foreach (Type @type in list)
          {
              var transformer = (IHandleBarTransformer) Activator.CreateInstance(@type);
              handlebarsContext.RegisterHelper(transformer.Name, transformer.Render);
          }
     }
};
@StefH commented on GitHub (Aug 25, 2019): The *HandlebarsRegistrationCallback* can be used by anyone who wants to register it's own Handlebars helpers. So in your case this should be enough for your requirements, see code sample below: ``` c# var settings = new FluentMockServerSettings() { HandlebarsRegistrationCallback = (handlebarsContext, fileSystemHandler) => { List<IHandleBarTransformer> list = ...; // Load all dll's and find which types implement your IHandleBarTransformer interface foreach (Type @type in list) { var transformer = (IHandleBarTransformer) Activator.CreateInstance(@type); handlebarsContext.RegisterHelper(transformer.Name, transformer.Render); } } }; ```
Author
Owner

@muzammilkm commented on GitHub (Aug 25, 2019):

Yes, that correct. But for that

  • i would need an IHandleBarTransformer interface first in 'WireMock.Net'.

  • Next step, I just want to property of List<IHandleBarTransformer> in FluentMockServerSettings, so that i can pass it & internally WireMock.Net can register them Instead of using HandlebarsRegistrationCallback.

  • Next step, WireMock.Net can auto load of type IHandleBarTransformer interface

@muzammilkm commented on GitHub (Aug 25, 2019): Yes, that correct. But for that - i would need an `IHandleBarTransformer` interface first in 'WireMock.Net'. - _Next step,_ I just want to property of `List<IHandleBarTransformer>` in `FluentMockServerSettings`, so that i can pass it & internally WireMock.Net can register them Instead of using `HandlebarsRegistrationCallback`. - _Next step,_ WireMock.Net can auto load of type `IHandleBarTransformer` interface
Author
Owner

@StefH commented on GitHub (Aug 25, 2019):

I still don't see why you would need the interface to be defined in WireMock.

See this examplehttps://github.com/WireMock-Net/WireMock.Net/blob/master/examples/WireMock.Net.Console.Net452.Classic/MainApp.cs#L56

Where I just did define the interface in my console app.

@StefH commented on GitHub (Aug 25, 2019): I still don't see why you would need the interface to be defined in WireMock. See this examplehttps://github.com/WireMock-Net/WireMock.Net/blob/master/examples/WireMock.Net.Console.Net452.Classic/MainApp.cs#L56 Where I just did define the interface in my console app.
Author
Owner

@muzammilkm commented on GitHub (Aug 25, 2019):

We have different teams who would be using WireMock.Net in there Test Projects and each team would have there own custom Handlebar transformer within there project.

If they want to create many mapping files & see how it renders for preview in browser, the only option they have is each team create another separate "console" project and run the console project to preview while working on json files.

I want to avoid this & create single wiremock tool for all of them & they will pass "test.project.dll" argument. With which i can load & register helper using interface. If this interface is available in WireMock.Net then everybody would impl & i could dynamically load & register.

@muzammilkm commented on GitHub (Aug 25, 2019): We have different teams who would be using WireMock.Net in there Test Projects and each team would have there own custom Handlebar transformer within there project. If they want to create many mapping files & see how it renders for preview in browser, the only option they have is each team create another separate "console" project and run the console project to preview while working on json files. I want to avoid this & create single wiremock tool for all of them & they will pass "test.project.dll" argument. With which i can load & register helper using interface. If this interface is available in WireMock.Net then everybody would impl & i could dynamically load & register.
Author
Owner

@StefH commented on GitHub (Feb 14, 2020):

Hello @muzammilkm is this still an issue for you or your team?

@StefH commented on GitHub (Feb 14, 2020): Hello @muzammilkm is this still an issue for you or your team?
Author
Owner

@muzammilkm commented on GitHub (Feb 14, 2020):

closing this.

@muzammilkm commented on GitHub (Feb 14, 2020): closing this.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#200