From 7de9b48c69bb86619278f4bec7a53cd26745dd05 Mon Sep 17 00:00:00 2001 From: Wvader <34067397+wvader@users.noreply.github.com> Date: Mon, 22 Aug 2022 03:14:14 +0100 Subject: [PATCH] Inject empty constructor --- src/BlueWest.MapTo/Extensions/CommonSource.cs | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/BlueWest.MapTo/Extensions/CommonSource.cs b/src/BlueWest.MapTo/Extensions/CommonSource.cs index 92434e8..4553839 100644 --- a/src/BlueWest.MapTo/Extensions/CommonSource.cs +++ b/src/BlueWest.MapTo/Extensions/CommonSource.cs @@ -32,6 +32,7 @@ namespace MapTo.Extensions .WriteOpeningBracket() .WriteLine() // Class body + .GeneratePublicEmptyConstructor(model, ref constructorHeaders, true) .GeneratePublicConstructor(model, ref constructorHeaders) .GeneratePublicConstructor(model, ref constructorHeaders, true); @@ -112,6 +113,40 @@ namespace MapTo.Extensions return builder; } + private static SourceBuilder GeneratePublicEmptyConstructor(this SourceBuilder builder, MappingModel model, ref List constructorHeaders, bool filterNonMapped = false) + { + const string mappingContextParameterName = "context"; + + foreach (var targetSourceType in model.MappedSourceTypes) + { + var baseConstructor = /*model.HasMappedBaseClass ? $" : base({mappingContextParameterName}, {sourceClassParameterName})" :*/ string.Empty; + + var constructorHeader = + $"public {model.TypeIdentifierName}(){baseConstructor}"; + + + bool hasAlreadyConstructor = false; + + foreach (var header in constructorHeaders) + { + if(constructorHeader.Contains(header)) hasAlreadyConstructor = true; + } + + if (hasAlreadyConstructor) continue; + + constructorHeaders.Add(constructorHeader); + + builder + .WriteLine(constructorHeader) + .WriteOpeningBracket() + .WriteClosingBracket() + .WriteLine(); + } + + // End constructor declaration + return builder; + } + private static bool IsMappedProperty(this System.Collections.Immutable.ImmutableArray properties, MappedMember property) {