diff --git a/AgileMapper.UnitTests/Dictionaries/WhenMappingToNewDictionaryMembers.cs b/AgileMapper.UnitTests/Dictionaries/WhenMappingToNewDictionaryMembers.cs index 52a91ba22..78e8b9664 100644 --- a/AgileMapper.UnitTests/Dictionaries/WhenMappingToNewDictionaryMembers.cs +++ b/AgileMapper.UnitTests/Dictionaries/WhenMappingToNewDictionaryMembers.cs @@ -184,6 +184,43 @@ public void ShouldMapADictionaryObjectValuesToNewDictionaryObjectValues() result.Value["key2"].ShouldNotBeSameAs(source.Value["key2"]); } + // See https://github.com/agileobjects/AgileMapper/issues/97 + [Fact] + public void ShouldDeepCloneAReadOnlyDictionaryMember() + { + var source = new Issue97.ReadonlyDictionary(); + + source.Dictionary["Test"] = "123"; + + var cloned = Mapper.DeepClone(source); + + cloned.Dictionary.ContainsKey("Test").ShouldBeTrue(); + cloned.Dictionary["Test"].ShouldBe("123"); + } + + [Fact] + public void ShouldUseACloneConstructorToPopulateADictionaryConstructorParameter() + { + var source = new PublicReadOnlyProperty>( + new Dictionary { ["Test"] = "Hello!" }); + + var result = Mapper.Map(source).ToANew>>(); + + result.Value.ContainsKey("Test").ShouldBeTrue(); + result.Value["Test"].ShouldBe("Hello!"); + } + + [Fact] + public void ShouldNotCreateDictionaryAsFallbackComplexType() + { + var source = new PublicReadOnlyProperty>( + new Dictionary()); + + var cloned = Mapper.DeepClone(source); + + cloned.ShouldBeNull(); + } + [Fact] public void ShouldFlattenAComplexTypeCollectionToANestedObjectDictionaryImplementation() { @@ -234,5 +271,22 @@ public void ShouldFlattenAComplexTypeCollectionToANestedObjectDictionaryImplemen result.Value.ShouldNotContainKey("[1].Address.Line2"); } + + #region Helper Members + + private static class Issue97 + { + public class ReadonlyDictionary + { + public ReadonlyDictionary() + { + Dictionary = new Dictionary(); + } + + public IDictionary Dictionary { get; } + } + } + + #endregion } } diff --git a/AgileMapper/DataSources/Finders/SourceMemberDataSourceFinder.cs b/AgileMapper/DataSources/Finders/SourceMemberDataSourceFinder.cs index a6fda1c74..7a59d2630 100644 --- a/AgileMapper/DataSources/Finders/SourceMemberDataSourceFinder.cs +++ b/AgileMapper/DataSources/Finders/SourceMemberDataSourceFinder.cs @@ -22,7 +22,7 @@ public IEnumerable FindFor(DataSourceFindContext context) { if (context.DataSourceIndex == 0) { - if (targetMember.IsComplex && (targetMember.Type != typeof(object))) + if (UseFallbackComplexTypeMappingDataSource(targetMember)) { yield return new ComplexTypeMappingDataSource(context.DataSourceIndex, context.ChildMappingData); } @@ -61,5 +61,8 @@ private static IDataSource GetSourceMemberDataSourceOrNull(DataSourceFindContext return context.GetFinalDataSource(sourceMemberDataSource, contextMappingData); } + + private static bool UseFallbackComplexTypeMappingDataSource(QualifiedMember targetMember) + => targetMember.IsComplex && !targetMember.IsDictionary && (targetMember.Type != typeof(object)); } } \ No newline at end of file diff --git a/AgileMapper/Extensions/Internal/EnumerableExtensions.cs b/AgileMapper/Extensions/Internal/EnumerableExtensions.cs index e6bcba0fb..b106f25ac 100644 --- a/AgileMapper/Extensions/Internal/EnumerableExtensions.cs +++ b/AgileMapper/Extensions/Internal/EnumerableExtensions.cs @@ -43,6 +43,7 @@ public static void AddRange(this List items, IEnu [DebuggerStepThrough] public static T First(this IList items) => items[0]; + [DebuggerStepThrough] public static T First(this IList items, Func predicate) { if (TryFindMatch(items, predicate, out var match)) @@ -53,9 +54,11 @@ public static T First(this IList items, Func predicate) throw new InvalidOperationException("Sequence contains no matching element"); } + [DebuggerStepThrough] public static T FirstOrDefault(this IList items, Func predicate) => TryFindMatch(items, predicate, out var match) ? match : default(T); + [DebuggerStepThrough] public static bool TryFindMatch(this IList items, Func predicate, out T match) { for (int i = 0, n = items.Count; i < n; i++) diff --git a/AgileMapper/Members/MemberCache.cs b/AgileMapper/Members/MemberCache.cs index 55e3c51b7..56d64e410 100644 --- a/AgileMapper/Members/MemberCache.cs +++ b/AgileMapper/Members/MemberCache.cs @@ -40,6 +40,11 @@ public IList GetTargetMembers(Type targetType) { return _membersCache.GetOrAdd(TypeKey.ForTargetMembers(targetType), key => { + if (key.Type.IsEnumerable()) + { + return Enumerable.EmptyArray; + } + var fields = GetFields(key.Type, All); var properties = GetProperties(key.Type, All); var methods = GetMethods(key.Type, OnlyCallableSetters, Member.SetMethod); diff --git a/AgileMapper/Members/SourceMemberMatcher.cs b/AgileMapper/Members/SourceMemberMatcher.cs index 49bc9010a..fa5e0a1d2 100644 --- a/AgileMapper/Members/SourceMemberMatcher.cs +++ b/AgileMapper/Members/SourceMemberMatcher.cs @@ -4,7 +4,6 @@ using System.Collections.Generic; using System.Linq; using Extensions; - using Extensions.Internal; internal static class SourceMemberMatcher { diff --git a/AgileMapper/ObjectPopulation/DictionaryMappingExpressionFactory.cs b/AgileMapper/ObjectPopulation/DictionaryMappingExpressionFactory.cs index dc3937369..0857df2f6 100644 --- a/AgileMapper/ObjectPopulation/DictionaryMappingExpressionFactory.cs +++ b/AgileMapper/ObjectPopulation/DictionaryMappingExpressionFactory.cs @@ -277,40 +277,59 @@ protected override bool TargetCannotBeMapped(IObjectMappingData mappingData, out protected override IEnumerable GetObjectPopulation(MappingCreationContext context) { - var mapperData = context.MapperData; - - if (!mapperData.TargetMember.IsDictionary) + if (!context.MapperData.TargetMember.IsDictionary) { yield return GetDictionaryPopulation(context.MappingData); yield break; } - Func assignmentFactory; + var assignmentFactory = GetDictionaryAssignmentFactoryOrNull(context, out var useAssignmentOnly); + + if (useAssignmentOnly) + { + yield return assignmentFactory.Invoke(context.MappingData); + yield break; + } + + var population = GetDictionaryPopulation(context.MappingData); + var assignment = assignmentFactory?.Invoke(context.MappingData); + + yield return assignment; + yield return population; + } + + private static Func GetDictionaryAssignmentFactoryOrNull( + MappingCreationContext context, + out bool useAssignmentOnly) + { + if (context.MapperData.TargetMember.IsReadOnly) + { + useAssignmentOnly = false; + return null; + } + + var mapperData = context.MapperData; if (SourceMemberIsDictionary(mapperData, out var sourceDictionaryMember)) { if (UseDictionaryCloneConstructor(sourceDictionaryMember, mapperData, out var cloneConstructor)) { - yield return GetClonedDictionaryAssignment(mapperData, cloneConstructor); - yield break; + useAssignmentOnly = true; + return md => GetClonedDictionaryAssignment(md.MapperData, cloneConstructor); } - assignmentFactory = GetMappedDictionaryAssignment; - } - else if (context.InstantiateLocalVariable) - { - assignmentFactory = (dsm, md) => GetParameterlessDictionaryAssignment(md); + useAssignmentOnly = false; + return md => GetMappedDictionaryAssignment(sourceDictionaryMember, md); } - else + + useAssignmentOnly = false; + + if (context.InstantiateLocalVariable) { - assignmentFactory = null; + return GetParameterlessDictionaryAssignment; } - var population = GetDictionaryPopulation(context.MappingData); - var assignment = assignmentFactory?.Invoke(sourceDictionaryMember, context.MappingData); - - yield return assignment; - yield return population; + return null; } private static bool SourceMemberIsDictionary( @@ -328,9 +347,9 @@ private static bool UseDictionaryCloneConstructor( { cloneConstructor = null; - return mapperData.TargetMember.ElementType.IsSimple() && - (sourceDictionaryMember.Type == mapperData.TargetType) && - ((cloneConstructor = GetDictionaryCloneConstructor(mapperData)) != null); + return (sourceDictionaryMember.Type == mapperData.TargetType) && + mapperData.TargetMember.ElementType.IsSimple() && + ((cloneConstructor = GetDictionaryCloneConstructor(mapperData)) != null); } private static ConstructorInfo GetDictionaryCloneConstructor(ITypePair mapperData) diff --git a/AgileMapper/ObjectPopulation/MappingDataCreationFactory.cs b/AgileMapper/ObjectPopulation/MappingDataCreationFactory.cs index 59fce1258..d7067d4f8 100644 --- a/AgileMapper/ObjectPopulation/MappingDataCreationFactory.cs +++ b/AgileMapper/ObjectPopulation/MappingDataCreationFactory.cs @@ -35,7 +35,7 @@ private static bool UseAsConversion(ObjectMapperData childMapperData, out Expres return false; } - //[DebuggerStepThrough] + [DebuggerStepThrough] public static Expression ForChild( MappingValues mappingValues, int dataSourceIndex, diff --git a/AgileMapper/ObjectPopulation/ObjectMapperData.cs b/AgileMapper/ObjectPopulation/ObjectMapperData.cs index 6e3075a45..aaf8d1940 100644 --- a/AgileMapper/ObjectPopulation/ObjectMapperData.cs +++ b/AgileMapper/ObjectPopulation/ObjectMapperData.cs @@ -239,6 +239,13 @@ private static bool TypeHasACompatibleChildMember( checkedTypes.Add(parentType); + if (parentType.IsEnumerable()) + { + parentType = parentType.GetEnumerableElementType(); + + return !parentType.IsSimple() && TypeHasACompatibleChildMember(targetType, parentType, checkedTypes); + } + var childTargetMembers = GlobalContext.Instance.MemberCache.GetTargetMembers(parentType); foreach (var childMember in childTargetMembers)