Skip to content

Add IEntityType versions for Entity Type Name and Entity File Name transformers #245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -291,7 +291,7 @@ private void GenerateDbSets(IModel model)
}

var transformedEntityTypeName = GetEntityTypeName(
entityType, EntityTypeTransformationService.TransformTypeEntityName(entityType.Name));
entityType, EntityTypeTransformationService.TransformTypeEntityName(entityType, entityType.Name));
dbSets.Add(new Dictionary<string, object>
{
{ "set-property-type", transformedEntityTypeName },
Expand Down Expand Up @@ -327,7 +327,7 @@ private void InitializeEntityTypeBuilder(IEntityType entityType, IndentedStringB
if (!_entityTypeBuilderInitialized)
{
var transformedEntityTypeName = GetEntityTypeName(
entityType, EntityTypeTransformationService.TransformTypeEntityName(entityType.Name));
entityType, EntityTypeTransformationService.TransformTypeEntityName(entityType, entityType.Name));

sb.AppendLine();
sb.AppendLine($"modelBuilder.Entity<{transformedEntityTypeName}>({EntityLambdaIdentifier} =>");
Expand Down Expand Up @@ -536,7 +536,7 @@ private void GenerateTableName(IEntityType entityType, IndentedStringBuilder sb)
var schema = entityType.GetSchema();
var defaultSchema = entityType.Model.GetDefaultSchema();

var transformedTableName = EntityTypeTransformationService.TransformTypeEntityName(tableName);
var transformedTableName = EntityTypeTransformationService.TransformTypeEntityName(entityType, tableName);
var tableNameVirtual = tableName != null && !tableName.Equals(transformedTableName);

var explicitSchema = schema != null && schema != defaultSchema;
Expand Down Expand Up @@ -773,7 +773,7 @@ private void GenerateRelationship(IEntityType entityType, IForeignKey foreignKey

lines.Add(
$".{nameof(ReferenceReferenceBuilder.HasForeignKey)}"
+ (foreignKey.IsUnique ? $"<{GetEntityTypeName(entityType, EntityTypeTransformationService.TransformTypeEntityName(entityType.Name))}>" : "")
+ (foreignKey.IsUnique ? $"<{GetEntityTypeName(entityType, EntityTypeTransformationService.TransformTypeEntityName(entityType, entityType.Name))}>" : "")
+ $"(d => {GenerateLambdaToKey(entityType, foreignKey.Properties, "d", EntityTypeTransformationService.TransformPropertyName)})");

var defaultOnDeleteAction = foreignKey.IsRequired
Expand Down Expand Up @@ -820,19 +820,19 @@ private void GenerateManyToMany(ISkipNavigation skipNavigation, IndentedStringBu
var joinEntityType = skipNavigation.JoinEntityType;
using (sb.Indent())
{
sb.AppendLine($"{EntityLambdaIdentifier}.{nameof(EntityTypeBuilder.HasMany)}(d => d.{EntityTypeTransformationService.TransformTypeEntityName(skipNavigation.Name)})");
sb.AppendLine($"{EntityLambdaIdentifier}.{nameof(EntityTypeBuilder.HasMany)}(d => d.{EntityTypeTransformationService.TransformTypeEntityName(skipNavigation.JoinEntityType, skipNavigation.Name)})");
using (sb.Indent())
{
sb.AppendLine($".{nameof(CollectionNavigationBuilder.WithMany)}(p => p.{EntityTypeTransformationService.TransformTypeEntityName(inverse.Name)})");
sb.AppendLine($".{nameof(CollectionNavigationBuilder.WithMany)}(p => p.{EntityTypeTransformationService.TransformTypeEntityName(inverse.DeclaringEntityType, inverse.Name)})");
sb.AppendLine(
$".{nameof(CollectionCollectionBuilder.UsingEntity)}<{CSharpHelper.Reference(Model.DefaultPropertyBagType)}>(");
using (sb.Indent())
{
sb.AppendLine($"{CSharpHelper.Literal(joinEntityType.Name)},");
var lines = new List<string>();

var navEntityTypeName = GetEntityTypeName(inverse.ForeignKey.PrincipalEntityType, EntityTypeTransformationService.TransformTypeEntityName(inverse.ForeignKey.PrincipalEntityType.Name));
var skipNavEntityTypeName = GetEntityTypeName(skipNavigation.ForeignKey.PrincipalEntityType, EntityTypeTransformationService.TransformTypeEntityName(skipNavigation.ForeignKey.PrincipalEntityType.Name));
var navEntityTypeName = GetEntityTypeName(inverse.ForeignKey.PrincipalEntityType, EntityTypeTransformationService.TransformTypeEntityName(inverse.ForeignKey.PrincipalEntityType, inverse.ForeignKey.PrincipalEntityType.Name));
var skipNavEntityTypeName = GetEntityTypeName(skipNavigation.ForeignKey.PrincipalEntityType, EntityTypeTransformationService.TransformTypeEntityName(skipNavigation.ForeignKey.PrincipalEntityType, skipNavigation.ForeignKey.PrincipalEntityType.Name));
GenerateForeignKeyConfigurationLines(inverse.ForeignKey, navEntityTypeName, "l");
GenerateForeignKeyConfigurationLines(skipNavigation.ForeignKey, skipNavEntityTypeName, "r");
sb.AppendLine("j =>");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ protected virtual void GenerateClass(IEntityType entityType)
GenerateEntityTypeDataAnnotations(entityType);
}

var transformedEntityName = EntityTypeTransformationService.TransformTypeEntityName(entityType.Name);
var transformedEntityName = EntityTypeTransformationService.TransformTypeEntityName(entityType, entityType.Name);

if (_options?.Value?.GenerateComments == true)
TemplateData.Add("comment", GenerateComment(entityType.GetComment(), 1));
Expand Down Expand Up @@ -220,10 +220,11 @@ protected virtual void GenerateConstructor(IEntityType entityType)

foreach (var navigation in collectionNavigations)
{
var navPropertyType = EntityTypeTransformationService.TransformTypeEntityName(navigation.TargetEntityType, navigation.TargetEntityType.Name);
lines.Add(new Dictionary<string, object>
{
{ "property-name", navigation.Name },
{ "property-type", navigation.TargetEntityType.Name }
{ "property-type", navPropertyType }
});
}

Expand Down Expand Up @@ -335,7 +336,7 @@ protected virtual void GenerateNavigationProperties(IEntityType entityType)
var propertyIsNullable = !navigation.IsCollection &&
UseNullableReferenceTypes &&
!navigation.ForeignKey.IsRequired;
var navPropertyType = navigation.TargetEntityType.Name;
var navPropertyType = EntityTypeTransformationService.TransformTypeEntityName(navigation.TargetEntityType, navigation.TargetEntityType.Name);
navProperties.Add(new Dictionary<string, object>
{
{ "nav-property-collection", navigation.IsCollection },
Expand Down Expand Up @@ -378,7 +379,7 @@ protected virtual void GenerateSkipNavigationProperties(IEntityType entityType)
var propertyIsNullable = !navigation.IsCollection &&
UseNullableReferenceTypes &&
!navigation.ForeignKey.IsRequired;
var navPropertyType = navigation.TargetEntityType.Name;
var navPropertyType = EntityTypeTransformationService.TransformTypeEntityName(navigation.TargetEntityType, navigation.TargetEntityType.Name);
if (propertyIsNullable &&
!navPropertyType.EndsWith("?")) {
navPropertyType += "?";
Expand Down Expand Up @@ -689,7 +690,7 @@ private void GenerateInversePropertyAttribute(IEntityType entityType, INavigatio
m => m.Name == inverseNavigation.DeclaringEntityType.Name ||
EntityTypeTransformationService.TransformNavPropertyName(entityType, m.Name, navigation.TargetEntityType.Name)
== EntityTypeTransformationService.TransformNavPropertyName(entityType, inverseNavigation.DeclaringEntityType.Name, navigation.TargetEntityType.Name))
? $"nameof({EntityTypeTransformationService.TransformTypeEntityName(inverseNavigation.DeclaringType.Name)}.{propertyName})"
? $"nameof({EntityTypeTransformationService.TransformTypeEntityName(inverseNavigation.DeclaringEntityType, inverseNavigation.DeclaringType.Name)}.{propertyName})"
: CSharpHelper.Literal(propertyName));

NavPropertyAnnotations.Add(new Dictionary<string, object>
Expand Down Expand Up @@ -766,7 +767,7 @@ private void GenerateInversePropertyAttribute(IEntityType entityType, ISkipNavig
m => m.Name == inverseNavigation.DeclaringEntityType.Name ||
EntityTypeTransformationService.TransformNavPropertyName(entityType, m.Name, navigation.TargetEntityType.Name)
== EntityTypeTransformationService.TransformNavPropertyName(entityType, inverseNavigation.DeclaringEntityType.Name, navigation.TargetEntityType.Name))
? $"nameof({EntityTypeTransformationService.TransformTypeEntityName(inverseNavigation.DeclaringType.Name)}.{propertyName})"
? $"nameof({EntityTypeTransformationService.TransformTypeEntityName(inverseNavigation.DeclaringEntityType, inverseNavigation.DeclaringType.Name)}.{propertyName})"
: CSharpHelper.Literal(propertyName));

NavPropertyAnnotations.Add(new Dictionary<string, object>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public virtual ScaffoldedModel GenerateModel(IModel model, ModelCodeGenerationOp
options.UseDataAnnotations,
options.UseNullableReferenceTypes);

var transformedFileName = EntityTypeTransformationService.TransformEntityFileName(entityType.Name);
var transformedFileName = EntityTypeTransformationService.TransformEntityFileName(entityType, entityType.Name);
var schema = !string.IsNullOrEmpty(entityType.GetTableName())
? entityType.GetSchema()
: entityType.GetViewSchema();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,27 @@ public HbsEntityTypeTransformationService2(
PropertyTransformer2 = propertyTransformer;
NavPropertyTransformer2 = navPropertyTransformer;
}

/// <summary>
/// HbsEntityTypeTransformationService constructor.
/// </summary>
/// <param name="entityTypeNameTransformer">Entity type name transformer.</param>
/// <param name="entityFileNameTransformer">Entity file name transformer.</param>
/// <param name="constructorTransformer">Constructor transformer.</param>
/// <param name="propertyTransformer">Property name transformer.</param>
/// <param name="navPropertyTransformer">Navigation property name transformer.</param>
public HbsEntityTypeTransformationService2(
Func<IEntityType, string, string> entityTypeNameTransformer = null,
Func<IEntityType, string, string> entityFileNameTransformer = null,
Func<IEntityType, EntityPropertyInfo, EntityPropertyInfo> constructorTransformer = null,
Func<IEntityType, EntityPropertyInfo, EntityPropertyInfo> propertyTransformer = null,
Func<IEntityType, EntityPropertyInfo, EntityPropertyInfo> navPropertyTransformer = null)
{
EntityTypeNameTransformer2 = entityTypeNameTransformer;
EntityFileNameTransformer2 = entityFileNameTransformer;
ConstructorTransformer2 = constructorTransformer;
PropertyTransformer2 = propertyTransformer;
NavPropertyTransformer2 = navPropertyTransformer;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ public abstract class HbsEntityTypeTransformationServiceBase : IEntityTypeTransf
/// </summary>
protected Func<EntityPropertyInfo, EntityPropertyInfo> NavPropertyTransformer { get; set; }

/// <summary>
/// Entity name transformer.
/// </summary>
protected Func<IEntityType, string, string> EntityTypeNameTransformer2 { get; set; }

/// <summary>
/// Entity file name transformer.
/// </summary>
protected Func<IEntityType, string, string> EntityFileNameTransformer2 { get; set; }

/// <summary>
/// Constructor transformer.
/// </summary>
Expand Down Expand Up @@ -69,6 +79,17 @@ public HbsEntityTypeTransformationServiceBase(
public string TransformTypeEntityName(string entityName) =>
EntityTypeNameTransformer?.Invoke(entityName) ?? entityName;

/// <summary>
/// Transform entity type name.
/// </summary>
/// <param name="entityType">Entity type.</param>
/// <param name="entityName">Entity type name.</param>
/// <returns>Transformed entity type name.</returns>
public string TransformTypeEntityName(IEntityType entityType, string entityName) =>
EntityTypeNameTransformer2?.Invoke(entityType, entityName)
?? EntityTypeNameTransformer?.Invoke(entityName)
?? entityName;

/// <summary>
/// Transform entity file name.
/// </summary>
Expand All @@ -77,6 +98,17 @@ public string TransformTypeEntityName(string entityName) =>
public string TransformEntityFileName(string entityFileName) =>
EntityFileNameTransformer?.Invoke(entityFileName) ?? entityFileName;

/// <summary>
/// Transform entity file name.
/// </summary>
/// <param name="entityType">Entity type.</param>
/// <param name="entityFileName">Entity file name.</param>
/// <returns>Transformed entity file name.</returns>
public string TransformEntityFileName(IEntityType entityType, string entityFileName) =>
EntityFileNameTransformer2?.Invoke(entityType, entityFileName)
?? EntityFileNameTransformer?.Invoke(entityFileName)
?? entityFileName;

/// <summary>
/// Transform single property name.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ protected virtual void GenerateClass(IEntityType entityType)
{
Check.NotNull(entityType, nameof(entityType));

var transformedEntityName = EntityTypeTransformationService.TransformTypeEntityName(entityType.Name);
var transformedEntityName = EntityTypeTransformationService.TransformTypeEntityName(entityType, entityType.Name);

TemplateData.Add("comment", entityType.GetComment());
TemplateData.Add("class", transformedEntityName);
Expand All @@ -158,10 +158,11 @@ protected virtual void GenerateConstructor(IEntityType entityType)

foreach (var navigation in collectionNavigations)
{
var navPropertyType = EntityTypeTransformationService.TransformTypeEntityName(navigation.TargetEntityType, navigation.TargetEntityType.Name);
lines.Add(new Dictionary<string, object>
{
{ "property-name", navigation.Name },
{ "property-type", navigation.TargetEntityType.Name },
{ "property-type", navPropertyType },
});
}

Expand Down Expand Up @@ -219,10 +220,11 @@ protected virtual void GenerateNavigationProperties(IEntityType entityType)

foreach (var navigation in sortedNavigations)
{
var navPropertyType = EntityTypeTransformationService.TransformTypeEntityName(navigation.TargetEntityType, navigation.TargetEntityType.Name);
navProperties.Add(new Dictionary<string, object>
{
{ "nav-property-collection", navigation.IsCollection },
{ "nav-property-type", navigation.TargetEntityType.Name },
{ "nav-property-type", navPropertyType },
{ "nav-property-name", TypeScriptHelper.ToCamelCase(navigation.Name) },
{ "nav-property-annotations", new List<Dictionary<string, object>>() },
{ "nav-property-isnullable", false },
Expand Down Expand Up @@ -254,10 +256,11 @@ protected virtual void GenerateSkipNavigationProperties(IEntityType entityType)

foreach (var navigation in sortedNavigations)
{
var navPropertyType = EntityTypeTransformationService.TransformTypeEntityName(navigation.TargetEntityType, navigation.TargetEntityType.Name);
navProperties.Add(new Dictionary<string, object>
{
{ "nav-property-collection", navigation.IsCollection },
{ "nav-property-type", navigation.TargetEntityType.Name },
{ "nav-property-type", navPropertyType },
{ "nav-property-name", TypeScriptHelper.ToCamelCase(navigation.Name) },
{ "nav-property-annotations", new List<Dictionary<string, object>>() },
{ "nav-property-isnullable", false },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public virtual ScaffoldedModel GenerateModel(IModel model, ModelCodeGenerationOp
options.UseDataAnnotations,
options.UseNullableReferenceTypes);

var transformedFileName = EntityTypeTransformationService.TransformEntityFileName(entityType.Name);
var transformedFileName = EntityTypeTransformationService.TransformEntityFileName(entityType, entityType.Name);
var entityTypeFileName = transformedFileName + FileExtension;
if (_options?.Value?.EnableSchemaFolders == true) {
entityTypeFileName = entityType.GetSchema() + @"\" + entityTypeFileName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,35 @@ public interface IEntityTypeTransformationService
/// <returns>Transformed entity type name.</returns>
string TransformTypeEntityName(string entityName);

/// <summary>
/// Transform entity type name.
/// </summary>
/// <param name="entityType">Entity type.</param>
/// <param name="entityName">Entity type name.</param>
/// <returns>Transformed entity type name.</returns>
string TransformTypeEntityName(IEntityType entityType, string entityName)
{
return TransformTypeEntityName(entityName);
}

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

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

/// <summary>
/// Transform single property name.
/// </summary>
Expand Down
Loading