Skip to content

Commit ec57f72

Browse files
committed
Lazy-loading ObjectMappingData Mappers through a method
1 parent 3189ce1 commit ec57f72

File tree

8 files changed

+15
-18
lines changed

8 files changed

+15
-18
lines changed

AgileMapper/ObjectPopulation/IObjectMappingData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ internal interface IObjectMappingData : IObjectMappingDataUntyped
1919

2020
ObjectMapperData MapperData { get; }
2121

22-
IObjectMapper Mapper { get; set; }
22+
IObjectMapper GetOrCreateMapper();
2323

2424
IChildMemberMappingData GetChildMappingData(IMemberMapperData childMapperData);
2525

AgileMapper/ObjectPopulation/MappingDataFactory.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static ObjectMappingData<TSource, TTarget> ForChild<TSource, TTarget>(
4242
(md.DataSourceIndex == dataSourceIndex) &&
4343
(md.TargetMember.RegistrationName == targetMemberRegistrationName));
4444

45-
mappingData.Mapper = mapperData.Mapper;
45+
mappingData.SetMapper(mapperData.Mapper);
4646

4747
return mappingData;
4848
}
@@ -59,8 +59,8 @@ public static ObjectMappingData<TSourceElement, TTargetElement> ForElement<TSour
5959

6060
if (ChildMappersNeeded(mappingData.Parent))
6161
{
62-
mappingData.Mapper =
63-
GetParentMappingData(mappingData).MapperData.ChildMapperDatas.First().Mapper;
62+
mappingData.SetMapper(
63+
GetParentMappingData(mappingData).MapperData.ChildMapperDatas.First().Mapper);
6464
}
6565

6666
return mappingData;

AgileMapper/ObjectPopulation/MappingFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public static Expression GetInlineMappingBlock(
150150
MappingValues mappingValues,
151151
Expression createMappingDataCall)
152152
{
153-
var mapper = mappingData.Mapper;
153+
var mapper = mappingData.GetOrCreateMapper();
154154

155155
if (mapper.IsNullObject)
156156
{

AgileMapper/ObjectPopulation/ObjectMapper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public object MapSubObject(IObjectMappingData mappingData)
140140
mappingData.MapperKey,
141141
key =>
142142
{
143-
var mapperToCache = key.MappingData.Mapper;
143+
var mapperToCache = key.MappingData.GetOrCreateMapper();
144144

145145
key.MappingData = null;
146146

AgileMapper/ObjectPopulation/ObjectMapperFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public ObjectMapper<TSource, TTarget> GetOrCreateRoot<TSource, TTarget>(ObjectMa
5757
mappingData.MapperKey,
5858
key =>
5959
{
60-
var mapperToCache = key.MappingData.Mapper;
60+
var mapperToCache = key.MappingData.GetOrCreateMapper();
6161
var data = key.MappingData;
6262

6363
key.MappingData = null;

AgileMapper/ObjectPopulation/ObjectMappingData.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,11 @@ private ObjectMappingData(
6868

6969
public ObjectMapperKeyBase MapperKey { get; }
7070

71-
public IObjectMapper Mapper
72-
{
73-
get => _mapper ?? (_mapper = MapperContext.ObjectMapperFactory.Create(this));
74-
set
75-
{
76-
_mapper = (ObjectMapper<TSource, TTarget>)value;
77-
_mapperData = _mapper.MapperData;
78-
}
79-
}
71+
public IObjectMapper GetOrCreateMapper()
72+
=> _mapper ?? (_mapper = MapperContext.ObjectMapperFactory.Create(this));
73+
74+
public void SetMapper(IObjectMapper mapper)
75+
=> _mapper = (ObjectMapper<TSource, TTarget>)mapper;
8076

8177
public ObjectMapperData MapperData
8278
{

AgileMapper/ObjectPopulation/Recursion/RecursionMapperFunc.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private void CreateMapperFunc(IObjectMappingData mappingData)
5353
mappingData.MapperKey.MappingData = mappingData;
5454
mappingData.MapperKey.MapperData = mappingData.MapperData;
5555

56-
MappingLambda = mappingData.Mapper.MappingLambda;
56+
MappingLambda = mappingData.GetOrCreateMapper().MappingLambda;
5757

5858
var typedMappingLambda = (Expression<MapperFunc<TChildSource, TChildTarget>>)MappingLambda;
5959
_recursionMapperFunc = typedMappingLambda.Compile();

AgileMapper/Plans/MappingPlan.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ internal MappingPlan(IObjectMapper cachedMapper)
2828
}
2929
}
3030

31-
internal static MappingPlan For(IObjectMappingData mappingData) => new MappingPlan(mappingData.Mapper);
31+
internal static MappingPlan For(IObjectMappingData mappingData)
32+
=> new MappingPlan(mappingData.GetOrCreateMapper());
3233

3334
/// <summary>
3435
/// Converts the given <paramref name="mappingPlan"/> to its string representation.

0 commit comments

Comments
 (0)