Add model refactored

This commit is contained in:
CodeLiturgy 2022-08-28 18:14:08 +01:00
parent 12d5839f16
commit ca95c87839
3 changed files with 66 additions and 45 deletions

View File

@ -156,31 +156,14 @@ namespace MapTo
return null; return null;
}*/ }*/
var efAddAttributeTypeSymbols = GetEntityTypeSymbol(MemberSyntax, EfAddMethodsAttributeSource.AttributeName, semanticModel);
// Main Entity // Main Entity
var entityData = GetEntityTypeData(MemberSyntax, semanticModel); var entityData = GetEntityTypeData(MemberSyntax, semanticModel);
string entityTypeName = entityData.Name, createTypeIdentifierName = entityData.Name, readTypeIdentifierName = entityData.Name;
string entityTypeFullName = entityData.ToDisplayString(), createTypeFullName = entityData.ToDisplayString(), readTypeFullName = entityData.ToDisplayString();
string entityTypeName = entityData.Name;
string entityTypeFullName = entityData.ToDisplayString();
if (efAddAttributeTypeSymbols.Length > 0)
{
// Create DTO
createTypeIdentifierName = efAddAttributeTypeSymbols[0].Name;
createTypeFullName = efAddAttributeTypeSymbols[0].ToDisplayString();
}
if (efAddAttributeTypeSymbols.Length > 1)
{
// Read DTO
readTypeIdentifierName = efAddAttributeTypeSymbols[1].Name;
readTypeFullName = efAddAttributeTypeSymbols[1].ToDisplayString();
}
// get containing class type information // get containing class type information
ClassDeclarationSyntax classDeclarationSyntax = MemberSyntax.Parent as ClassDeclarationSyntax; ClassDeclarationSyntax classDeclarationSyntax = MemberSyntax.Parent as ClassDeclarationSyntax;
@ -202,15 +185,16 @@ namespace MapTo
// TODO: Test // TODO: Test
propertyName = fieldDeclaration.ToString(); propertyName = fieldDeclaration.ToString();
} }
if (efAddAttributeTypeSymbols.IsDefaultOrEmpty) var addConfig = ExtractEfAddMethodsModel(semanticModel, entityTypeName, entityTypeFullName);
if (addConfig == null)
{ {
AddDiagnostic(DiagnosticsFactory.MapFromAttributeNotFoundError(MemberSyntax.GetLocation())); AddDiagnostic(DiagnosticsFactory.MapFromAttributeNotFoundError(MemberSyntax.GetLocation()));
return null; return null;
} }
//var typeIdentifierName = MemberSyntax.GetIdentifierName(); //var typeIdentifierName = MemberSyntax.GetIdentifierName();
//var isTypeInheritFromMappedBaseClass = IsTypeInheritFromMappedBaseClass(semanticModel); //var isTypeInheritFromMappedBaseClass = IsTypeInheritFromMappedBaseClass(semanticModel);
@ -222,14 +206,41 @@ namespace MapTo
dbContextName, dbContextName,
contextTypeFullName, contextTypeFullName,
entityTypeFullName, entityTypeFullName,
entityTypeName, entityTypeName,
createTypeFullName, addConfig,
createTypeIdentifierName,
readTypeFullName,
readTypeIdentifierName,
Usings); Usings);
} }
private EfAddMethodsModel ExtractEfAddMethodsModel(SemanticModel semanticModel, string entityTypeName, string entityTypeFullName)
{
var efAddAttributeTypeSymbols =
GetEntityTypeSymbol(MemberSyntax, EfAddMethodsAttributeSource.AttributeName, semanticModel);
string createTypeIdentifierName = entityTypeName;
string createTypeFullName = entityTypeFullName;
string readTypeIdentifierName = entityTypeName;
string readTypeFullName = entityTypeFullName;
if (efAddAttributeTypeSymbols.Length > 0)
{
// Create DTO
createTypeIdentifierName = efAddAttributeTypeSymbols[0].Name;
createTypeFullName = efAddAttributeTypeSymbols[0].ToDisplayString();
}
if (efAddAttributeTypeSymbols.Length > 1)
{
// Read DTO
readTypeIdentifierName = efAddAttributeTypeSymbols[1].Name;
readTypeFullName = efAddAttributeTypeSymbols[1].ToDisplayString();
}
return new EfAddMethodsModel(createTypeFullName, createTypeIdentifierName, readTypeFullName,
readTypeIdentifierName);
}
private static ITypeSymbol GetEntityTypeData(MemberDeclarationSyntax memberDeclarationSyntax, SemanticModel? semanticModel = null) private static ITypeSymbol GetEntityTypeData(MemberDeclarationSyntax memberDeclarationSyntax, SemanticModel? semanticModel = null)
{ {
var member = (memberDeclarationSyntax as PropertyDeclarationSyntax).Type; var member = (memberDeclarationSyntax as PropertyDeclarationSyntax).Type;

View File

@ -68,6 +68,23 @@ namespace MapTo
ImmutableArray<string> Usings ImmutableArray<string> Usings
); );
internal interface IEfMethodsConfiguration {}
internal record EfAddMethodsModel(
string CreateTypeFullName,
string CreateTypeIdentifierName,
string ReturnTypeFullName,
string ReturnTypeIdentifierName
) : IEfMethodsConfiguration;
internal record EfUpdateMethodsModel(
string CreateTypeFullName,
string CreateTypeIdentifierName,
string ReturnTypeFullName,
string ReturnTypeIdentifierName
): IEfMethodsConfiguration;
internal record EfMethodsModel( internal record EfMethodsModel(
SourceGenerationOptions Options, SourceGenerationOptions Options,
@ -75,16 +92,9 @@ namespace MapTo
string PropertyName, string PropertyName,
string ContextTypeName, string ContextTypeName,
string ContextFullType, string ContextFullType,
string EntityTypeFullName, string EntityTypeFullName,
string EntityTypeIdentifierName, string EntityTypeIdentifierName,
EfAddMethodsModel AddMethodsModel,
string CreateTypeFullName,
string CreateTypeIdentifierName,
string ReadTypeFullName,
string ReadTypeIdentifierName,
ImmutableArray<string> Usings ImmutableArray<string> Usings
); );

