diff --git a/src/MapTo/MappingContext.cs b/src/MapTo/MappingContext.cs index 07e883e..f162c42 100644 --- a/src/MapTo/MappingContext.cs +++ b/src/MapTo/MappingContext.cs @@ -137,6 +137,8 @@ namespace MapTo return null; } + + string? converterFullyQualifiedName = null; var converterParameters = ImmutableArray.Empty; ITypeSymbol? mappedSourcePropertyType = null; @@ -162,7 +164,9 @@ namespace MapTo converterParameters.ToImmutableArray(), sourceProperty.Name, ToQualifiedDisplayName(mappedSourcePropertyType), - ToQualifiedDisplayName(enumerableTypeArgumentType)); + ToQualifiedDisplayName(enumerableTypeArgumentType), + (sourceProperty as IPropertySymbol).IsReadOnly); +; } protected bool TryGetMapTypeConverter(ISymbol property, IPropertySymbol sourceProperty, out string? converterFullyQualifiedName, diff --git a/src/MapTo/Models.cs b/src/MapTo/Models.cs index 9bda8be..4fb766c 100644 --- a/src/MapTo/Models.cs +++ b/src/MapTo/Models.cs @@ -29,7 +29,8 @@ namespace MapTo ImmutableArray TypeConverterParameters, string SourcePropertyName, string? MappedSourcePropertyTypeName, - string? EnumerableTypeArgument) + string? EnumerableTypeArgument, + bool isReadOnly) { public bool IsEnumerable => EnumerableTypeArgument is not null; } diff --git a/src/MapTo/Sources/MapClassSource.cs b/src/MapTo/Sources/MapClassSource.cs index 0effe4e..12074c5 100644 --- a/src/MapTo/Sources/MapClassSource.cs +++ b/src/MapTo/Sources/MapClassSource.cs @@ -21,17 +21,24 @@ namespace MapTo.Sources .WriteLine($"partial class {model.TypeIdentifierName}") .WriteOpeningBracket(); - // Class body - /*if (model.GenerateSecondaryConstructor) - { - builder - .GenerateSecondaryConstructor(model) - .WriteLine(); - }*/ - + // Class body + /*if (model.GenerateSecondaryConstructor) + { builder - .GeneratePrivateConstructor(model) - .WriteLine() + .GenerateSecondaryConstructor(model) + .WriteLine(); + }*/ + + builder + .GeneratePrivateConstructor(model) + .WriteLine(); + + if(PropertiesAreReadOnly(model)) + { + builder.GenerateUpdateMethod(model); + } + + builder //.GenerateFactoryMethod(model) .GenerateUpdateMethod(model) @@ -105,7 +112,7 @@ namespace MapTo.Sources { builder.WriteLine(property.MappedSourcePropertyTypeName is null ? $"{property.Name} = {sourceClassParameterName}.{property.SourcePropertyName};" - : $"{property.Name} = {mappingContextParameterName}.{MappingContextSource.MapMethodName}<{property.MappedSourcePropertyTypeName}, {property.Type}>({sourceClassParameterName}.{property.SourcePropertyName});"); + : ""); } } else @@ -123,6 +130,16 @@ namespace MapTo.Sources } + private static bool PropertiesAreReadOnly(MappingModel model) + { + foreach (var property in model.MappedProperties) + { + if (!property.isReadOnly) return false; + } + return true ; + + } + private static SourceBuilder GenerateFactoryMethod(this SourceBuilder builder, MappingModel model) { var sourceClassParameterName = model.SourceTypeIdentifierName.ToCamelCase();