Use new model for ef method

This commit is contained in:
CodeLiturgy 2022-09-04 22:11:04 +01:00
parent 3917d231a2
commit 9211cde83c
4 changed files with 102 additions and 85 deletions

View File

@ -1,14 +0,0 @@
using MapTo.Sources;
namespace MapTo.Extensions
{
internal static class EfGeneratorExtensions
{
internal static SourceCode GenerateEfMethods(this EfMethodsModel model)
{
var builder = new SourceBuilder();
return new SourceCode(builder.ToString(), $"{model.Namespace}.{model.EntityTypeIdentifierName}.g.cs");
}
}
}

View File

@ -68,39 +68,44 @@ namespace MapTo
ImmutableArray<string> Usings ImmutableArray<string> Usings
); );
internal interface IEfMethodsConfiguration {} internal interface IEfMethodsModel { }
internal record EfMethodsModel(
SourceGenerationOptions Options,
string Namespace,
string ContextTypeName,
string ContextFullType,
ImmutableArray<IEfMethodsModel> MethodsModels,
ImmutableArray<string> Usings
);
internal record EfEntityDataModel(
string PropertyName,
string EntityTypeFullName,
string EntityTypeIdentifierName
);
internal record EfAddMethodsModel( internal record EfAddMethodsModel(
EfEntityDataModel Entity,
string CreateTypeFullName, string CreateTypeFullName,
string CreateTypeIdentifierName, string CreateTypeIdentifierName,
string ReturnTypeFullName, string ReturnTypeFullName,
string ReturnTypeIdentifierName string ReturnTypeIdentifierName
) : IEfMethodsConfiguration; ) : IEfMethodsModel;
internal record EfUpdateMethodsModel( internal record EfUpdateMethodsModel(
EfEntityDataModel Entity,
string UpdateTypeFullName, string UpdateTypeFullName,
string UpdateTypeIdentifierName, string UpdateTypeIdentifierName,
string ReturnTypeFullName, string ReturnTypeFullName,
string ReturnTypeIdentifierName, string ReturnTypeIdentifierName,
string keyPropertyName, string keyPropertyName,
string keyFullTypeName string keyFullTypeName
): IEfMethodsConfiguration; ): IEfMethodsModel;
internal record EfMethodsModel(
SourceGenerationOptions Options,
string Namespace,
string PropertyName,
string ContextTypeName,
string ContextFullType,
string EntityTypeFullName,
string EntityTypeIdentifierName,
EfAddMethodsModel AddMethodsModel,
EfUpdateMethodsModel UpdateMethodsModel,
ImmutableArray<string> Usings
);
internal record SourceGenerationOptions( internal record SourceGenerationOptions(
AccessModifier ConstructorAccessModifier, AccessModifier ConstructorAccessModifier,
AccessModifier GeneratedMethodsAccessModifier, AccessModifier GeneratedMethodsAccessModifier,

View File

@ -272,8 +272,10 @@ namespace MapTo
propertyName = fieldDeclaration.ToString(); propertyName = fieldDeclaration.ToString();
} }
var efEntity = new EfEntityDataModel(propertyName, entityTypeFullName, entityTypeName);
var addConfig = ExtractEfAddMethodsModel(semanticModel, entityTypeName, entityTypeFullName);
var addConfig = ExtractEfAddMethodsModel(semanticModel, efEntity);
if (addConfig == null) if (addConfig == null)
{ {
@ -281,7 +283,7 @@ namespace MapTo
return null; return null;
} }
var updateConfig = ExtractEfUpdateMethodsModel(semanticModel, entityTypeName, entityTypeFullName); var updateConfig = ExtractEfUpdateMethodsModel(semanticModel, efEntity);
if (updateConfig == null) if (updateConfig == null)
{ {
@ -289,30 +291,31 @@ namespace MapTo
return null; return null;
} }
// Create list of configurations
List<IEfMethodsModel> models = new List<IEfMethodsModel>();
models.Add(addConfig);
models.Add(updateConfig);
return new EfMethodsModel( return new EfMethodsModel(
SourceGenerationOptions, SourceGenerationOptions,
contextNamespace, contextNamespace,
propertyName, dbContextName, contextTypeFullName,
dbContextName, models.ToImmutableArray(),
contextTypeFullName,
entityTypeFullName,
entityTypeName,
addConfig,
updateConfig,
Usings); Usings);
} }
private EfAddMethodsModel ExtractEfAddMethodsModel(SemanticModel semanticModel, string entityTypeName, string entityTypeFullName) private EfAddMethodsModel ExtractEfAddMethodsModel(SemanticModel semanticModel, EfEntityDataModel efEntityDataModel)
{ {
var efAddAttributeTypeSymbols = var efAddAttributeTypeSymbols =
GetEntityTypeSymbol(MemberSyntax, EfAddMethodsAttributeSource.AttributeName, semanticModel); GetEntityTypeSymbol(MemberSyntax, EfAddMethodsAttributeSource.AttributeName, semanticModel);
string createTypeIdentifierName = entityTypeName; string createTypeIdentifierName = efEntityDataModel.EntityTypeIdentifierName;
string createTypeFullName = entityTypeFullName; string createTypeFullName = efEntityDataModel.EntityTypeFullName;
string returnTypeIdentifierName = entityTypeName; string returnTypeIdentifierName = efEntityDataModel.EntityTypeIdentifierName;
string returnTypeFullName = entityTypeFullName; string returnTypeFullName = efEntityDataModel.EntityTypeFullName;
if (efAddAttributeTypeSymbols.Length > 0) if (efAddAttributeTypeSymbols.Length > 0)
@ -328,11 +331,11 @@ namespace MapTo
} }
return new EfAddMethodsModel(createTypeFullName, createTypeIdentifierName, returnTypeFullName, return new EfAddMethodsModel(efEntityDataModel, createTypeFullName, createTypeIdentifierName, returnTypeFullName,
returnTypeIdentifierName); returnTypeIdentifierName);
} }
private EfUpdateMethodsModel ExtractEfUpdateMethodsModel(SemanticModel semanticModel, string entityTypeName, string entityTypeFullName) private EfUpdateMethodsModel ExtractEfUpdateMethodsModel(SemanticModel semanticModel,EfEntityDataModel efEntityDataModel)
{ {
var efAddAttributeTypeSymbols = var efAddAttributeTypeSymbols =
GetEntityTypeSymbol(MemberSyntax, EfUpdateMethodsAttributeSource.AttributeName, semanticModel); GetEntityTypeSymbol(MemberSyntax, EfUpdateMethodsAttributeSource.AttributeName, semanticModel);
@ -345,6 +348,7 @@ namespace MapTo
var keyMemberFullName = keyMemberType.Value.Type.ToDisplayString(); var keyMemberFullName = keyMemberType.Value.Type.ToDisplayString();
var keyMemberName = keyMemberType.Value.Type.Name; var keyMemberName = keyMemberType.Value.Type.Name;
@ -358,10 +362,10 @@ namespace MapTo
if (efAddAttributeTypeSymbols == null) return null; if (efAddAttributeTypeSymbols == null) return null;
string updateTypeIdentifierName = entityTypeName; string updateTypeIdentifierName = efEntityDataModel.EntityTypeIdentifierName;
string updateTypeFullName = entityTypeFullName; string updateTypeFullName = efEntityDataModel.EntityTypeFullName;
string returnTypeIdentifierName = entityTypeName; string returnTypeIdentifierName = efEntityDataModel.EntityTypeIdentifierName;
string returnTypeFullName = entityTypeFullName; string returnTypeFullName = efEntityDataModel.EntityTypeFullName;
if (efAddAttributeTypeSymbols.Length > 0) if (efAddAttributeTypeSymbols.Length > 0)
{ {
@ -377,8 +381,7 @@ namespace MapTo
} }
return new EfUpdateMethodsModel(efEntityDataModel, updateTypeFullName, updateTypeIdentifierName, returnTypeFullName,
return new EfUpdateMethodsModel(updateTypeFullName, updateTypeIdentifierName, returnTypeFullName,
returnTypeIdentifierName, keyPropertyName, keyMemberFullName); returnTypeIdentifierName, keyPropertyName, keyMemberFullName);
} }

