private static SourceBuilder GeneratePrivateConstructor(this SourceBuilder builder, MappingModel model) { var sourceClassParameterName = model.SourceTypeIdentifierName.ToCamelCase(); const string mappingContextParameterName = "context"; builder .WriteLine($"private protected {model.TypeIdentifierName}({MappingContextSource.ClassName} {mappingContextParameterName}, {model.SourceType} {sourceClassParameterName})") .Indent() .Write(": this("); for (var i = 0; i < model.MappedProperties.Length; i++) { var property = model.MappedProperties[i]; if (property.TypeConverter is null) { if (property.IsEnumerable) { builder.Write( $"{property.Name}: {sourceClassParameterName}.{property.SourcePropertyName}.Select({mappingContextParameterName}.{MappingContextSource.MapMethodName}<{property.MappedSourcePropertyTypeName}, {property.EnumerableTypeArgument}>).ToList()"); } else { builder.Write(property.MappedSourcePropertyTypeName is null ? $"{property.Name}: {sourceClassParameterName}.{property.SourcePropertyName}" : $"{property.Name}: {mappingContextParameterName}.{MappingContextSource.MapMethodName}<{property.MappedSourcePropertyTypeName}, {property.Type}>({sourceClassParameterName}.{property.SourcePropertyName})"); } } else { var parameters = property.TypeConverterParameters.IsEmpty ? "null" : $"new object[] {{ {string.Join(", ", property.TypeConverterParameters)} }}"; builder.Write($"{property.Name}: new {property.TypeConverter}().Convert({sourceClassParameterName}.{property.SourcePropertyName}, {parameters})"); } if (i < model.MappedProperties.Length - 1) { builder.Write(", "); } } 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()); }
private static SourceBuilder GeneratePrivateConstructor(this SourceBuilder builder, MappingModel model) { var sourceClassParameterName = model.SourceTypeIdentifierName.ToCamelCase(); const string mappingContextParameterName = "context"; var baseConstructor = model.HasMappedBaseClass ? $" : base({mappingContextParameterName}, {sourceClassParameterName})" : string.Empty; builder .WriteLine($"private protected {model.TypeIdentifierName}({MappingContextSource.ClassName} {mappingContextParameterName}, {model.SourceType} {sourceClassParameterName}){baseConstructor}") .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);") .WriteLine(); 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();"); } else { builder.WriteLine(property.MappedSourcePropertyTypeName is null ? $"{property.Name} = {sourceClassParameterName}.{property.SourcePropertyName};" : $"{property.Name} = {mappingContextParameterName}.{MappingContextSource.MapMethodName}<{property.MappedSourcePropertyTypeName}, {property.Type}>({sourceClassParameterName}.{property.SourcePropertyName});"); } } else { var parameters = property.TypeConverterParameters.IsEmpty ? "null" : $"new object[] {{ {string.Join(", ", property.TypeConverterParameters)} }}"; builder.WriteLine($"{property.Name} = new {property.TypeConverter}().Convert({sourceClassParameterName}.{property.SourcePropertyName}, {parameters});"); } } // End constructor declaration return(builder.WriteClosingBracket()); }