Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Scaffold EF Core models using Handlebars templates.

You can register Handlebars helpers in the `ScaffoldingDesignTimeServices` where setup takes place.
- Create a named tuple as shown with `myHelper` below.
- Pass the tuple to the `AddHandlebarsScaffolding` extension method.
- Pass the tuple to the `AddHandlebarsHelpers` extension method.
- You may register as many helpers as you wish.

```csharp
Expand All @@ -86,7 +86,10 @@ public class ScaffoldingDesignTimeServices : IDesignTimeServices
var myHelper = (helperName: "my-helper", helperFunction: (Action<TextWriter, object, object[]>) MyHbsHelper);

// Add Handlebars scaffolding templates
services.AddHandlebarsScaffolding(options, myHelper);
services.AddHandlebarsScaffolding(options);

// Add Handlebars helpers
services.AddHandlebarsHelpers(myHelper);
}

// Sample Handlebars helper
Expand Down
5 changes: 4 additions & 1 deletion sample/ScaffoldingSample/Sample.ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ public class ScaffoldingDesignTimeServices : IDesignTimeServices
var myHelper = (helperName: "my-helper", helperFunction: (Action<TextWriter, object, object[]>) MyHbsHelper);

// Add Handlebars scaffolding templates
services.AddHandlebarsScaffolding(options, myHelper);
services.AddHandlebarsScaffolding(options);

// Add Handlebars helpers
services.AddHandlebarsHelpers(myHelper);
}

// Sample Handlebars helper
Expand Down
9 changes: 6 additions & 3 deletions sample/ScaffoldingSample/ScaffoldingDesignTimeServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
You may customize generated classes by modifying the Handlebars templates in the CodeTemplates folder.
Then run a 'dotnet ef dbcontext scaffold' command to reverse engineer classes from an existing database.
For example:
dotnet ef dbcontext scaffold "Data Source=(localdb)\MSSQLLocalDB; Initial Catalog=NorthwindSlim; Integrated Security=True" Microsoft.EntityFrameworkCore.SqlServer -o Models -c NorthwindSlimContext -f
dotnet ef dbcontext scaffold "Data Source=(localdb)\MSSQLLocalDB; Initial Catalog=NorthwindSlim; Integrated Security=True" Microsoft.EntityFrameworkCore.SqlServer -o Models -c NorthwindSlimContext -f --context-dir Contexts
*/

namespace ScaffoldingSample
{
public class ScaffoldingDesignTimeServices : IDesignTimeServices
{
public void ConfigureDesignTimeServices(IServiceCollection services)
public void ConfigureDesignTimeServices(IServiceCollection services)
{
// Generate both context and entities
var options = ReverseEngineerOptions.DbContextAndEntities;
Expand All @@ -23,7 +23,10 @@ public void ConfigureDesignTimeServices(IServiceCollection services)
var myHelper = (helperName: "my-helper", helperFunction: (Action<TextWriter, object, object[]>) MyHbsHelper);

// Add Handlebars scaffolding templates
services.AddHandlebarsScaffolding(options, myHelper);
services.AddHandlebarsScaffolding(options);

// Add Handlebars helpers
services.AddHandlebarsHelpers(myHelper);
}

// Sample Handlebars helper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@ public static class ServiceCollectionExtensions
/// </summary>
/// <param name="services"> The <see cref="IServiceCollection" /> to add services to. </param>
/// <param name="options">Options for reverse engineering classes from an existing database.</param>
/// <param name="handlebarsHelpers">Handlebars helpers.</param>
/// <returns>The same service collection so that multiple calls can be chained.</returns>
public static IServiceCollection AddHandlebarsScaffolding(this IServiceCollection services,
ReverseEngineerOptions options = ReverseEngineerOptions.DbContextAndEntities,
params (string helperName, Action<TextWriter, object, object[]> helperFunction)[] handlebarsHelpers)
ReverseEngineerOptions options = ReverseEngineerOptions.DbContextAndEntities)
{
Type dbContextGeneratorImpl;
var dbContextGeneratorType = typeof(ICSharpDbContextGenerator);
Expand All @@ -59,15 +57,7 @@ public static IServiceCollection AddHandlebarsScaffolding(this IServiceCollectio
services.AddSingleton<IEntityTypeTemplateService, HbsEntityTypeTemplateService>();
services.AddSingleton<IModelCodeGenerator, HbsCSharpModelGenerator>();
services.AddSingleton<IReverseEngineerScaffolder, HbsReverseEngineerScaffolder>();
services.AddSingleton<IHbsHelperService>(provider =>
{
var helpers = new Dictionary<string, Action<TextWriter, object, object[]>>
{
{Constants.SpacesHelper, HandlebarsHelpers.SpacesHelper}
};
handlebarsHelpers.ToList().ForEach(h => helpers.Add(h.helperName, h.helperFunction));
return new HbsHelperService(helpers);
});
services.AddSingleton<IHbsHelperService, HbsHelperService>();
return services;
}

Expand All @@ -80,12 +70,18 @@ public static IServiceCollection AddHandlebarsScaffolding(this IServiceCollectio
/// <param name="services"> The <see cref="IServiceCollection" /> to add services to. </param>
/// <param name="handlebarsHelpers">Handlebars helpers.</param>
/// <returns>The same service collection so that multiple calls can be chained.</returns>
[Obsolete("AddHandlebarsHelpers has been deprecated. Pass helpers to AddHandlebarsScaffolding instead.")]
public static IServiceCollection AddHandlebarsHelpers(this IServiceCollection services,
params (string helperName, Action<TextWriter, object, object[]> helperFunction)[] handlebarsHelpers)
{
var helperService = services.BuildServiceProvider().GetRequiredService<IHbsHelperService>();
handlebarsHelpers.ToList().ForEach(h => helperService.Helpers.Add(h.helperName, h.helperFunction));
services.AddSingleton<IHbsHelperService>(provider =>
{
var helpers = new Dictionary<string, Action<TextWriter, object, object[]>>
{
{Constants.SpacesHelper, HandlebarsHelpers.SpacesHelper}
};
handlebarsHelpers.ToList().ForEach(h => helpers.Add(h.helperName, h.helperFunction));
return new HbsHelperService(helpers);
});
return services;
}
}
Expand Down