View File

@ -15,10 +15,8 @@ namespace MapTo.Sources
{ {
using var builder = new SourceBuilder(); using var builder = new SourceBuilder();
var entityTypeName = model.EntityTypeIdentifierName;
var entityTypeFullName = model.EntityTypeFullName;
var contextFullName = model.ContextFullType; var contextFullName = model.ContextFullType;
var propertyName = model.PropertyName;
builder builder
.WriteLine(GeneratedFilesHeader) .WriteLine(GeneratedFilesHeader)
@ -37,19 +35,44 @@ namespace MapTo.Sources
.WriteOpeningBracket() .WriteOpeningBracket()
.WriteLine(); .WriteLine();
builder var entityTypeName = "";
.EfAddAddEntityMethod(model, addSourceTemplate, foreach (var methodModel in model.MethodsModels)
entityTypeName, {
contextFullName, if (methodModel is EfAddMethodsModel m)
entityTypeFullName, {
propertyName) entityTypeName = m.Entity.EntityTypeIdentifierName;
.WriteLine() var entityTypeFullName = m.Entity.EntityTypeFullName;
.EfAddUpdateEntityMethod(model, updateSourceTemplate, var propertyName = m.Entity.PropertyName;
entityTypeName,
contextFullName,
entityTypeFullName,
propertyName)
builder
.EfAddAddEntityMethod(m, addSourceTemplate,
entityTypeName,
contextFullName,
entityTypeFullName,
propertyName);
}
if (methodModel is EfUpdateMethodsModel updateModel)
{
entityTypeName = updateModel.Entity.EntityTypeIdentifierName;
var entityTypeFullName = updateModel.Entity.EntityTypeFullName;
var propertyName = updateModel.Entity.PropertyName;
builder
.WriteLine()
.EfAddUpdateEntityMethod(updateModel, updateSourceTemplate,
entityTypeName,
contextFullName,
entityTypeFullName,
propertyName);
}
}
builder
// End class declaration // End class declaration
.WriteClosingBracket() .WriteClosingBracket()
.WriteLine() .WriteLine()
@ -57,7 +80,7 @@ namespace MapTo.Sources
.WriteClosingBracket(); .WriteClosingBracket();
var generatedCode = builder.ToString(); var generatedCode = builder.ToString();
var hintName = $"{model.EntityTypeIdentifierName}Extensions.g.cs"; var hintName = $"{entityTypeName}Extensions.g.cs";
return new(generatedCode, hintName); return new(generatedCode, hintName);
} }
@ -74,13 +97,13 @@ namespace MapTo.Sources
return builder; return builder;
} }
private static SourceBuilder EfAddAddEntityMethod(this SourceBuilder builder, EfMethodsModel model, string addSourceTemplate, string entityTypeName, private static SourceBuilder EfAddAddEntityMethod(this SourceBuilder builder, EfAddMethodsModel model, string addSourceTemplate, string entityTypeName,
string contextFullName, string entityTypeFullName, string propertyName) string contextFullName, string entityTypeFullName, string propertyName)
{ {
var returnTypeFullName = model.AddMethodsModel.ReturnTypeFullName; var returnTypeFullName = model.ReturnTypeFullName;
var createTypeFullName = model.AddMethodsModel.CreateTypeFullName; var createTypeFullName = model.CreateTypeFullName;
var newEntityVarName = $"new{model.EntityTypeIdentifierName}"; var newEntityVarName = $"new{model.Entity.EntityTypeIdentifierName}";
var toCreateVarName = $"{model.EntityTypeIdentifierName.ToCamelCase()}ToCreate"; var toCreateVarName = $"{model.Entity.EntityTypeIdentifierName.ToCamelCase()}ToCreate";
if (!addSourceTemplate.IsEmpty()) if (!addSourceTemplate.IsEmpty())
{ {
@ -103,11 +126,11 @@ namespace MapTo.Sources
{ {
builder builder
.WriteLine(GeneratedFilesHeader) .WriteLine(GeneratedFilesHeader)
.WriteLine($"public static (bool, {model.AddMethodsModel.ReturnTypeFullName}) Add{entityTypeName}(") .WriteLine($"public static (bool, {model.ReturnTypeFullName}) Add{entityTypeName}(")
.WriteLine($"this {contextFullName} dbContext,") .WriteLine($"this {contextFullName} dbContext,")
.WriteLine($"{createTypeFullName} {toCreateVarName})") .WriteLine($"{createTypeFullName} {toCreateVarName})")
.WriteOpeningBracket() .WriteOpeningBracket()
.WriteLine($"var {newEntityVarName} = new {model.EntityTypeFullName}({toCreateVarName});") .WriteLine($"var {newEntityVarName} = new {model.Entity.EntityTypeFullName}({toCreateVarName});")
.WriteLine($"dbContext.{propertyName}.Add({newEntityVarName});") .WriteLine($"dbContext.{propertyName}.Add({newEntityVarName});")
.WriteLine($"var success = dbContext.SaveChanges() >= 0;") .WriteLine($"var success = dbContext.SaveChanges() >= 0;")
.WriteLine($"return (success, new {returnTypeFullName}({newEntityVarName}));") .WriteLine($"return (success, new {returnTypeFullName}({newEntityVarName}));")
@ -120,16 +143,16 @@ namespace MapTo.Sources
return builder; return builder;
} }
private static SourceBuilder EfAddUpdateEntityMethod(this SourceBuilder builder, EfMethodsModel model, string updateSourceTemplate, string entityTypeName, private static SourceBuilder EfAddUpdateEntityMethod(this SourceBuilder builder, EfUpdateMethodsModel model, string updateSourceTemplate, string entityTypeName,
string contextFullName, string entityTypeFullName, string propertyName) string contextFullName, string entityTypeFullName, string propertyName)
{ {
var returnTypeFullName = model.UpdateMethodsModel.ReturnTypeFullName; var returnTypeFullName = model.ReturnTypeFullName;
var updateTypeFullName = model.UpdateMethodsModel.UpdateTypeFullName; var updateTypeFullName = model.UpdateTypeFullName;
var updateVarName = $"{model.EntityTypeIdentifierName.ToCamelCase()}ToUpdate"; var updateVarName = $"{model.Entity.EntityTypeIdentifierName.ToCamelCase()}ToUpdate";
var keyPropertyName = model.UpdateMethodsModel.keyPropertyName; var keyPropertyName = model.keyPropertyName;
var keyTypeFullName = model.UpdateMethodsModel.keyFullTypeName; var keyTypeFullName = model.keyFullTypeName;
var existingVarName = entityTypeName.ToCamelCase(); var existingVarName = entityTypeName.ToCamelCase();
var keyVarName = entityTypeName.ToCamelCase() + model.UpdateMethodsModel.keyPropertyName; var keyVarName = entityTypeName.ToCamelCase() + model.keyPropertyName;
if (!updateSourceTemplate.IsEmpty()) if (!updateSourceTemplate.IsEmpty())
{ {
var templateToSourceBuilder = new StringBuilder(updateSourceTemplate); var templateToSourceBuilder = new StringBuilder(updateSourceTemplate);