Skip to content

Commit b8868da

Browse files
author
Anthony Sneed
committed
Register IHbsHelperService in AddHandlebarsScaffolding.
1 parent 24c3deb commit b8868da

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/EntityFrameworkCore.Scaffolding.Handlebars/ServiceCollectionExtensions.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,27 +57,31 @@ public static IServiceCollection AddHandlebarsScaffolding(this IServiceCollectio
5757
services.AddSingleton<IEntityTypeTemplateService, HbsEntityTypeTemplateService>();
5858
services.AddSingleton<IModelCodeGenerator, HbsCSharpModelGenerator>();
5959
services.AddSingleton<IReverseEngineerScaffolder, HbsReverseEngineerScaffolder>();
60+
services.AddSingleton<IHbsHelperService>(provider =>
61+
{
62+
var helpers = new Dictionary<string, Action<TextWriter, object, object[]>>
63+
{
64+
{Constants.SpacesHelper, HandlebarsHelpers.SpacesHelper}
65+
};
66+
return new HbsHelperService(helpers);
67+
});
6068
return services;
6169
}
6270

6371
/// <summary>
6472
/// Register Handlebars helpers.
73+
/// <para>
74+
/// Note: You must first call AddHandlebarsScaffolding before calling AddHandlebarsHelpers.
75+
/// </para>
6576
/// </summary>
6677
/// <param name="services"> The <see cref="IServiceCollection" /> to add services to. </param>
6778
/// <param name="handlebarsHelpers">Handlebars helpers.</param>
6879
/// <returns>The same service collection so that multiple calls can be chained.</returns>
6980
public static IServiceCollection AddHandlebarsHelpers(this IServiceCollection services,
7081
params (string helperName, Action<TextWriter, object, object[]> helperFunction)[] handlebarsHelpers)
7182
{
72-
services.AddSingleton<IHbsHelperService>(provider =>
73-
{
74-
var helpers = new Dictionary<string, Action<TextWriter, object, object[]>>
75-
{
76-
{Constants.SpacesHelper, HandlebarsHelpers.SpacesHelper}
77-
};
78-
handlebarsHelpers.ToList().ForEach(h => helpers.Add(h.helperName, h.helperFunction));
79-
return new HbsHelperService(helpers);
80-
});
83+
var helperService = services.BuildServiceProvider().GetRequiredService<IHbsHelperService>();
84+
handlebarsHelpers.ToList().ForEach(h => helperService.Helpers.Add(h.helperName, h.helperFunction));
8185
return services;
8286
}
8387
}

0 commit comments

Comments
 (0)