diff --git a/src/BlueWest.MapTo/Sources/EfMethods/EfMethodsGenerator.cs b/src/BlueWest.MapTo/Sources/EfMethods/EfMethodsGenerator.cs index 0ab9f46..c90adeb 100644 --- a/src/BlueWest.MapTo/Sources/EfMethods/EfMethodsGenerator.cs +++ b/src/BlueWest.MapTo/Sources/EfMethods/EfMethodsGenerator.cs @@ -78,7 +78,8 @@ namespace MapTo private static string GetCsxMethodTemplate(GeneratorExecutionContext context, string templateName) { - return context.AdditionalFiles + return context + .AdditionalFiles .FirstOrDefault(x => x.Path.Contains(templateName))? .GetText()? .ToString() ?? string.Empty; diff --git a/src/BlueWest.MapTo/Sources/EfMethods/EfMethodsSource.cs b/src/BlueWest.MapTo/Sources/EfMethods/EfMethodsSource.cs index 54f4930..be58f3d 100644 --- a/src/BlueWest.MapTo/Sources/EfMethods/EfMethodsSource.cs +++ b/src/BlueWest.MapTo/Sources/EfMethods/EfMethodsSource.cs @@ -35,41 +35,24 @@ namespace MapTo.Sources .WriteOpeningBracket() .WriteLine(); - 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); + .EfAddAddEntityMethod(model,m, addSourceTemplate) + .WriteLine(); + continue; } 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); + .EfAddUpdateEntityMethod(model, updateModel, updateSourceTemplate) + .WriteLine(); + continue; } - builder.WriteLine(); - - } builder @@ -87,36 +70,38 @@ namespace MapTo.Sources private static SourceBuilder ParseTemplate(this SourceBuilder builder, string finalTemplate) { string[] array = finalTemplate.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); - - foreach (var line in array) - { + for (var i = 0; i < array.Length; i++) + { + var line = array[i]; builder.WriteLine(line); } - return builder; } - private static SourceBuilder EfAddAddEntityMethod(this SourceBuilder builder, EfAddMethodsModel model, string addSourceTemplate, string entityTypeName, - string contextFullName, string entityTypeFullName, string propertyName) + private static SourceBuilder EfAddAddEntityMethod(this SourceBuilder builder, EfMethodsModel methodsModel, EfAddMethodsModel model, string addSourceTemplate) { var returnTypeFullName = model.ReturnTypeFullName; var createTypeFullName = model.CreateTypeFullName; var newEntityVarName = $"new{model.Entity.EntityTypeIdentifierName}"; var toCreateVarName = $"{model.Entity.EntityTypeIdentifierName.ToCamelCase()}ToCreate"; + var entityTypeName = model.Entity.EntityTypeIdentifierName; + var contextFullName = methodsModel.ContextFullType; + var propertyName = model.Entity.PropertyName; + if (!addSourceTemplate.IsEmpty()) { var templateToSourceBuilder = new StringBuilder(addSourceTemplate); templateToSourceBuilder - .Replace("{entityTypeName}", entityTypeName) + .Replace("{entityTypeName}", model.Entity.EntityTypeIdentifierName) .Replace("{returnTypeFullName}", returnTypeFullName) .Replace("{createTypeFullName}", createTypeFullName) .Replace("{contextFullName}", contextFullName) .Replace("{toCreateVarName}", toCreateVarName) .Replace("{newEntityVarName}", newEntityVarName) - .Replace("{entityTypeFullName}", entityTypeFullName) + .Replace("{entityTypeFullName}", model.Entity.EntityTypeFullName) .Replace("{propertyName}", propertyName); builder @@ -145,12 +130,14 @@ namespace MapTo.Sources return builder; } - private static SourceBuilder EfAddUpdateEntityMethod(this SourceBuilder builder, EfUpdateMethodsModel model, string updateSourceTemplate, string entityTypeName, - string contextFullName, string entityTypeFullName, string propertyName) + private static SourceBuilder EfAddUpdateEntityMethod(this SourceBuilder builder, EfMethodsModel methodsModel, EfUpdateMethodsModel model, string updateSourceTemplate) { + var entityTypeName = model.Entity.EntityTypeIdentifierName; + var contextFullName = methodsModel.ContextFullType; + var propertyName = model.Entity.PropertyName; var returnTypeFullName = model.ReturnTypeFullName; var updateTypeFullName = model.UpdateTypeFullName; - var updateVarName = $"{model.Entity.EntityTypeIdentifierName.ToCamelCase()}ToUpdate"; + var updateVarName = $"{entityTypeName.ToCamelCase()}ToUpdate"; var keyPropertyName = model.keyPropertyName; var keyTypeFullName = model.keyFullTypeName; var existingVarName = entityTypeName.ToCamelCase();