ok state before introducing update

This commit is contained in:
CodeLiturgy 2022-08-28 19:39:47 +01:00
parent ca95c87839
commit f419c0f8ea
4 changed files with 85 additions and 109 deletions

View File

@ -94,7 +94,6 @@ namespace MapTo
return sourceSymbol;
}
// we need two possible InamedTypeSymbol
protected ImmutableArray<INamedTypeSymbol> GetEntityTypeSymbols(SyntaxNode? attributeSyntax, SemanticModel? semanticModel = null)
{
if (attributeSyntax is null)
@ -194,11 +193,7 @@ namespace MapTo
AddDiagnostic(DiagnosticsFactory.MapFromAttributeNotFoundError(MemberSyntax.GetLocation()));
return null;
}
//var typeIdentifierName = MemberSyntax.GetIdentifierName();
//var isTypeInheritFromMappedBaseClass = IsTypeInheritFromMappedBaseClass(semanticModel);
return new EfMethodsModel(
SourceGenerationOptions,
contextNamespace,
@ -218,27 +213,25 @@ namespace MapTo
string createTypeIdentifierName = entityTypeName;
string createTypeFullName = entityTypeFullName;
string readTypeIdentifierName = entityTypeName;
string readTypeFullName = entityTypeFullName;
string returnTypeIdentifierName = entityTypeName;
string returnTypeFullName = 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();
returnTypeIdentifierName = efAddAttributeTypeSymbols[1].Name;
returnTypeFullName = efAddAttributeTypeSymbols[1].ToDisplayString();
}
return new EfAddMethodsModel(createTypeFullName, createTypeIdentifierName, readTypeFullName,
readTypeIdentifierName);
return new EfAddMethodsModel(createTypeFullName, createTypeIdentifierName, returnTypeFullName,
returnTypeIdentifierName);
}
private static ITypeSymbol GetEntityTypeData(MemberDeclarationSyntax memberDeclarationSyntax, SemanticModel? semanticModel = null)

View File

@ -33,7 +33,6 @@ namespace MapTo
var compilation = context.Compilation
.AddSource(ref context, UseUpdateAttributeSource.Generate(options))
.AddSource(ref context, AddDataAttributeSource.Generate(options))
.AddSource(ref context, JsonExtensionAttributeSource.Generate(options))
.AddSource(ref context, MapFromAttributeSource.Generate(options))
.AddSource(ref context, IgnoreMemberAttributeSource.Generate(options))

View File

@ -1,66 +0,0 @@
using static MapTo.Sources.Constants;
namespace MapTo.Sources
{
public class AddDataAttributeSource
{
internal const string AttributeName = "AddDataGenerator";
internal const string AttributeClassName = AttributeName + "Attribute";
internal const string FullyQualifiedName = RootNamespace + "." + AttributeClassName;
internal static SourceCode Generate(SourceGenerationOptions options)
{
using var builder = new SourceBuilder()
.WriteLine(GeneratedFilesHeader)
.WriteLine("using System;")
.WriteLine()
.WriteLine($"namespace {RootNamespace}")
.WriteOpeningBracket();
if (options.GenerateXmlDocument)
{
builder
.WriteLine("/// <summary>")
.WriteLine("/// Generates CRUD functions to be used with the specified database context and entity.")
.WriteLine("/// </summary>");
}
builder
.WriteLine("[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]")
.WriteLine($"public sealed class {AttributeName}Attribute : Attribute")
.WriteOpeningBracket();
if (options.GenerateXmlDocument)
{
builder
.WriteLine("/// <summary>")
.WriteLine($"/// Initializes a new instance of the <see cref=\"{AttributeName}Attribute\"/> class with the specified <paramref name=\"sourceType\"/>.")
.WriteLine("/// </summary>")
.WriteLine("/// <param name=\"sourceType\">The type of to map from.</param>");
}
builder
.WriteLine($"public {AttributeName}Attribute(Type databaseContextType)")
.WriteOpeningBracket()
.WriteLine("DatabaseContextType = databaseContextType;")
.WriteClosingBracket()
.WriteLine();
if (options.GenerateXmlDocument)
{
builder
.WriteLine("/// <summary>")
.WriteLine("/// Gets the type to map from.")
.WriteLine("/// </summary>");
}
builder
.WriteLine("public Type DatabaseContextType { get; }")
.WriteClosingBracket() // class
.WriteClosingBracket(); // namespace
return new(builder.ToString(), $"{AttributeName}Attribute.g.cs");
}
}
}

View File

