Inject Update method
This commit is contained in:
parent
5af2be8eef
commit
93e9d91ffd
|
@ -44,4 +44,8 @@
|
|||
<None Include="MapTo.props" Pack="true" PackagePath="build" Visible="false" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="bin\Release\netstandard2.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
@ -33,6 +33,7 @@ namespace MapTo.Sources
|
|||
.GeneratePrivateConstructor(model)
|
||||
.WriteLine()
|
||||
.GenerateFactoryMethod(model)
|
||||
.GenerateUpdateMethod(model)
|
||||
|
||||
// End class declaration
|
||||
.WriteClosingBracket()
|
||||
|
@ -80,15 +81,25 @@ namespace MapTo.Sources
|
|||
.WriteLine($"if ({sourceClassParameterName} == null) throw new ArgumentNullException(nameof({sourceClassParameterName}));")
|
||||
.WriteLine()
|
||||
.WriteLine($"{mappingContextParameterName}.{MappingContextSource.RegisterMethodName}({sourceClassParameterName}, this);")
|
||||
.WriteLine();
|
||||
.WriteLine().
|
||||
|
||||
WriteProperties( model, sourceClassParameterName, mappingContextParameterName);
|
||||
|
||||
// End constructor declaration
|
||||
return builder.WriteClosingBracket();
|
||||
}
|
||||
|
||||
private static SourceBuilder WriteProperties(this SourceBuilder builder, MappingModel model,
|
||||
string? sourceClassParameterName, string mappingContextParameterName)
|
||||
{
|
||||
foreach (var property in model.MappedProperties)
|
||||
{
|
||||
if (property.TypeConverter is null)
|
||||
{
|
||||
if (property.IsEnumerable)
|
||||
{
|
||||
builder.WriteLine($"{property.Name} = {sourceClassParameterName}.{property.SourcePropertyName}.Select({mappingContextParameterName}.{MappingContextSource.MapMethodName}<{property.MappedSourcePropertyTypeName}, {property.EnumerableTypeArgument}>).ToList();");
|
||||
builder.WriteLine(
|
||||
$"{property.Name} = {sourceClassParameterName}.{property.SourcePropertyName}.Select({mappingContextParameterName}.{MappingContextSource.MapMethodName}<{property.MappedSourcePropertyTypeName}, {property.EnumerableTypeArgument}>).ToList();");
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -103,12 +114,13 @@ namespace MapTo.Sources
|
|||
? "null"
|
||||
: $"new object[] {{ {string.Join(", ", property.TypeConverterParameters)} }}";
|
||||
|
||||
builder.WriteLine($"{property.Name} = new {property.TypeConverter}().Convert({sourceClassParameterName}.{property.SourcePropertyName}, {parameters});");
|
||||
}
|
||||
builder.WriteLine(
|
||||
$"{property.Name} = new {property.TypeConverter}().Convert({sourceClassParameterName}.{property.SourcePropertyName}, {parameters});");
|
||||
}
|
||||
|
||||
// End constructor declaration
|
||||
return builder.WriteClosingBracket();
|
||||
}
|
||||
return builder;
|
||||
|
||||
}
|
||||
|
||||
private static SourceBuilder GenerateFactoryMethod(this SourceBuilder builder, MappingModel model)
|
||||
|
@ -124,6 +136,20 @@ namespace MapTo.Sources
|
|||
.WriteClosingBracket();
|
||||
}
|
||||
|
||||
private static SourceBuilder GenerateUpdateMethod(this SourceBuilder builder, MappingModel model)
|
||||
{
|
||||
var sourceClassParameterName = model.SourceTypeIdentifierName.ToCamelCase();
|
||||
|
||||
builder
|
||||
.GenerateUpdaterMethodsXmlDocs(model, sourceClassParameterName)
|
||||
.WriteLine($"public void {model.Options.NullableReferenceSyntax}Update({model.SourceType}{model.Options.NullableReferenceSyntax} {sourceClassParameterName})")
|
||||
.WriteOpeningBracket()
|
||||
.WriteProperties( model, sourceClassParameterName,"context" )
|
||||
.WriteClosingBracket();
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
private static SourceBuilder GenerateConvertorMethodsXmlDocs(this SourceBuilder builder, MappingModel model, string sourceClassParameterName)
|
||||
{
|
||||
if (!model.Options.GenerateXmlDocument)
|
||||
|
@ -140,6 +166,21 @@ namespace MapTo.Sources
|
|||
.WriteLine($"/// <returns>A new instance of <see cred=\"{model.TypeIdentifierName}\"/> -or- <c>null</c> if <paramref name=\"{sourceClassParameterName}\"/> is <c>null</c>.</returns>");
|
||||
}
|
||||
|
||||
private static SourceBuilder GenerateUpdaterMethodsXmlDocs(this SourceBuilder builder, MappingModel model, string sourceClassParameterName)
|
||||
{
|
||||
if (!model.Options.GenerateXmlDocument)
|
||||
{
|
||||
return builder;
|
||||
}
|
||||
|
||||
return builder
|
||||
.WriteLine("/// <summary>")
|
||||
.WriteLine($"/// Updates <see cref=\"{model.TypeIdentifierName}\"/> and sets its participating properties")
|
||||
.WriteLine($"/// using the property values from <paramref name=\"{sourceClassParameterName}\"/>.")
|
||||
.WriteLine("/// </summary>")
|
||||
.WriteLine($"/// <param name=\"{sourceClassParameterName}\">The instance of <see cref=\"{model.SourceType}\"/> to use as source.</param>");
|
||||
}
|
||||
|
||||
private static SourceBuilder GenerateSourceTypeExtensionClass(this SourceBuilder builder, MappingModel model)
|
||||
{
|
||||
return builder
|
||||
|
|
|
@ -32,6 +32,7 @@ namespace MapTo.Sources
|
|||
|
||||
builder
|
||||
.GeneratePrivateConstructor(model)
|
||||
|
||||
.WriteLine()
|
||||
.GenerateFactoryMethod(model)
|
||||
|
||||
|
@ -42,6 +43,7 @@ namespace MapTo.Sources
|
|||
// Extension class declaration
|
||||
.GenerateSourceTypeExtensionClass(model)
|
||||
|
||||
|
||||
// End namespace declaration
|
||||
.WriteClosingBracket();
|
||||
|
||||
|
@ -75,8 +77,25 @@ namespace MapTo.Sources
|
|||
builder
|
||||
.WriteLine($"private protected {model.TypeIdentifierName}({MappingContextSource.ClassName} {mappingContextParameterName}, {model.SourceType} {sourceClassParameterName})")
|
||||
.Indent()
|
||||
.Write(": this(");
|
||||
.Write(": this(").
|
||||
|
||||
WriteProperties(model, sourceClassParameterName, mappingContextParameterName)
|
||||
|
||||
.WriteLine(")")
|
||||
.Unindent()
|
||||
.WriteOpeningBracket()
|
||||
.WriteLine($"if ({mappingContextParameterName} == null) throw new ArgumentNullException(nameof({mappingContextParameterName}));")
|
||||
.WriteLine($"if ({sourceClassParameterName} == null) throw new ArgumentNullException(nameof({sourceClassParameterName}));")
|
||||
.WriteLine()
|
||||
.WriteLine($"{mappingContextParameterName}.{MappingContextSource.RegisterMethodName}({sourceClassParameterName}, this);");
|
||||
|
||||
// End constructor declaration
|
||||
return builder.WriteClosingBracket();
|
||||
}
|
||||
|
||||
private static SourceBuilder WriteProperties(this SourceBuilder builder, MappingModel model, string sourceClassParameterName,
|
||||
string mappingContextParameterName)
|
||||
{
|
||||
for (var i = 0; i < model.MappedProperties.Length; i++)
|
||||
{
|
||||
var property = model.MappedProperties[i];
|
||||
|
@ -100,7 +119,8 @@ namespace MapTo.Sources
|
|||
? "null"
|
||||
: $"new object[] {{ {string.Join(", ", property.TypeConverterParameters)} }}";
|
||||
|
||||
builder.Write($"{property.Name}: new {property.TypeConverter}().Convert({sourceClassParameterName}.{property.SourcePropertyName}, {parameters})");
|
||||
builder.Write(
|
||||
$"{property.Name}: new {property.TypeConverter}().Convert({sourceClassParameterName}.{property.SourcePropertyName}, {parameters})");
|
||||
}
|
||||
|
||||
if (i < model.MappedProperties.Length - 1)
|
||||
|
@ -109,16 +129,7 @@ namespace MapTo.Sources
|
|||
}
|
||||
}
|
||||
|
||||
builder.WriteLine(")")
|
||||
.Unindent()
|
||||
.WriteOpeningBracket()
|
||||
.WriteLine($"if ({mappingContextParameterName} == null) throw new ArgumentNullException(nameof({mappingContextParameterName}));")
|
||||
.WriteLine($"if ({sourceClassParameterName} == null) throw new ArgumentNullException(nameof({sourceClassParameterName}));")
|
||||
.WriteLine()
|
||||
.WriteLine($"{mappingContextParameterName}.{MappingContextSource.RegisterMethodName}({sourceClassParameterName}, this);");
|
||||
|
||||
// End constructor declaration
|
||||
return builder.WriteClosingBracket();
|
||||
return builder;
|
||||
}
|
||||
|
||||
private static SourceBuilder GenerateFactoryMethod(this SourceBuilder builder, MappingModel model)
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
|
||||
"version": "0.7",
|
||||
"version": "0.8",
|
||||
"semVer1NumericIdentifierPadding": 1,
|
||||
"publicReleaseRefSpec": [
|
||||
"^refs/heads/master$",
|
||||
|
|
Loading…
Reference in New Issue