Add model refactored
This commit is contained in:
parent
12d5839f16
commit
ca95c87839
|
@ -156,31 +156,14 @@ namespace MapTo
|
|||
return null;
|
||||
}*/
|
||||
|
||||
var efAddAttributeTypeSymbols = GetEntityTypeSymbol(MemberSyntax, EfAddMethodsAttributeSource.AttributeName, semanticModel);
|
||||
|
||||
// Main Entity
|
||||
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
|
||||
ClassDeclarationSyntax classDeclarationSyntax = MemberSyntax.Parent as ClassDeclarationSyntax;
|
||||
|
||||
|
@ -202,15 +185,16 @@ namespace MapTo
|
|||
// TODO: Test
|
||||
propertyName = fieldDeclaration.ToString();
|
||||
}
|
||||
|
||||
|
||||
if (efAddAttributeTypeSymbols.IsDefaultOrEmpty)
|
||||
|
||||
|
||||
var addConfig = ExtractEfAddMethodsModel(semanticModel, entityTypeName, entityTypeFullName);
|
||||
|
||||
if (addConfig == null)
|
||||
{
|
||||
AddDiagnostic(DiagnosticsFactory.MapFromAttributeNotFoundError(MemberSyntax.GetLocation()));
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//var typeIdentifierName = MemberSyntax.GetIdentifierName();
|
||||
//var isTypeInheritFromMappedBaseClass = IsTypeInheritFromMappedBaseClass(semanticModel);
|
||||
|
||||
|
@ -222,14 +206,41 @@ namespace MapTo
|
|||
dbContextName,
|
||||
contextTypeFullName,
|
||||
entityTypeFullName,
|
||||
entityTypeName,
|
||||
createTypeFullName,
|
||||
createTypeIdentifierName,
|
||||
readTypeFullName,
|
||||
readTypeIdentifierName,
|
||||
entityTypeName,
|
||||
addConfig,
|
||||
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)
|
||||
{
|
||||
var member = (memberDeclarationSyntax as PropertyDeclarationSyntax).Type;
|
||||
|
|
|
@ -68,6 +68,23 @@ namespace MapTo
|
|||
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(
|
||||
SourceGenerationOptions Options,
|
||||
|
@ -75,16 +92,9 @@ namespace MapTo
|
|||
string PropertyName,
|
||||
string ContextTypeName,
|
||||
string ContextFullType,
|
||||
|
||||
string EntityTypeFullName,
|
||||
string EntityTypeIdentifierName,
|
||||
|
||||
string CreateTypeFullName,
|
||||
string CreateTypeIdentifierName,
|
||||
|
||||
string ReadTypeFullName,
|
||||
string ReadTypeIdentifierName,
|
||||
|
||||
EfAddMethodsModel AddMethodsModel,
|
||||
ImmutableArray<string> Usings
|
||||
);
|
||||
|
||||
|
|
|
@ -18,10 +18,10 @@ namespace MapTo.Sources
|
|||
|
||||
var entityTypeName = model.EntityTypeIdentifierName;
|
||||
var entityTypeFullName = model.EntityTypeFullName;
|
||||
var readTypeName = model.ReadTypeIdentifierName;
|
||||
var readTypeFullName = model.ReadTypeFullName;
|
||||
var createTypeName = model.CreateTypeIdentifierName;
|
||||
var createTypeFullName = model.CreateTypeFullName;
|
||||
var returnTypeName = model.AddMethodsModel.ReturnTypeIdentifierName;
|
||||
var returnTypeFullName = model.AddMethodsModel.ReturnTypeFullName;
|
||||
var createTypeName = model.AddMethodsModel.CreateTypeIdentifierName;
|
||||
var createTypeFullName = model.AddMethodsModel.CreateTypeFullName;
|
||||
var contextFullName = model.ContextFullType;
|
||||
var propertyName = model.PropertyName;
|
||||
var newEntityVarName = $"new{model.EntityTypeIdentifierName}";
|
||||
|
@ -46,7 +46,7 @@ namespace MapTo.Sources
|
|||
templateToSourceBuilder
|
||||
|
||||
.Replace("{entityTypeName}", entityTypeName)
|
||||
.Replace("{readTypeFullName}", readTypeFullName)
|
||||
.Replace("{returnTypeFullName}", returnTypeFullName)
|
||||
.Replace("{createTypeFullName}", createTypeFullName)
|
||||
.Replace("{contextFullName}", contextFullName)
|
||||
.Replace("{toCreateVarName}", toCreateVarName)
|
||||
|
@ -64,14 +64,14 @@ namespace MapTo.Sources
|
|||
builder
|
||||
.WriteLine(GeneratedFilesHeader)
|
||||
|
||||
.WriteLine($"public static (bool, {model.ReadTypeFullName}) Add{entityTypeName}(")
|
||||
.WriteLine($"public static (bool, {model.AddMethodsModel.ReturnTypeFullName}) Add{entityTypeName}(")
|
||||
.WriteLine($"this {contextFullName} dbContext,")
|
||||
.WriteLine($"{createTypeFullName} {toCreateVarName})")
|
||||
.WriteOpeningBracket()
|
||||
.WriteLine($"var {newEntityVarName} = new {model.EntityTypeFullName}({toCreateVarName});")
|
||||
.WriteLine($"dbContext.{propertyName}.Add({newEntityVarName});")
|
||||
.WriteLine($"var success = dbContext.SaveChanges() >= 0;")
|
||||
.WriteLine($"return (success, new {readTypeFullName}({newEntityVarName}));")
|
||||
.WriteLine($"return (success, new {returnTypeFullName}({newEntityVarName}));")
|
||||
.WriteClosingBracket();
|
||||
builder
|
||||
.WriteLine()
|
||||
|
|
Loading…
Reference in New Issue