Skip to content

Commit aebda06

Browse files
committed
Reusing MappingTypes instances when runtime types aren't needed
1 parent 4a262f5 commit aebda06

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

AgileMapper/Members/MappingTypes.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,19 @@ public static MappingTypes For<TSource, TTarget>(TSource source, TTarget target)
6363
targetType = typeof(TTarget);
6464
}
6565

66+
var runtimeTypesNeeded = runtimeSourceTypeNeeded || runtimeTargetTypeNeeded;
67+
68+
if (!runtimeTypesNeeded)
69+
{
70+
return Fixed<TSource, TTarget>();
71+
}
72+
6673
var isEnumerable = TypeInfo<TTarget>.IsEnumerable || (!runtimeTypesAreTheSame && targetType.IsEnumerable());
6774

6875
return new MappingTypes(
6976
sourceType,
7077
targetType,
71-
runtimeSourceTypeNeeded || runtimeTargetTypeNeeded,
78+
true, // <- runtimeTypesNeeded
7279
runtimeTypesAreTheSame,
7380
isEnumerable);
7481
}
@@ -86,7 +93,14 @@ public static MappingTypes For<TSource, TTarget>(TSource source, TTarget target)
8693
public bool IsEnumerable { get; }
8794

8895
public bool Equals(MappingTypes otherTypes)
89-
=> (otherTypes.SourceType == SourceType) && (otherTypes.TargetType == TargetType);
96+
{
97+
if (otherTypes == this)
98+
{
99+
return true;
100+
}
101+
102+
return (otherTypes.SourceType == SourceType) && (otherTypes.TargetType == TargetType);
103+
}
90104

91105
public MappingTypes WithTypes<TNewSource, TNewTarget>()
92106
{

AgileMapper/ObjectPopulation/ObjectMapperCacheExtensions.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@ public static IObjectMapper GetOrAddMapper(
1515

1616
var mapper = objectMapperCache.GetOrAdd(
1717
mappingData.MapperKey,
18-
key => key.MappingData.Mapper);
18+
key =>
19+
{
20+
var mapperToCache = key.MappingData.Mapper;
1921

20-
mappingData.MapperKey.MappingData = null;
22+
key.MappingData = null;
23+
24+
return mapperToCache;
25+
});
2126

2227
return mapper;
2328
}

AgileMapper/ObjectPopulation/ObjectMapperFactory.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ namespace AgileObjects.AgileMapper.ObjectPopulation
22
{
33
using System.Linq.Expressions;
44
using Caching;
5-
using Extensions;
6-
using Members;
75

86
internal class ObjectMapperFactory
97
{

AgileMapper/ObjectPopulation/ObjectMapperKeyBase.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,16 @@ protected bool SourceHasRequiredTypes(ObjectMapperKeyBase otherKey)
2828
public abstract IMembersSource GetMembersSource(IObjectMappingData parentMappingData);
2929

3030
public ObjectMapperKeyBase WithTypes<TNewSource, TNewTarget>()
31-
=> CreateInstance(MappingTypes.WithTypes<TNewSource, TNewTarget>());
31+
{
32+
var mappingTypes = MappingTypes.RuntimeTypesNeeded
33+
? MappingTypes.WithTypes<TNewSource, TNewTarget>()
34+
: MappingTypes.Fixed<TNewSource, TNewTarget>();
35+
36+
return CreateInstance(mappingTypes);
37+
}
3238

3339
protected abstract ObjectMapperKeyBase CreateInstance(MappingTypes newMappingTypes);
40+
41+
3442
}
3543
}

0 commit comments

Comments
 (0)