Skip to content

Commit aff3ad0

Browse files
committed
Optimising root mapping plan mapping data creation
1 parent cb2344e commit aff3ad0

File tree

6 files changed

+32
-39
lines changed

6 files changed

+32
-39
lines changed

AgileMapper.UnitTests/Members/WhenFindingDataSources.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace AgileObjects.AgileMapper.UnitTests.Members
22
{
33
using AgileMapper.Members;
4+
using ObjectPopulation;
45
using Shouldly;
56
using TestClasses;
67
using Xunit;
@@ -15,7 +16,7 @@ public void ShouldNotMatchSameNameIncompatibleTypeProperties()
1516
var targetMember = TargetMemberFor<PublicProperty<byte>>(x => x.Value);
1617

1718
var mappingContext = new MappingExecutor<TwoValues>(DefaultMapperContext.RuleSets.CreateNew, DefaultMapperContext);
18-
var rootMappingData = mappingContext.CreateRootMappingData(source, target);
19+
var rootMappingData = ObjectMappingDataFactory.ForRoot(source, target, mappingContext);
1920
var rootMapperData = rootMappingData.MapperData;
2021

2122
var childMapperData = new ChildMemberMapperData(targetMember, rootMapperData);

AgileMapper/IMappingContext.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
namespace AgileObjects.AgileMapper
22
{
3-
using ObjectPopulation;
4-
53
internal interface IMappingContext
64
{
75
MapperContext MapperContext { get; }
86

97
MappingRuleSet RuleSet { get; }
10-
11-
IObjectMappingData CreateRootMappingData<TSource, TTarget>(TSource source, TTarget target);
128
}
139
}

AgileMapper/MappingExecutor.cs

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using Api;
66
using Api.Configuration;
77
using Extensions;
8-
using Members;
98
using ObjectPopulation;
109

1110
internal class MappingExecutor<TSource> : ITargetTypeSelector<TSource>, IMappingContext
@@ -97,38 +96,19 @@ private TTarget PerformMapping<TTarget>(MappingRuleSet ruleSet, TTarget target)
9796

9897
private TTarget PerformMapping<TTarget>(TTarget target)
9998
{
100-
if (SkipTypeChecks<TTarget>())
99+
if (TypeInfo<TSource>.RuntimeTypeNeeded || TypeInfo<TTarget>.RuntimeTypeNeeded)
101100
{
102-
// Optimise for the most common scenario:
103-
var typedRootMappingData = CreateTypedRootMappingData(target);
101+
var rootMappingData = ObjectMappingDataFactory.ForRoot(_source, target, this);
102+
var result = rootMappingData.MapStart();
104103

105-
return typedRootMappingData.MapStart();
104+
return (TTarget)result;
106105
}
107106

108-
var rootMappingData = CreateRootMappingData(target);
109-
var result = rootMappingData.MapStart();
107+
// Optimise for the most common scenario:
108+
var typedRootMappingData = ObjectMappingDataFactory
109+
.ForRootFixedTypes(_source, target, this);
110110

111-
return (TTarget)result;
111+
return typedRootMappingData.MapStart();
112112
}
113-
114-
private static bool SkipTypeChecks<TTarget>()
115-
=> !(TypeInfo<TSource>.RuntimeTypeNeeded || TypeInfo<TTarget>.RuntimeTypeNeeded);
116-
117-
private ObjectMappingData<TSource, TTarget> CreateTypedRootMappingData<TTarget>(TTarget target)
118-
{
119-
return new ObjectMappingData<TSource, TTarget>(
120-
_source,
121-
target,
122-
null, // <- No enumerable index because we're at the root
123-
new RootObjectMapperKey(MappingTypes<TSource, TTarget>.Fixed, this),
124-
this,
125-
parent: null);
126-
}
127-
128-
private IObjectMappingData CreateRootMappingData<TTarget>(TTarget target)
129-
=> CreateRootMappingData(_source, target);
130-
131-
public IObjectMappingData CreateRootMappingData<TDataSource, TDataTarget>(TDataSource source, TDataTarget target)
132-
=> ObjectMappingDataFactory.ForRoot(source, target, this);
133113
}
134114
}

AgileMapper/ObjectPopulation/ObjectMappingDataFactory.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ internal class ObjectMappingDataFactory : IObjectMappingDataFactoryBridge
1616
{
1717
private static readonly IObjectMappingDataFactoryBridge _bridge = new ObjectMappingDataFactory();
1818

19+
public static ObjectMappingData<TSource, TTarget> ForRootFixedTypes<TSource, TTarget>(
20+
TSource source,
21+
TTarget target,
22+
IMappingContext mappingContext)
23+
{
24+
return new ObjectMappingData<TSource, TTarget>(
25+
source,
26+
target,
27+
null, // <- No enumerable index because we're at the root
28+
new RootObjectMapperKey(MappingTypes<TSource, TTarget>.Fixed, mappingContext),
29+
mappingContext,
30+
parent: null);
31+
}
32+
1933
public static IObjectMappingData ForRoot<TSource, TTarget>(
2034
TSource source,
2135
TTarget target,

AgileMapper/ObjectPopulation/RootObjectMapperKey.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ namespace AgileObjects.AgileMapper.ObjectPopulation
22
{
33
using Members;
44
using Members.Sources;
5+
#if DEBUG
56
using ReadableExpressions.Extensions;
7+
#endif
68

79
internal class RootObjectMapperKey : ObjectMapperKeyBase
810
{
@@ -37,14 +39,14 @@ public override bool Equals(object obj)
3739
SourceHasRequiredTypes(otherKey);
3840
}
3941

40-
#region ExcludeFromCodeCoverage
42+
#region ExcludeFromCodeCoverage
4143
#if DEBUG
4244
[ExcludeFromCodeCoverage]
4345
#endif
44-
#endregion
46+
#endregion
4547
public override int GetHashCode() => 0;
4648

47-
#region ToString
49+
#region ToString
4850
#if DEBUG
4951
[ExcludeFromCodeCoverage]
5052
public override string ToString()
@@ -55,6 +57,6 @@ public override string ToString()
5557
return $"{_ruleSet.Name}: {sourceTypeName} -> {targetTypeName}";
5658
}
5759
#endif
58-
#endregion
60+
#endregion
5961
}
6062
}

AgileMapper/Plans/MappingPlan.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ internal MappingPlan(IMappingContext mappingContext)
2727
{
2828
_generatedPlanData = new List<MappingPlanData>();
2929

30-
var rootMappingData = mappingContext
31-
.CreateRootMappingData(default(TSource), default(TTarget));
30+
var rootMappingData = ObjectMappingDataFactory
31+
.ForRootFixedTypes(default(TSource), default(TTarget), mappingContext);
3232

3333
var rootPlanData = new MappingPlanData(rootMappingData);
3434

0 commit comments

Comments
 (0)