Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ public EntityPropertyInfo() { }
/// </summary>
/// <param name="propertyType">Property type.</param>
/// <param name="propertyName">Property name.</param>
public EntityPropertyInfo(string propertyType, string propertyName)
public EntityPropertyInfo(string propertyType, string propertyName, bool? propertyIsNullable = null)
{
PropertyType = propertyType;
PropertyName = propertyName;
PropertyIsNullable = propertyIsNullable;
}

/// <summary>
Expand All @@ -30,5 +31,10 @@ public EntityPropertyInfo(string propertyType, string propertyName)
/// Property name.
/// </summary>
public string PropertyName { get; set; }

/// <summary>
/// Gets if the property Is Nullable
/// </summary>
public bool? PropertyIsNullable { get; protected set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ private void GenerateDbSets(IModel model)

foreach (var entityType in model.GetScaffoldEntityTypes(_options.Value))
{
var transformedEntityName = EntityTypeTransformationService.TransformEntityName(entityType.Name);
var transformedEntityName = EntityTypeTransformationService.TransformEntityName(entityType);
dbSets.Add(new Dictionary<string, object>
{
{ "set-property-type", transformedEntityName },
Expand Down Expand Up @@ -357,7 +357,7 @@ private void InitializeEntityTypeBuilder(IEntityType entityType, IndentedStringB
{
if (!_entityTypeBuilderInitialized)
{
var transformedEntityName = EntityTypeTransformationService.TransformEntityName(entityType.Name);
var transformedEntityName = EntityTypeTransformationService.TransformEntityName(entityType);

sb.AppendLine();
sb.AppendLine($"modelBuilder.Entity<{transformedEntityName}>({EntityLambdaIdentifier} =>");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ protected override void GenerateClass(IEntityType entityType)
GenerateEntityTypeDataAnnotations(entityType);
}

var transformedEntityName = EntityTypeTransformationService.TransformEntityName(entityType.Name);
var transformedEntityName = EntityTypeTransformationService.TransformEntityName(entityType);

TemplateData.Add("class", transformedEntityName);

Expand Down Expand Up @@ -181,6 +181,7 @@ protected override void GenerateConstructor(IEntityType entityType)
{
{ "property-name", navigation.Name },
{ "property-type", navigation.GetTargetType().Name },
{ "property-isnullable", null }
});
}

Expand Down Expand Up @@ -213,7 +214,8 @@ protected override void GenerateProperties(IEntityType entityType)
{
{ "property-type", CSharpHelper.Reference(property.ClrType) },
{ "property-name", property.Name },
{ "property-annotations", PropertyAnnotationsData }
{ "property-annotations", PropertyAnnotationsData },
{ "property-isnullable", property.IsNullable }
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public override ScaffoldedModel GenerateModel(IModel model, ModelCodeGenerationO
options.ModelNamespace,
options.UseDataAnnotations);

var transformedFileName = EntityTypeTransformationService.TransformEntityFileName(entityType.DisplayName());
var transformedFileName = EntityTypeTransformationService.TransformEntityFileName(entityType);
var entityTypeFileName = transformedFileName + FileExtension;
resultingFiles.AdditionalFiles.Add(
new ScaffoldedFile
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using System;
using System.Collections.Generic;

namespace EntityFrameworkCore.Scaffolding.Handlebars
Expand All @@ -11,12 +13,12 @@ public class HbsEntityTypeTransformationService : IEntityTypeTransformationServi
/// <summary>
/// Entity name transformer.
/// </summary>
public Func<string, string> EntityNameTransformer { get; }
public Func<IEntityType, string> EntityNameTransformer { get; }

/// <summary>
/// Entity file name transformer.
/// </summary>
public Func<string, string> EntityFileNameTransformer { get; }
public Func<IEntityType, string> EntityFileNameTransformer { get; }

/// <summary>
/// Constructor transformer.
Expand All @@ -42,8 +44,8 @@ public class HbsEntityTypeTransformationService : IEntityTypeTransformationServi
/// <param name="propertyTransformer">Property name transformer.</param>
/// <param name="navPropertyTransformer">Navigation property name transformer.</param>
public HbsEntityTypeTransformationService(
Func<string, string> entityNameTransformer = null,
Func<string, string> entityFileNameTransformer = null,
Func<IEntityType, string> entityNameTransformer = null,
Func<IEntityType, string> entityFileNameTransformer = null,
Func<EntityPropertyInfo, EntityPropertyInfo> constructorTransformer = null,
Func<EntityPropertyInfo, EntityPropertyInfo> propertyTransformer = null,
Func<EntityPropertyInfo, EntityPropertyInfo> navPropertyTransformer = null)
Expand All @@ -57,19 +59,21 @@ public HbsEntityTypeTransformationService(

/// <summary>
/// Transform entity type name.
/// (default is .Name)
/// </summary>
/// <param name="entityName">Entity type name.</param>
/// <param name="entity">Entity type.</param>
/// <returns>Transformed entity type name.</returns>
public string TransformEntityName(string entityName) =>
EntityNameTransformer?.Invoke(entityName) ?? entityName;
public string TransformEntityName(IEntityType entity) =>
EntityNameTransformer?.Invoke(entity) ?? entity.Name;

/// <summary>
/// Transform entity file name.
/// (default is .DisplayName() from Microsoft.EntityFrameworkCore Extension)
/// </summary>
/// <param name="entityFileName">Entity file name.</param>
/// <param name="entity">Entity Type.</param>
/// <returns>Transformed entity file name.</returns>
public string TransformEntityFileName(string entityFileName) =>
EntityFileNameTransformer?.Invoke(entityFileName) ?? entityFileName;
public string TransformEntityFileName(IEntityType entity) =>
EntityFileNameTransformer?.Invoke(entity) ?? entity.DisplayName();

/// <summary>
/// Transform single property name.
Expand All @@ -94,13 +98,16 @@ public List<Dictionary<string, object>> TransformConstructor(List<Dictionary<str
foreach (var line in lines)
{
var propTypeInfo = new EntityPropertyInfo(line["property-type"] as string,
line["property-name"] as string);
line["property-name"] as string,
line["property-isnullable"] as bool?
);
var transformedProp = ConstructorTransformer?.Invoke(propTypeInfo) ?? propTypeInfo;

transformedLines.Add(new Dictionary<string, object>
{
{ "property-name", transformedProp.PropertyName },
{ "property-type", transformedProp.PropertyType },
{ "property-isnullable", transformedProp.PropertyIsNullable }
});
}

Expand All @@ -119,14 +126,16 @@ public List<Dictionary<string, object>> TransformProperties(List<Dictionary<stri
foreach (var property in properties)
{
var propTypeInfo = new EntityPropertyInfo(property["property-type"] as string,
property["property-name"] as string);
property["property-name"] as string,
property["property-isnullable"] as bool?);
var transformedProp = PropertyTransformer?.Invoke(propTypeInfo) ?? propTypeInfo;

transformedProperties.Add(new Dictionary<string, object>
{
{ "property-type", transformedProp.PropertyType },
{ "property-name", transformedProp.PropertyName },
{ "property-annotations", property["property-annotations"] }
{ "property-annotations", property["property-annotations"] },
{ "property-isnullable", transformedProp.PropertyIsNullable },
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public override SavedModelFiles Save(ScaffoldedModel scaffoldedModel, string out
foreach (var entityTypeFile in scaffoldedModel.AdditionalFiles)
{
var additionalFilePath = Path.Combine(outputDir, entityTypeFile.Path);
Directory.CreateDirectory(Path.GetDirectoryName(additionalFilePath));
File.WriteAllText(additionalFilePath, entityTypeFile.Code, Encoding.UTF8);
additionalFiles.Add(additionalFilePath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ protected override void GenerateClass(IEntityType entityType)
{
Check.NotNull(entityType, nameof(entityType));

var transformedEntityName = EntityTypeTransformationService.TransformEntityName(entityType.Name);
var transformedEntityName = EntityTypeTransformationService.TransformEntityName(entityType);

TemplateData.Add("class", transformedEntityName);

Expand Down Expand Up @@ -172,6 +172,7 @@ protected override void GenerateProperties(IEntityType entityType)
{ "property-type", TypeScriptHelper.TypeName(property.ClrType) },
{ "property-name", TypeScriptHelper.ToCamelCase(property.Name) },
{ "property-annotations", new List<Dictionary<string, object>>() },
{ "property-isnullable", property.IsNullable}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public override ScaffoldedModel GenerateModel(IModel model, ModelCodeGenerationO
options.ModelNamespace,
options.UseDataAnnotations);

var transformedFileName = EntityTypeTransformationService.TransformEntityFileName(entityType.DisplayName());
var transformedFileName = EntityTypeTransformationService.TransformEntityFileName(entityType);
var entityTypeFileName = transformedFileName + FileExtension;
resultingFiles.AdditionalFiles.Add(
new ScaffoldedFile
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore.Metadata;
using System.Collections.Generic;

namespace EntityFrameworkCore.Scaffolding.Handlebars
{
Expand All @@ -10,16 +11,16 @@ public interface IEntityTypeTransformationService
/// <summary>
/// Transform entity type name.
/// </summary>
/// <param name="entityName">Entity type name.</param>
/// <param name="entity">Entity type</param>
/// <returns>Transformed entity type name.</returns>
string TransformEntityName(string entityName);
string TransformEntityName(IEntityType entity);

/// <summary>
/// Transform entity file name.
/// </summary>
/// <param name="entityFileName">Entity file name.</param>
/// <param name="entity">Entity type.</param>
/// <returns>Transformed entity file name.</returns>
string TransformEntityFileName(string entityFileName);
string TransformEntityFileName(IEntityType entity);

/// <summary>
/// Transform single property name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using EntityFrameworkCore.Scaffolding.Handlebars;
using EntityFrameworkCore.Scaffolding.Handlebars.Helpers;
using HandlebarsDotNet;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Scaffolding;
using Microsoft.EntityFrameworkCore.Scaffolding.Internal;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -194,8 +195,8 @@ public static IServiceCollection AddHandlebarsBlockHelpers(this IServiceCollecti
/// <param name="navPropertyTransformer">Navigation property name transformer.</param>
/// <returns>The same service collection so that multiple calls can be chained.</returns>
public static IServiceCollection AddHandlebarsTransformers(this IServiceCollection services,
Func<string, string> entityNameTransformer = null,
Func<string, string> entityFileNameTransformer = null,
Func<IEntityType, string> entityNameTransformer = null,
Func<IEntityType, string> entityFileNameTransformer = null,
Func<EntityPropertyInfo, EntityPropertyInfo> constructorTransformer = null,
Func<EntityPropertyInfo, EntityPropertyInfo> propertyTransformer = null,
Func<EntityPropertyInfo, EntityPropertyInfo> navPropertyTransformer = null)
Expand Down