Use new model for ef method
This commit is contained in:
parent
3917d231a2
commit
9211cde83c
|
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -68,39 +68,44 @@ namespace MapTo
|
|||
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(
|
||||
|
||||
EfEntityDataModel Entity,
|
||||
string CreateTypeFullName,
|
||||
string CreateTypeIdentifierName,
|
||||
string ReturnTypeFullName,
|
||||
string ReturnTypeIdentifierName
|
||||
) : IEfMethodsConfiguration;
|
||||
) : IEfMethodsModel;
|
||||
internal record EfUpdateMethodsModel(
|
||||
|
||||
EfEntityDataModel Entity,
|
||||
string UpdateTypeFullName,
|
||||
string UpdateTypeIdentifierName,
|
||||
string ReturnTypeFullName,
|
||||
string ReturnTypeIdentifierName,
|
||||
string keyPropertyName,
|
||||
string keyFullTypeName
|
||||
): IEfMethodsConfiguration;
|
||||
|
||||
|
||||
internal record EfMethodsModel(
|
||||
SourceGenerationOptions Options,
|
||||
string Namespace,
|
||||
string PropertyName,
|
||||
string ContextTypeName,
|
||||
string ContextFullType,
|
||||
string EntityTypeFullName,
|
||||
string EntityTypeIdentifierName,
|
||||
EfAddMethodsModel AddMethodsModel,
|
||||
EfUpdateMethodsModel UpdateMethodsModel,
|
||||
ImmutableArray<string> Usings
|
||||
);
|
||||
|
||||
): IEfMethodsModel;
|
||||
|
||||
|
||||
internal record SourceGenerationOptions(
|
||||
AccessModifier ConstructorAccessModifier,
|
||||
AccessModifier GeneratedMethodsAccessModifier,
|
||||
|
|
|
@ -272,8 +272,10 @@ namespace MapTo
|
|||
propertyName = fieldDeclaration.ToString();
|
||||
}
|
||||
|
||||
var efEntity = new EfEntityDataModel(propertyName, entityTypeFullName, entityTypeName);
|
||||
|
||||
var addConfig = ExtractEfAddMethodsModel(semanticModel, entityTypeName, entityTypeFullName);
|
||||
|
||||
var addConfig = ExtractEfAddMethodsModel(semanticModel, efEntity);
|
||||
|
||||
if (addConfig == null)
|
||||
{
|
||||
|
@ -281,7 +283,7 @@ namespace MapTo
|
|||
return null;
|
||||
}
|
||||
|
||||
var updateConfig = ExtractEfUpdateMethodsModel(semanticModel, entityTypeName, entityTypeFullName);
|
||||
var updateConfig = ExtractEfUpdateMethodsModel(semanticModel, efEntity);
|
||||
|
||||
if (updateConfig == null)
|
||||
{
|
||||
|
@ -289,30 +291,31 @@ namespace MapTo
|
|||
return null;
|
||||
}
|
||||
|
||||
// Create list of configurations
|
||||
|
||||
List<IEfMethodsModel> models = new List<IEfMethodsModel>();
|
||||
|
||||
models.Add(addConfig);
|
||||
models.Add(updateConfig);
|
||||
|
||||
|
||||
return new EfMethodsModel(
|
||||
SourceGenerationOptions,
|
||||
contextNamespace,
|
||||
propertyName,
|
||||
dbContextName,
|
||||
contextTypeFullName,
|
||||
entityTypeFullName,
|
||||
entityTypeName,
|
||||
addConfig,
|
||||
updateConfig,
|
||||
dbContextName, contextTypeFullName,
|
||||
models.ToImmutableArray(),
|
||||
Usings);
|
||||
}
|
||||
|
||||
private EfAddMethodsModel ExtractEfAddMethodsModel(SemanticModel semanticModel, string entityTypeName, string entityTypeFullName)
|
||||
private EfAddMethodsModel ExtractEfAddMethodsModel(SemanticModel semanticModel, EfEntityDataModel efEntityDataModel)
|
||||
{
|
||||
var efAddAttributeTypeSymbols =
|
||||
GetEntityTypeSymbol(MemberSyntax, EfAddMethodsAttributeSource.AttributeName, semanticModel);
|
||||
|
||||
string createTypeIdentifierName = entityTypeName;
|
||||
string createTypeFullName = entityTypeFullName;
|
||||
string returnTypeIdentifierName = entityTypeName;
|
||||
string returnTypeFullName = entityTypeFullName;
|
||||
string createTypeIdentifierName = efEntityDataModel.EntityTypeIdentifierName;
|
||||
string createTypeFullName = efEntityDataModel.EntityTypeFullName;
|
||||
string returnTypeIdentifierName = efEntityDataModel.EntityTypeIdentifierName;
|
||||
string returnTypeFullName = efEntityDataModel.EntityTypeFullName;
|
||||
|
||||
|
||||
if (efAddAttributeTypeSymbols.Length > 0)
|
||||
|
@ -328,11 +331,11 @@ namespace MapTo
|
|||
}
|
||||
|
||||
|
||||
return new EfAddMethodsModel(createTypeFullName, createTypeIdentifierName, returnTypeFullName,
|
||||
return new EfAddMethodsModel(efEntityDataModel, createTypeFullName, createTypeIdentifierName, returnTypeFullName,
|
||||
returnTypeIdentifierName);
|
||||
}
|
||||
|
||||
private EfUpdateMethodsModel ExtractEfUpdateMethodsModel(SemanticModel semanticModel, string entityTypeName, string entityTypeFullName)
|
||||
private EfUpdateMethodsModel ExtractEfUpdateMethodsModel(SemanticModel semanticModel,EfEntityDataModel efEntityDataModel)
|
||||
{
|
||||
var efAddAttributeTypeSymbols =
|
||||
GetEntityTypeSymbol(MemberSyntax, EfUpdateMethodsAttributeSource.AttributeName, semanticModel);
|
||||
|
@ -342,6 +345,7 @@ namespace MapTo
|
|||
var keyMemberType = FindFirstTypeInfoOfMemberExpressionSyntax(semanticModel, MemberSyntax);
|
||||
|
||||
var secondTypeInfo = FindSecondTypeInfoOfMemberExpressionSyntax(semanticModel, MemberSyntax);
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -358,10 +362,10 @@ namespace MapTo
|
|||
|
||||
if (efAddAttributeTypeSymbols == null) return null;
|
||||
|
||||
string updateTypeIdentifierName = entityTypeName;
|
||||
string updateTypeFullName = entityTypeFullName;
|
||||
string returnTypeIdentifierName = entityTypeName;
|
||||
string returnTypeFullName = entityTypeFullName;
|
||||
string updateTypeIdentifierName = efEntityDataModel.EntityTypeIdentifierName;
|
||||
string updateTypeFullName = efEntityDataModel.EntityTypeFullName;
|
||||
string returnTypeIdentifierName = efEntityDataModel.EntityTypeIdentifierName;
|
||||
string returnTypeFullName = efEntityDataModel.EntityTypeFullName;
|
||||
|
||||
if (efAddAttributeTypeSymbols.Length > 0)
|
||||
{
|
||||
|
@ -377,8 +381,7 @@ namespace MapTo
|
|||
}
|
||||
|
||||
|
||||
|
||||
return new EfUpdateMethodsModel(updateTypeFullName, updateTypeIdentifierName, returnTypeFullName,
|
||||
return new EfUpdateMethodsModel(efEntityDataModel, updateTypeFullName, updateTypeIdentifierName, returnTypeFullName,
|
||||
returnTypeIdentifierName, keyPropertyName, keyMemberFullName);
|
||||
}
|
||||
|
||||
|
|
|
@ -15,10 +15,8 @@ namespace MapTo.Sources
|
|||
{
|
||||
using var builder = new SourceBuilder();
|
||||
|
||||
var entityTypeName = model.EntityTypeIdentifierName;
|
||||
var entityTypeFullName = model.EntityTypeFullName;
|
||||
|
||||
var contextFullName = model.ContextFullType;
|
||||
var propertyName = model.PropertyName;
|
||||
|
||||
builder
|
||||
.WriteLine(GeneratedFilesHeader)
|
||||
|
@ -37,19 +35,44 @@ namespace MapTo.Sources
|
|||
.WriteOpeningBracket()
|
||||
.WriteLine();
|
||||
|
||||
builder
|
||||
.EfAddAddEntityMethod(model, addSourceTemplate,
|
||||
entityTypeName,
|
||||
contextFullName,
|
||||
entityTypeFullName,
|
||||
propertyName)
|
||||
.WriteLine()
|
||||
.EfAddUpdateEntityMethod(model, updateSourceTemplate,
|
||||
entityTypeName,
|
||||
contextFullName,
|
||||
entityTypeFullName,
|
||||
propertyName)
|
||||
var entityTypeName = "";
|
||||
foreach (var methodModel in model.MethodsModels)
|
||||
{
|
||||
if (methodModel is EfAddMethodsModel m)
|
||||
{
|
||||
entityTypeName = m.Entity.EntityTypeIdentifierName;
|
||||
var entityTypeFullName = m.Entity.EntityTypeFullName;
|
||||
var propertyName = m.Entity.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
|
||||
.WriteClosingBracket()
|
||||
.WriteLine()
|
||||
|
@ -57,7 +80,7 @@ namespace MapTo.Sources
|
|||
.WriteClosingBracket();
|
||||
|
||||
var generatedCode = builder.ToString();
|
||||
var hintName = $"{model.EntityTypeIdentifierName}Extensions.g.cs";
|
||||
var hintName = $"{entityTypeName}Extensions.g.cs";
|
||||
return new(generatedCode, hintName);
|
||||
}
|
||||
|
||||
|
@ -74,13 +97,13 @@ namespace MapTo.Sources
|
|||
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)
|
||||
{
|
||||
var returnTypeFullName = model.AddMethodsModel.ReturnTypeFullName;
|
||||
var createTypeFullName = model.AddMethodsModel.CreateTypeFullName;
|
||||
var newEntityVarName = $"new{model.EntityTypeIdentifierName}";
|
||||
var toCreateVarName = $"{model.EntityTypeIdentifierName.ToCamelCase()}ToCreate";
|
||||
var returnTypeFullName = model.ReturnTypeFullName;
|
||||
var createTypeFullName = model.CreateTypeFullName;
|
||||
var newEntityVarName = $"new{model.Entity.EntityTypeIdentifierName}";
|
||||
var toCreateVarName = $"{model.Entity.EntityTypeIdentifierName.ToCamelCase()}ToCreate";
|
||||
|
||||
if (!addSourceTemplate.IsEmpty())
|
||||
{
|
||||
|
@ -103,11 +126,11 @@ namespace MapTo.Sources
|
|||
{
|
||||
builder
|
||||
.WriteLine(GeneratedFilesHeader)
|
||||
.WriteLine($"public static (bool, {model.AddMethodsModel.ReturnTypeFullName}) Add{entityTypeName}(")
|
||||
.WriteLine($"public static (bool, {model.ReturnTypeFullName}) Add{entityTypeName}(")
|
||||
.WriteLine($"this {contextFullName} dbContext,")
|
||||
.WriteLine($"{createTypeFullName} {toCreateVarName})")
|
||||
.WriteOpeningBracket()
|
||||
.WriteLine($"var {newEntityVarName} = new {model.EntityTypeFullName}({toCreateVarName});")
|
||||
.WriteLine($"var {newEntityVarName} = new {model.Entity.EntityTypeFullName}({toCreateVarName});")
|
||||
.WriteLine($"dbContext.{propertyName}.Add({newEntityVarName});")
|
||||
.WriteLine($"var success = dbContext.SaveChanges() >= 0;")
|
||||
.WriteLine($"return (success, new {returnTypeFullName}({newEntityVarName}));")
|
||||
|
@ -120,16 +143,16 @@ namespace MapTo.Sources
|
|||
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)
|
||||
{
|
||||
var returnTypeFullName = model.UpdateMethodsModel.ReturnTypeFullName;
|
||||
var updateTypeFullName = model.UpdateMethodsModel.UpdateTypeFullName;
|
||||
var updateVarName = $"{model.EntityTypeIdentifierName.ToCamelCase()}ToUpdate";
|
||||
var keyPropertyName = model.UpdateMethodsModel.keyPropertyName;
|
||||
var keyTypeFullName = model.UpdateMethodsModel.keyFullTypeName;
|
||||
var returnTypeFullName = model.ReturnTypeFullName;
|
||||
var updateTypeFullName = model.UpdateTypeFullName;
|
||||
var updateVarName = $"{model.Entity.EntityTypeIdentifierName.ToCamelCase()}ToUpdate";
|
||||
var keyPropertyName = model.keyPropertyName;
|
||||
var keyTypeFullName = model.keyFullTypeName;
|
||||
var existingVarName = entityTypeName.ToCamelCase();
|
||||
var keyVarName = entityTypeName.ToCamelCase() + model.UpdateMethodsModel.keyPropertyName;
|
||||
var keyVarName = entityTypeName.ToCamelCase() + model.keyPropertyName;
|
||||
if (!updateSourceTemplate.IsEmpty())
|
||||
{
|
||||
var templateToSourceBuilder = new StringBuilder(updateSourceTemplate);
|
||||
|
|
Loading…
Reference in New Issue