Skip to content

Commit a67adb9

Browse files
committed
Merge branch 'enable-prior-transformers'
2 parents 693daed + 0768048 commit a67adb9

14 files changed

+381
-183
lines changed

README.md

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -254,20 +254,27 @@ public class ScaffoldingDesignTimeServices : IDesignTimeServices
254254
// Add optional Handlebars helpers
255255
services.AddHandlebarsHelpers(myHelper);
256256

257-
// Add Handlebars transformer for Country property
258-
services.AddHandlebarsTransformers(
259-
propertyTransformer: e =>
260-
e.PropertyName == "Country"
261-
? new EntityPropertyInfo("Country", e.PropertyName, false)
262-
: new EntityPropertyInfo(e.PropertyType, e.PropertyName, e.PropertyIsNullable));
263-
264-
// Add optional Handlebars transformers
265-
//services.AddHandlebarsTransformers(
266-
// entityNameTransformer: n => n + "Foo",
267-
// entityFileNameTransformer: n => n + "Foo",
268-
// constructorTransformer: e => new EntityPropertyInfo(e.PropertyType + "Foo", e.PropertyName + "Foo"),
269-
// propertyTransformer: e => new EntityPropertyInfo(e.PropertyType, e.PropertyName + "Foo"),
270-
// navPropertyTransformer: e => new EntityPropertyInfo(e.PropertyType + "Foo", e.PropertyName + "Foo"));
257+
// Add Handlebars transformer for Country property
258+
services.AddHandlebarsTransformers(
259+
propertyTransformer: (e, p) =>
260+
p.PropertyName == "Country"
261+
? new EntityPropertyInfo("Country?", p.PropertyName, false)
262+
: new EntityPropertyInfo(p.PropertyType, p.PropertyName, p.PropertyIsNullable));
263+
264+
// Add Handlebars transformer for Id property
265+
//services.AddHandlebarsTransformers(
266+
// propertyTransformer: (e, p) =>
267+
// $"{e.Name}Id" == p.PropertyName
268+
// ? new EntityPropertyInfo(p.PropertyType, "Id", false)
269+
// : new EntityPropertyInfo(p.PropertyType, p.PropertyName, p.PropertyIsNullable));
270+
271+
// Add optional Handlebars transformers
272+
//services.AddHandlebarsTransformers(
273+
// entityTypeNameTransformer: n => n + "Foo",
274+
// entityFileNameTransformer: n => n + "Foo",
275+
// constructorTransformer: (e, p) => new EntityPropertyInfo(p.PropertyType + "Foo", p.PropertyName + "Foo"),
276+
// propertyTransformer: (e, p) => new EntityPropertyInfo(p.PropertyType, p.PropertyName + "Foo"),
277+
// navPropertyTransformer: (e, p) => new EntityPropertyInfo(p.PropertyType + "Foo", p.PropertyName + "Foo"));
271278
}
272279

273280
// Sample Handlebars helper

sample/ScaffoldingSample/Contexts/NorthwindSlimContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
7979

8080
entity.HasOne(d => d.Customer)
8181
.WithOne(p => p.CustomerSetting)
82-
.HasForeignKey<dbo.CustomerSetting (Dictionary<string, object>)>(d => d.CustomerId)
82+
.HasForeignKey<dbo.CustomerSetting>(d => d.CustomerId)
8383
.OnDelete(DeleteBehavior.ClientSetNull)
8484
.HasConstraintName("FK_CustomerSetting_Customer");
8585
});

sample/ScaffoldingSample/ScaffoldingDesignTimeServices.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void ConfigureDesignTimeServices(IServiceCollection services)
7070

7171
// Add optional Handlebars transformers
7272
//services.AddHandlebarsTransformers(
73-
// entityNameTransformer: n => n + "Foo",
73+
// entityTypeNameTransformer: n => n + "Foo",
7474
// entityFileNameTransformer: n => n + "Foo",
7575
// constructorTransformer: (e, p) => new EntityPropertyInfo(p.PropertyType + "Foo", p.PropertyName + "Foo"),
7676
// propertyTransformer: (e, p) => new EntityPropertyInfo(p.PropertyType, p.PropertyName + "Foo"),