View File

@ -18,10 +18,10 @@ namespace MapTo.Sources
var entityTypeName = model.EntityTypeIdentifierName; var entityTypeName = model.EntityTypeIdentifierName;
var entityTypeFullName = model.EntityTypeFullName; var entityTypeFullName = model.EntityTypeFullName;
var readTypeName = model.ReadTypeIdentifierName; var returnTypeName = model.AddMethodsModel.ReturnTypeIdentifierName;
var readTypeFullName = model.ReadTypeFullName; var returnTypeFullName = model.AddMethodsModel.ReturnTypeFullName;
var createTypeName = model.CreateTypeIdentifierName; var createTypeName = model.AddMethodsModel.CreateTypeIdentifierName;
var createTypeFullName = model.CreateTypeFullName; var createTypeFullName = model.AddMethodsModel.CreateTypeFullName;
var contextFullName = model.ContextFullType; var contextFullName = model.ContextFullType;
var propertyName = model.PropertyName; var propertyName = model.PropertyName;
var newEntityVarName = $"new{model.EntityTypeIdentifierName}"; var newEntityVarName = $"new{model.EntityTypeIdentifierName}";
@ -46,7 +46,7 @@ namespace MapTo.Sources
templateToSourceBuilder templateToSourceBuilder
.Replace("{entityTypeName}", entityTypeName) .Replace("{entityTypeName}", entityTypeName)
.Replace("{readTypeFullName}", readTypeFullName) .Replace("{returnTypeFullName}", returnTypeFullName)
.Replace("{createTypeFullName}", createTypeFullName) .Replace("{createTypeFullName}", createTypeFullName)
.Replace("{contextFullName}", contextFullName) .Replace("{contextFullName}", contextFullName)
.Replace("{toCreateVarName}", toCreateVarName) .Replace("{toCreateVarName}", toCreateVarName)
@ -64,14 +64,14 @@ namespace MapTo.Sources
builder builder
.WriteLine(GeneratedFilesHeader) .WriteLine(GeneratedFilesHeader)
.WriteLine($"public static (bool, {model.ReadTypeFullName}) Add{entityTypeName}(") .WriteLine($"public static (bool, {model.AddMethodsModel.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.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 {readTypeFullName}({newEntityVarName}));") .WriteLine($"return (success, new {returnTypeFullName}({newEntityVarName}));")
.WriteClosingBracket(); .WriteClosingBracket();
builder builder
.WriteLine() .WriteLine()