TrackableEntities / EntityFrameworkCore.Scaffolding.Handlebars

Scaffold EF Core models using Handlebars templates.
MIT License
208 stars 53 forks source link

[QUESTION / DISCUSSION] Multiple Templates #123

Closed Illinariss closed 4 years ago

Illinariss commented 4 years ago

Hi, two questions.

Question 1. I have 2 librarys for entities (client and server) and each needs different templates. At the moment I have to Switch the Templates for each Scaffolding. Is there a way to run Scaffolding once and:

Question 2. To get the property.IsKey() and property.IsForeignKey() value i had to extent HbsCSharpEntityTypeGenerator and HbsEntityTypeTransformationService.

 properties.Add(new Dictionary<string, object>
                {
                    { "property-type", propertyType },
                    { "property-name", property.Name },
                    { "property-annotations",  PropertyAnnotationsData },
                    { "property-comment", property.GetComment() },
                    { "property-isnullable", property.IsNullable },
                    { "nullable-reference-types", _options?.Value?.EnableNullableReferenceTypes == true },
                    { "property-iskey", property.IsKey() },
                    { "property-isforeignkey", property.IsForeignKey() }
                });
 transformedProperties.Add(new Dictionary<string, object>
                {
                    { "property-type", transformedProp.PropertyType },
                    { "property-name", transformedProp.PropertyName },
                    { "property-annotations", property["property-annotations"] },
                    { "property-comment", property["property-comment"] },
                    { "property-isnullable", transformedProp.PropertyIsNullable },
                    { "nullable-reference-types", property["nullable-reference-types"] },
                    { "property-iskey", property["property-iskey"]},
                    { "property-isforeignkey",property["property-isforeignkey"]}
                });

What is the right way to get the property informations ?

tonysneed commented 4 years ago

@Illinariss Response to Question 1:

Is there a way to run Scaffolding once and generate entities for more than one project at the same time? [paraphrased]

Not really. The way the EF Core CLI works is that it is scoped to just one project, called the "startup project," which must be a .NET Core library or app. You can specify a different "target project," which can be for example a .NET Standard library, but you can only scaffold one project each time you run the dotnet ef dbcontext scaffold command.

What you're going to need to do is run the scaffolding command twice: once for the client project and again for the server project. Each project will need its own CodeTemplates folder where you can customize the code generation in different ways.

tonysneed commented 4 years ago

@Illinariss Response to Question 2:

What is the right way to get the property informations?

You have the right approach. Extend HbsCSharpEntityTypeGenerator to override GenerateProperties, and extend HbsEntityTypeTransformationService to override TransformProperties.

Then add these lines of code to your ScaffoldingDesignTimeServices class:

services.AddSingleton<ICSharpEntityTypeGenerator, MyHbsCSharpEntityTypeGenerator>();
services.AddSingleton<IEntityTypeTransformationService, MyHbsEntityTypeTransformationService>();

Note that using Handlebars templates is a different approach than T4 templates, where you can add imperative code to the template. With the Handlebars approach, imperative code is added by extending the base classes, which in the end, provides more flexibility and avoids what can become spaghetti code in your templates.

Alternatively, if you think property.IsKey and property.IsForeignKey might be useful for others, you can submit a pull request to add these, as well as other common property info properties. If so, be sure to read the project Contributing Guidelines first.

tonysneed commented 4 years ago

Please re-open if I can further clarify or answer any other questions.