sample/ScaffoldingSample/ScaffoldingSample.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,8 @@
1515
<ProjectReference Include="..\..\src\EntityFrameworkCore.Scaffolding.Handlebars\EntityFrameworkCore.Scaffolding.Handlebars.csproj" />
1616
</ItemGroup>
1717

18+
<ItemGroup>
19+
<Folder Include="Models\dbo\" />
20+
</ItemGroup>
21+
1822
</Project>

src/EntityFrameworkCore.Scaffolding.Handlebars/EntityFrameworkCore.Scaffolding.Handlebars.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>net6.0</TargetFramework>
55
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
6-
<Version>6.0.0-preview3</Version>
6+
<Version>6.0.0-preview4</Version>
77
<Authors>Tony Sneed</Authors>
88
<Company>Tony Sneed</Company>
99
<Title>Entity Framework Core Scaffolding with Handlebars</Title>
@@ -12,7 +12,7 @@
1212
<PackageProjectUrl>https://github.com/TrackableEntities/EntityFrameworkCore.Scaffolding.Handlebars</PackageProjectUrl>
1313
<PackageIcon>icon.png</PackageIcon>
1414
<PackageTags>scaffolding reverse-engineer entity-framework-core handlebars</PackageTags>
15-
<PackageReleaseNotes>See: https://github.com/TrackableEntities/EntityFrameworkCore.Scaffolding.Handlebars/releases/tag/v6.0.0-preview3</PackageReleaseNotes>
15+
<PackageReleaseNotes>See: https://github.com/TrackableEntities/EntityFrameworkCore.Scaffolding.Handlebars/releases/tag/v6.0.0-preview4</PackageReleaseNotes>
1616
<LangVersion>latest</LangVersion>
1717
<IncludeSource>true</IncludeSource>
1818
<SignAssembly>true</SignAssembly>

src/EntityFrameworkCore.Scaffolding.Handlebars/EntityPropertyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public EntityPropertyInfo() { }
1616
/// <param name="propertyType">Property type.</param>
1717
/// <param name="propertyName">Property name.</param>
1818
/// <param name="propertyIsNullable">Property is nullable.</param>
19-
public EntityPropertyInfo(string propertyType, string propertyName, bool propertyIsNullable = false)
19+
public EntityPropertyInfo(string propertyType, string propertyName, bool? propertyIsNullable = null)
2020
{
2121
PropertyType = propertyType;
2222
PropertyName = propertyName;
@@ -36,6 +36,6 @@ public EntityPropertyInfo(string propertyType, string propertyName, bool propert
3636
/// <summary>
3737
/// Property is nullable.
3838
/// </summary>
39-
public bool PropertyIsNullable { get; set; }
39+
public bool? PropertyIsNullable { get; set; }
4040
}
4141
}

src/EntityFrameworkCore.Scaffolding.Handlebars/HbsCSharpDbContextGenerator.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -744,13 +744,13 @@ private void GenerateRelationship(IEntityType entityType, IForeignKey foreignKey
744744
canUseDataAnnotations = false;
745745
lines.Add(
746746
$".{nameof(ReferenceReferenceBuilder.HasPrincipalKey)}"
747-
+ (foreignKey.IsUnique ? $"<{EntityTypeTransformationService.TransformPropertyName(entityType, ((ITypeBase)foreignKey.PrincipalEntityType).DisplayName(), "")}>" : "")
747+
+ (foreignKey.IsUnique ? $"<{EntityTypeTransformationService.TransformPropertyName(entityType, foreignKey.PrincipalEntityType.Name, "")}>" : "")
748748
+ $"(p => {GenerateLambdaToKey(entityType, foreignKey.PrincipalKey.Properties, "p", EntityTypeTransformationService.TransformNavPropertyName)})");
749749
}
750750

751751
lines.Add(
752752
$".{nameof(ReferenceReferenceBuilder.HasForeignKey)}"
753-
+ (foreignKey.IsUnique ? $"<{GetEntityTypeName(foreignKey.PrincipalEntityType, EntityTypeTransformationService.TransformTypeEntityName(((ITypeBase)foreignKey.DeclaringEntityType).DisplayName()))}>" : "")
753+
+ (foreignKey.IsUnique ? $"<{GetEntityTypeName(foreignKey.PrincipalEntityType, EntityTypeTransformationService.TransformTypeEntityName(foreignKey.DeclaringEntityType.Name))}>" : "")
754754
+ $"(d => {GenerateLambdaToKey(entityType, foreignKey.Properties, "d", EntityTypeTransformationService.TransformPropertyName)})");
755755

