Inject Update method
This commit is contained in:
parent
5af2be8eef
commit
93e9d91ffd
|
@ -43,5 +43,9 @@
|
||||||
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
|
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
|
||||||
<None Include="MapTo.props" Pack="true" PackagePath="build" Visible="false" />
|
<None Include="MapTo.props" Pack="true" PackagePath="build" Visible="false" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Folder Include="bin\Release\netstandard2.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
@ -33,6 +33,7 @@ namespace MapTo.Sources
|
||||||
.GeneratePrivateConstructor(model)
|
.GeneratePrivateConstructor(model)
|
||||||
.WriteLine()
|
.WriteLine()
|
||||||
.GenerateFactoryMethod(model)
|
.GenerateFactoryMethod(model)
|
||||||
|
.GenerateUpdateMethod(model)
|
||||||
|
|
||||||
// End class declaration
|
// End class declaration
|
||||||
.WriteClosingBracket()
|
.WriteClosingBracket()
|
||||||
|
@ -80,19 +81,29 @@ namespace MapTo.Sources
|
||||||
.WriteLine($"if ({sourceClassParameterName} == null) throw new ArgumentNullException(nameof({sourceClassParameterName}));")
|
.WriteLine($"if ({sourceClassParameterName} == null) throw new ArgumentNullException(nameof({sourceClassParameterName}));")
|
||||||
.WriteLine()
|
.WriteLine()
|
||||||
.WriteLine($"{mappingContextParameterName}.{MappingContextSource.RegisterMethodName}({sourceClassParameterName}, this);")
|
.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)
|
foreach (var property in model.MappedProperties)
|
||||||
{
|
{
|
||||||
if (property.TypeConverter is null)
|
if (property.TypeConverter is null)
|
||||||
{
|
{
|
||||||
if (property.IsEnumerable)
|
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
|
else
|
||||||
{
|
{
|
||||||
builder.WriteLine(property.MappedSourcePropertyTypeName is null
|
builder.WriteLine(property.MappedSourcePropertyTypeName is null
|
||||||
? $"{property.Name} = {sourceClassParameterName}.{property.SourcePropertyName};"
|
? $"{property.Name} = {sourceClassParameterName}.{property.SourcePropertyName};"
|
||||||
: $"{property.Name} = {mappingContextParameterName}.{MappingContextSource.MapMethodName}<{property.MappedSourcePropertyTypeName}, {property.Type}>({sourceClassParameterName}.{property.SourcePropertyName});");
|
: $"{property.Name} = {mappingContextParameterName}.{MappingContextSource.MapMethodName}<{property.MappedSourcePropertyTypeName}, {property.Type}>({sourceClassParameterName}.{property.SourcePropertyName});");
|
||||||
}
|
}
|
||||||
|
@ -103,14 +114,15 @@ namespace MapTo.Sources
|
||||||
? "null"
|
? "null"
|
||||||
: $"new object[] {{ {string.Join(", ", property.TypeConverterParameters)} }}";
|
: $"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)
|
private static SourceBuilder GenerateFactoryMethod(this SourceBuilder builder, MappingModel model)
|
||||||
{
|
{
|
||||||
var sourceClassParameterName = model.SourceTypeIdentifierName.ToCamelCase();
|
var sourceClassParameterName = model.SourceTypeIdentifierName.ToCamelCase();
|
||||||
|
@ -124,6 +136,20 @@ namespace MapTo.Sources
|
||||||
.WriteClosingBracket();
|
.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)
|
private static SourceBuilder GenerateConvertorMethodsXmlDocs(this SourceBuilder builder, MappingModel model, string sourceClassParameterName)
|
||||||
{
|
{
|
||||||
if (!model.Options.GenerateXmlDocument)
|
if (!model.Options.GenerateXmlDocument)
|
||||||
|
@ -139,6 +165,21 @@ namespace MapTo.Sources
|
||||||
.WriteLine($"/// <param name=\"{sourceClassParameterName}\">The instance of <see cref=\"{model.SourceType}\"/> to use as source.</param>")
|
.WriteLine($"/// <param name=\"{sourceClassParameterName}\">The instance of <see cref=\"{model.SourceType}\"/> to use as source.</param>")
|
||||||
.WriteLine($"/// <returns>A new instance of <see cred=\"{model.TypeIdentifierName}\"/> -or- <c>null</c> if <paramref name=\"{sourceClassParameterName}\"/> is <c>null</c>.</returns>");
|
.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)
|
private static SourceBuilder GenerateSourceTypeExtensionClass(this SourceBuilder builder, MappingModel model)
|
||||||
{
|
{
|
||||||
|
|
|
@ -32,6 +32,7 @@ namespace MapTo.Sources
|
||||||
|
|
||||||
builder
|
builder
|
||||||
.GeneratePrivateConstructor(model)
|
.GeneratePrivateConstructor(model)
|
||||||
|
|
||||||
.WriteLine()
|
.WriteLine()
|
||||||
.GenerateFactoryMethod(model)
|
.GenerateFactoryMethod(model)
|
||||||
|
|
||||||
|
@ -41,6 +42,7 @@ namespace MapTo.Sources
|
||||||
|
|
||||||
// Extension class declaration
|
// Extension class declaration
|
||||||
.GenerateSourceTypeExtensionClass(model)
|
.GenerateSourceTypeExtensionClass(model)
|
||||||
|
|
||||||
|
|
||||||
// End namespace declaration
|
// End namespace declaration
|
||||||
.WriteClosingBracket();
|
.WriteClosingBracket();
|
||||||
|
@ -75,8 +77,25 @@ namespace MapTo.Sources
|
||||||
builder
|
builder
|
||||||
.WriteLine($"private protected {model.TypeIdentifierName}({MappingContextSource.ClassName} {mappingContextParameterName}, {model.SourceType} {sourceClassParameterName})")
|
.WriteLine($"private protected {model.TypeIdentifierName}({MappingContextSource.ClassName} {mappingContextParameterName}, {model.SourceType} {sourceClassParameterName})")
|
||||||
.Indent()
|
.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++)
|
for (var i = 0; i < model.MappedProperties.Length; i++)
|
||||||
{
|
{
|
||||||
var property = model.MappedProperties[i];
|
var property = model.MappedProperties[i];
|
||||||
|
@ -100,7 +119,8 @@ namespace MapTo.Sources
|
||||||
? "null"
|
? "null"
|
||||||
: $"new object[] {{ {string.Join(", ", property.TypeConverterParameters)} }}";
|
: $"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)
|
if (i < model.MappedProperties.Length - 1)
|
||||||
|
@ -109,16 +129,7 @@ namespace MapTo.Sources
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
builder.WriteLine(")")
|
return builder;
|
||||||
.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 GenerateFactoryMethod(this SourceBuilder builder, MappingModel model)
|
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",
|
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
|
||||||
"version": "0.7",
|
"version": "0.8",
|
||||||
"semVer1NumericIdentifierPadding": 1,
|
"semVer1NumericIdentifierPadding": 1,
|
||||||
"publicReleaseRefSpec": [
|
"publicReleaseRefSpec": [
|
||||||
"^refs/heads/master$",
|
"^refs/heads/master$",
|
||||||
|
|
Loading…
Reference in New Issue