@ -9,24 +9,15 @@ namespace MapTo.Sources
{
internal static class EfMethodsSource
{
static IEnumerable<string> GetFilesFromDir(string dir) =>
Directory.EnumerateFiles(dir);
internal static SourceCode Generate(EfMethodsModel model, string addSourceTemplate)
{
using var builder = new SourceBuilder();
var entityTypeName = model.EntityTypeIdentifierName;
var entityTypeFullName = model.EntityTypeFullName;
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}";
var toCreateVarName = $"{model.EntityTypeIdentifierName.ToCamelCase()}ToCreate";
builder
.WriteUsings(model.Usings)
.WriteLine("using Microsoft.EntityFrameworkCore;")
@ -38,13 +29,36 @@ namespace MapTo.Sources
.WriteLine($"public static partial class {model.ContextTypeName}Extensions")
.WriteOpeningBracket()
.WriteLine();
builder
.EfAddAddEntityMethod(model, addSourceTemplate,
entityTypeName,
contextFullName,
entityTypeFullName,
propertyName)
// End class declaration
.WriteClosingBracket()
.WriteLine()
// End namespace declaration
.WriteClosingBracket();
var generatedCode = builder.ToString();
var hintName = $"{model.EntityTypeIdentifierName}Extensions.g.cs";
return new(generatedCode, hintName);
}
private static SourceBuilder EfAddAddEntityMethod(this SourceBuilder builder, EfMethodsModel 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";
if (!addSourceTemplate.IsEmpty())
{
var templateToSourceBuilder = new StringBuilder(addSourceTemplate);
templateToSourceBuilder
.Replace("{entityTypeName}", entityTypeName)
.Replace("{returnTypeFullName}", returnTypeFullName)
.Replace("{createTypeFullName}", createTypeFullName)
@ -56,14 +70,12 @@ namespace MapTo.Sources
builder
.Write(templateToSourceBuilder.ToString());
}
if (addSourceTemplate.IsEmpty())
{
builder
.WriteLine(GeneratedFilesHeader)
.WriteLine(GeneratedFilesHeader)
.WriteLine($"public static (bool, {model.AddMethodsModel.ReturnTypeFullName}) Add{entityTypeName}(")
.WriteLine($"this {contextFullName} dbContext,")
.WriteLine($"{createTypeFullName} {toCreateVarName})")
@ -73,19 +85,57 @@ namespace MapTo.Sources
.WriteLine($"var success = dbContext.SaveChanges() >= 0;")
.WriteLine($"return (success, new {returnTypeFullName}({newEntityVarName}));")
.WriteClosingBracket();
builder
.WriteLine()
// End class declaration
.WriteClosingBracket()
.WriteLine()
// End namespace declaration
.WriteClosingBracket();
builder
.WriteLine();
}
var generatedCode = builder.ToString();
var hintName = $"{model.EntityTypeIdentifierName}Extensions.g.cs";
return new(generatedCode, hintName);
return builder;
}
private static SourceBuilder EfAddUpdateEntityMethod(this SourceBuilder builder, EfMethodsModel model, string addSourceTemplate, string entityTypeName,
string contextFullName, string entityTypeFullName, string propertyName)
{
var returnTypeFullName = model.AddMethodsModel.ReturnTypeFullName;
var updateTypeFullName = model.AddMethodsModel.CreateTypeFullName;
var newEntityVarName = $"new{model.EntityTypeIdentifierName}";
var toCreateVarName = $"{model.EntityTypeIdentifierName.ToCamelCase()}ToCreate";
if (!addSourceTemplate.IsEmpty())
{
var templateToSourceBuilder = new StringBuilder(addSourceTemplate);
templateToSourceBuilder
.Replace("{entityTypeName}", entityTypeName)
.Replace("{returnTypeFullName}", returnTypeFullName)
.Replace("{updateTypeFullName}", updateTypeFullName)
.Replace("{contextFullName}", contextFullName)
.Replace("{propertyName}", propertyName);
builder
.Write(templateToSourceBuilder.ToString());
}
if (addSourceTemplate.IsEmpty())
{
builder
.WriteLine(GeneratedFilesHeader)
.WriteLine($"public static (bool, {model.AddMethodsModel.ReturnTypeFullName}) Add{entityTypeName}(")
.WriteLine($"this {contextFullName} dbContext,")
.WriteLine($"{updateTypeFullName} {toCreateVarName})")
.WriteOpeningBracket()
.WriteLine($"var {newEntityVarName} = new {model.EntityTypeFullName}({toCreateVarName});")
.WriteLine($"dbContext.{propertyName}.Add({newEntityVarName});")
.WriteLine($"var success = dbContext.SaveChanges() >= 0;")
.WriteLine($"return (success, new {returnTypeFullName}({newEntityVarName}));")
.WriteClosingBracket();
builder
.WriteLine();
}
return builder;
}
}
}