756756
var defaultOnDeleteAction = foreignKey.IsRequired
@@ -797,10 +797,10 @@ private void GenerateManyToMany(ISkipNavigation skipNavigation, IndentedStringBu
797797
var joinEntityType = skipNavigation.JoinEntityType;
798798
using (sb.Indent())
799799
{
800-
sb.AppendLine($"{EntityLambdaIdentifier}.{nameof(EntityTypeBuilder.HasMany)}(d => d.{skipNavigation.Name})");
800+
sb.AppendLine($"{EntityLambdaIdentifier}.{nameof(EntityTypeBuilder.HasMany)}(d => d.{EntityTypeTransformationService.TransformTypeEntityName(skipNavigation.Name)})");
801801
using (sb.Indent())
802802
{
803-
sb.AppendLine($".{nameof(CollectionNavigationBuilder.WithMany)}(p => p.{inverse.Name})");
803+
sb.AppendLine($".{nameof(CollectionNavigationBuilder.WithMany)}(p => p.{EntityTypeTransformationService.TransformTypeEntityName(inverse.Name)})");
804804
sb.AppendLine(
805805
$".{nameof(CollectionCollectionBuilder.UsingEntity)}<{CSharpHelper.Reference(Model.DefaultPropertyBagType)}>(");
806806
using (sb.Indent())

src/EntityFrameworkCore.Scaffolding.Handlebars/HbsCSharpEntityTypeGenerator.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -262,12 +262,10 @@ protected override void GenerateProperties(IEntityType entityType)
262262
var propertyType = CSharpHelper.Reference(property.ClrType);
263263
if (UseNullableReferenceTypes
264264
&& property.IsNullable
265-
&& !propertyType.EndsWith("?")) {
266-
propertyType += "?";
267-
}
268-
var propertyIsNullable = property.ClrType.IsValueType
269-
|| (UseNullableReferenceTypes
270-
&& property.IsNullable);
265+
&& !propertyType.EndsWith("?"))
266+
{ propertyType += "?"; }
267+
var propertyIsNullable = UseNullableReferenceTypes
268+
&& (property.ClrType.IsValueType || property.IsNullable);
271269
properties.Add(new Dictionary<string, object>
272270
{
273271
{ "property-type", propertyType },
@@ -343,10 +341,6 @@ protected override void GenerateNavigationProperties(IEntityType entityType)
343341
UseNullableReferenceTypes &&
344342
!navigation.ForeignKey.IsRequired;
345343
var navPropertyType = navigation.TargetEntityType.Name;
346-
if (propertyIsNullable &&
347-
!navPropertyType.EndsWith("?")) {
348-
navPropertyType += "?";
349-
}
350344
navProperties.Add(new Dictionary<string, object>
351345
{
352346
{ "nav-property-collection", navigation.IsCollection },
Lines changed: 15 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,35 @@
1-
using Microsoft.EntityFrameworkCore.Metadata;
2-
using System;
3-
using System.Collections.Generic;
1+
using System;
42
namespace EntityFrameworkCore.Scaffolding.Handlebars
53
{
64
/// <summary>
75
/// Default service for transforming entity type definitions.
86
/// </summary>
9-
public class HbsEntityTypeTransformationService : IEntityTypeTransformationService
10-
{
7+
public class HbsEntityTypeTransformationService : HbsEntityTypeTransformationServiceBase
8+
{
119
/// <summary>
1210
/// Entity name transformer.
1311
/// </summary>
14-
public Func<string, string> EntityTypeNameTransformer { get; }
12+
public new Func<string, string> EntityTypeNameTransformer { get => base.EntityTypeNameTransformer; }
1513

1614
/// <summary>
1715
/// Entity file name transformer.
1816
/// </summary>
19-
public Func<string, string> EntityFileNameTransformer { get; }
17+
public new Func<string, string> EntityFileNameTransformer { get => base.EntityFileNameTransformer; }
2018

2119
/// <summary>
2220
/// Constructor transformer.
2321
/// </summary>
24-
public Func<IEntityType, EntityPropertyInfo, EntityPropertyInfo> ConstructorTransformer { get; }
22+
public new Func<EntityPropertyInfo, EntityPropertyInfo> ConstructorTransformer { get => base.ConstructorTransformer; }
2523

2624
/// <summary>
2725
/// Property name transformer.
2826
/// </summary>
29-
public Func<IEntityType, EntityPropertyInfo, EntityPropertyInfo> PropertyTransformer { get; }
27+
public new Func<EntityPropertyInfo, EntityPropertyInfo> PropertyTransformer { get => base.PropertyTransformer; }
3028

3129
/// <summary>
3230
/// Navigation property name transformer.
3331
/// </summary>
34-
public Func<IEntityType, EntityPropertyInfo, EntityPropertyInfo> NavPropertyTransformer { get; }
32+
public new Func<EntityPropertyInfo, EntityPropertyInfo> NavPropertyTransformer { get => base.NavPropertyTransformer; }
3533

3634
/// <summary>
3735
/// HbsEntityTypeTransformationService constructor.
@@ -44,142 +42,14 @@ public class HbsEntityTypeTransformationService : IEntityTypeTransformationServi
4442
public HbsEntityTypeTransformationService(
4543
Func<string, string> entityTypeNameTransformer = null,
4644
Func<string, string> entityFileNameTransformer = null,
47-
Func<IEntityType, EntityPropertyInfo, EntityPropertyInfo> constructorTransformer = null,
48-
Func<IEntityType, EntityPropertyInfo, EntityPropertyInfo> propertyTransformer = null,
49-
Func<IEntityType, EntityPropertyInfo, EntityPropertyInfo> navPropertyTransformer = null)
45+
Func<EntityPropertyInfo, EntityPropertyInfo> constructorTransformer = null,
46+
Func<EntityPropertyInfo, EntityPropertyInfo> propertyTransformer = null,
47+
Func<EntityPropertyInfo, EntityPropertyInfo> navPropertyTransformer = null)
48+
: base(entityTypeNameTransformer, entityFileNameTransformer)
5049
{
51-
EntityTypeNameTransformer = entityTypeNameTransformer;
52-
EntityFileNameTransformer = entityFileNameTransformer;
53-
ConstructorTransformer = constructorTransformer;
54-
PropertyTransformer = propertyTransformer;
55-
NavPropertyTransformer = navPropertyTransformer;
56-
}
57-
58-
/// <summary>
59-
/// Transform entity type name.
60-
/// </summary>
61-
/// <param name="entityName">Entity type name.</param>
62-
/// <returns>Transformed entity type name.</returns>
63-
public string TransformTypeEntityName(string entityName) =>
64-
EntityTypeNameTransformer?.Invoke(entityName) ?? entityName;
65-
66-
/// <summary>
67-
/// Transform entity file name.
68-
/// </summary>
69-
/// <param name="entityFileName">Entity file name.</param>
70-
/// <returns>Transformed entity file name.</returns>
71-
public string TransformEntityFileName(string entityFileName) =>
72-
EntityFileNameTransformer?.Invoke(entityFileName) ?? entityFileName;
73-
74-
/// <summary>
75-
/// Transform single property name.
76-
/// </summary>
77-
/// <param name="entityType">Entity type.</param>
78-
/// <param name="propertyName">Property name.</param>
79-
/// <param name="propertyType">Property type</param>
80-
/// <returns>Transformed property name.</returns>
81-
public string TransformPropertyName(IEntityType entityType, string propertyName, string propertyType)
82-
{
83-
var propTypeInfo = new EntityPropertyInfo { PropertyName = propertyName, PropertyType = propertyType };
84-
return PropertyTransformer?.Invoke(entityType, propTypeInfo)?.PropertyName ?? propertyName;
85-
}
86-
87-
/// <summary>
88-
/// Transform single navigation property name.
89-
/// </summary>
90-
/// <param name="entityType">Entity type.</param>
91-
/// <param name="propertyName">Property name.</param>
92-
/// <param name="propertyType">Property type</param>
93-
/// <returns>Transformed property name.</returns>
94-
public string TransformNavPropertyName(IEntityType entityType, string propertyName, string propertyType)
95-
{
96-
var propTypeInfo = new EntityPropertyInfo { PropertyName = propertyName, PropertyType = propertyType };
97-
return NavPropertyTransformer?.Invoke(entityType, propTypeInfo)?.PropertyName ?? propertyName;
98-
}
99-
100-
/// <summary>
101-
/// Transform entity type constructor.
102-
/// </summary>
103-
/// <param name="entityType">Entity type.</param>
104-
/// <param name="lines">Constructor lines.</param>
105-
/// <returns>Transformed constructor lines.</returns>
106-
public List<Dictionary<string, object>> TransformConstructor(IEntityType entityType, List<Dictionary<string, object>> lines)
107-
{
108-
var transformedLines = new List<Dictionary<string, object>>();
109-
110-
foreach (var line in lines)
111-
{
112-
var propTypeInfo = new EntityPropertyInfo(line["property-type"] as string,
113-
line["property-name"] as string);
114-
var transformedProp = ConstructorTransformer?.Invoke(entityType, propTypeInfo) ?? propTypeInfo;
115-
116-
transformedLines.Add(new Dictionary<string, object>
117-
{
118-
{ "property-name", transformedProp.PropertyName },
119-
{ "property-type", transformedProp.PropertyType }
120-
});
121-
}
122-
123-
return transformedLines;
124-
}
125-
126-
/// <summary>
127-
/// Transform entity type properties.
128-
/// </summary>
129-
/// <param name="entityType">Entity type.</param>
130-
/// <param name="properties">Entity type properties.</param>
131-
/// <returns>Transformed entity type properties.</returns>
132-
public List<Dictionary<string, object>> TransformProperties(IEntityType entityType, List<Dictionary<string, object>> properties)
133-
{
134-
var transformedProperties = new List<Dictionary<string, object>>();
135-
136-
foreach (var property in properties)
137-
{
138-
var propTypeInfo = new EntityPropertyInfo(property["property-type"] as string,
139-
property["property-name"] as string,
140-
(property["property-isnullable"] as bool?) == true);
141-
var transformedProp = PropertyTransformer?.Invoke(entityType, propTypeInfo) ?? propTypeInfo;
142-
143-
transformedProperties.Add(new Dictionary<string, object>
144-
{
145-
{ "property-type", transformedProp.PropertyType },
146-
{ "property-name", transformedProp.PropertyName },
147-
{ "property-annotations", property["property-annotations"] },
148-
{ "property-comment", property["property-comment"] },
149-
{ "property-isnullable", transformedProp.PropertyIsNullable },
150-
{ "nullable-reference-types", property["nullable-reference-types"] }
151-
});
152-
}
153-
154-
return transformedProperties;
155-
}
156-
157-
/// <summary>
158-
/// Transform entity type navigation properties.
159-
/// </summary>
160-
/// <param name="entityType">Entity type.</param>
161-
/// <param name="navProperties">Entity type navigation properties.</param>
162-
/// <returns>Transformed entity type navigation properties.</returns>
163-
public List<Dictionary<string, object>> TransformNavigationProperties(IEntityType entityType, List<Dictionary<string, object>> navProperties)
164-
{
165-
var transformedNavProperties = new List<Dictionary<string, object>>();
166-
167-
foreach (var navProperty in navProperties)
168-
{
169-
var propTypeInfo = new EntityPropertyInfo(navProperty["nav-property-type"] as string,
170-
navProperty["nav-property-name"] as string);
171-
var transformedProp = NavPropertyTransformer?.Invoke(entityType, propTypeInfo) ?? propTypeInfo;
172-
173-
var newNavProperty = new Dictionary<string, object>(navProperty)
174-
{
175-
["nav-property-type"] = transformedProp.PropertyType,
176-
["nav-property-name"] = transformedProp.PropertyName
177-
};
178-
179-
transformedNavProperties.Add(newNavProperty);
180-
}
181-
182-
return transformedNavProperties;
50+
base.ConstructorTransformer = constructorTransformer;
51+
base.PropertyTransformer = propertyTransformer;
52+
base.NavPropertyTransformer = navPropertyTransformer;
18353
}
18454
}
18555
}

0 commit comments

Comments
 (0)