Skip to content

Commit 0534e49

Browse files
committed
Fixing caching of complex type ctor parameter construction expressions / Using hash code comparers in member caches
1 parent b6ff1cf commit 0534e49

File tree

7 files changed

+13
-15
lines changed

7 files changed

+13
-15
lines changed

AgileMapper.UnitTests/Configuration/WhenConfiguringObjectCreation.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public void ShouldUseAConfiguredSingleParameterFactoryFuncForASpecifiedSourceTar
122122
nonMatchingSourceResult.Address.Line2.ShouldBeNull();
123123

124124
var nonMatchingTargetResult = mapper.Map(matchingSource).ToANew<Person>();
125+
125126
// The declared target type doesn't match, but CustomerViewModel is automatically
126127
// mapped to Customer because of the matching naming conventions:
127128
nonMatchingTargetResult.Address.Line2.ShouldBe("Frankie House");

AgileMapper/Api/Configuration/CustomDataSourceTargetMemberSpecifier.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -266,14 +266,8 @@ private static Exception AmbiguousParameterException(string parameterMatchInfo)
266266

267267
private ConfiguredDataSourceFactory CreateForCtorParam<TParam>(ParameterInfo parameter)
268268
{
269-
var memberChain = new[]
270-
{
271-
Member.RootTarget<TTarget>(),
272-
Member.ConstructorParameter(parameter)
273-
};
274-
275269
var valueLambda = GetValueLambdaInfo<TParam>();
276-
var constructorParameter = QualifiedMember.From(memberChain, _configInfo.MapperContext);
270+
var constructorParameter = CreateRootTargetQualifiedMember().Append(Member.ConstructorParameter(parameter));
277271

278272
return new ConfiguredDataSourceFactory(_configInfo, valueLambda, constructorParameter);
279273
}

AgileMapper/Members/ConfiguredSourceMember.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ private ConfiguredSourceMember(
8181
: type.GetEnumerableElementType();
8282
}
8383

84-
_childMemberCache = mapperContext.Cache.CreateNew<Member, ConfiguredSourceMember>();
84+
_childMemberCache = mapperContext.Cache.CreateNew<Member, ConfiguredSourceMember>(
85+
default(HashCodeComparer<Member>));
8586
}
8687

8788
public bool IsRoot => false;

AgileMapper/Members/Member.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ private static class TargetMemberCache<T>
244244

245245
public bool Equals(Member otherMember) => otherMember._hashCode == _hashCode;
246246

247+
public override int GetHashCode() => _hashCode;
248+
247249
#region ExcludeFromCodeCoverage
248250
#if DEBUG
249251
[ExcludeFromCodeCoverage]

AgileMapper/Members/MemberExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public static Member[] RelativeTo(this Member[] memberChain, Member[] otherMembe
199199
var startIndex = memberChain.Length - 1;
200200

201201
if ((memberChain.Length > 2) &&
202-
memberChain[startIndex] == memberChain[startIndex - 1])
202+
memberChain[startIndex] == memberChain[startIndex - 1])
203203
{
204204
// The member chain ends in a 1-to-1, immediately recursive
205205
// relationship; skip the last element:
@@ -210,7 +210,7 @@ public static Member[] RelativeTo(this Member[] memberChain, Member[] otherMembe
210210
{
211211
var member = memberChain[startIndex];
212212

213-
if (member == otherMembersLeafMember)
213+
if (member.Equals(otherMembersLeafMember))
214214
{
215215
break;
216216
}

AgileMapper/Members/QualifiedMember.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private QualifiedMember(Member leafMember, MapperContext mapperContext)
8181

8282
if (!IsSimple)
8383
{
84-
_childMemberCache = mapperContext.Cache.CreateNew<Member, QualifiedMember>();
84+
_childMemberCache = mapperContext.Cache.CreateNew<Member, QualifiedMember>(default(HashCodeComparer<Member>));
8585
}
8686

8787
RegistrationName = (LeafMember.MemberType != MemberType.ConstructorParameter)

AgileMapper/ObjectPopulation/ComplexTypes/ComplexTypeConstructionFactory.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,11 @@ public void AddTo(IList<Construction> constructions, ConstructionKey key)
361361
{
362362
if (ParameterCount > 0)
363363
{
364-
foreach (var memberAndDataSourceSet in _argumentDataSources)
364+
var dataSources = key.MappingData.MapperData.DataSourcesByTargetMember;
365+
366+
foreach (var memberAndDataSourceSet in _argumentDataSources.Filter(ads => !dataSources.ContainsKey(ads.Item1)))
365367
{
366-
key.MappingData.MapperData.DataSourcesByTargetMember.Add(
367-
memberAndDataSourceSet.Item1,
368-
memberAndDataSourceSet.Item2);
368+
dataSources.Add(memberAndDataSourceSet.Item1, memberAndDataSourceSet.Item2);
369369
}
370370
}
371371

0 commit comments

Comments
 (0)