Skip to content

Commit ee33db9

Browse files
authored
Service provider support (#75)
* Start of service provider support * Support for resolving named and unnamed services through MappingData * Support for configuring a service provider instance * Support for discovered named GetService overloads * Support for resolving GetService(Type, string, ...extras) service provider methods * Surfacing registered service provider instances from MappingInstanceData / Test coverage for service provision error conditions * Erroring if requested service provider type is not configured * Duplicate service provider error test * Erroring on duplicate service provider definition / Test coverage for service provider use in a configured data source * Support for service provision in a global mapping callback * Erroring if null service providers supplied * Fixing MappingContext access in MappingInstanceData base ctor
1 parent bed660e commit ee33db9

15 files changed

+1041
-22
lines changed

AgileMapper.UnitTests.NonParallel/Configuration/WhenConfiguringMappingCallbacks.cs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ public void ShouldExecuteAGlobalPreMappingCallbackViaTheStaticApi()
1414
{
1515
var mappedTypes = new List<Type>();
1616

17-
Mapper
18-
.Before
17+
Mapper.Before
1918
.MappingBegins
2019
.Call((s, t) => mappedTypes.AddRange(new[] { s.GetType(), t?.GetType() }));
2120

@@ -34,8 +33,7 @@ public void ShouldExecuteAGlobalPostMappingCallbackViaTheStaticApiConditionally(
3433
{
3534
var mappedTypes = new List<Type>();
3635

37-
Mapper
38-
.After
36+
Mapper.After
3937
.MappingEnds
4038
.If((s, t) => SourceIsPersonViewModel(s, t))
4139
.Call((s, t) => mappedTypes.AddRange(new[] { s.GetType(), t.GetType() }));
@@ -49,10 +47,7 @@ public void ShouldExecuteAGlobalPostMappingCallbackViaTheStaticApiConditionally(
4947
}
5048

5149
// ReSharper disable once UnusedParameter.Local
52-
private static bool SourceIsPersonViewModel(object source, object target)
53-
{
54-
return source is PersonViewModel;
55-
}
50+
private static bool SourceIsPersonViewModel(object source, object target) => source is PersonViewModel;
5651

5752
[Fact]
5853
public void ShouldExecutePreAndPostMappingCallbacksForASpecifiedMemberConditionallyViaTheStaticApi()
@@ -63,8 +58,7 @@ public void ShouldExecutePreAndPostMappingCallbacksForASpecifiedMemberConditiona
6358
var customersAdded = 0;
6459
var customersRemoved = 0;
6560

66-
Mapper
67-
.WhenMapping
61+
Mapper.WhenMapping
6862
.To<Customer>()
6963
.Before
7064
.Mapping(c => c.Discount)
@@ -103,24 +97,24 @@ public void ShouldExecutePreAndPostMappingCallbacksForASpecifiedMemberConditiona
10397
}
10498

10599
[Fact]
106-
public void ShouldExecuteAPreMappingCallbackForAConstructorOnlyTarget()
100+
public void ShouldExecuteAPreMappingCallbackForAConstructorOnlyTargetViaTheStaticApi()
107101
{
108-
using (var mapper = Mapper.CreateNew())
102+
TestThenReset(() =>
109103
{
110104
var callbackCalled = false;
111105

112-
mapper.WhenMapping
106+
Mapper.WhenMapping
113107
.To<PublicCtor<string>>()
114108
.Before
115109
.MappingBegins
116110
.Call(ctx => callbackCalled = true);
117111

118112
var source = new PublicProperty<Guid> { Value = Guid.NewGuid() };
119-
var result = mapper.Map(source).ToANew<PublicCtor<string>>();
113+
var result = Mapper.Map(source).ToANew<PublicCtor<string>>();
120114

121115
result.Value.ShouldBe(source.Value.ToString());
122116
callbackCalled.ShouldBeTrue();
123-
}
117+
});
124118
}
125119
}
126120
}

AgileMapper.UnitTests.NonParallel/NonParallelTestsBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ protected void TestThenReset(Action testAction)
88
{
99
try
1010
{
11+
Mapper.ResetDefaultInstance();
12+
1113
testAction.Invoke();
1214
}
1315
finally

AgileMapper.UnitTests/AgileMapper.UnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
<Compile Include="Configuration\Inline\WhenViewingMappingPlans.cs" />
106106
<Compile Include="Configuration\WhenApplyingMapperConfigurations.cs" />
107107
<Compile Include="Configuration\WhenApplyingMapperConfigurationsIncorrectly.cs" />
108+
<Compile Include="Configuration\WhenResolvingServices.cs" />
108109
<Compile Include="Configuration\WhenViewingMappingPlans.cs" />
109110
<Compile Include="Dictionaries\WhenMappingFromDictionariesOnToComplexTypes.cs" />
110111
<Compile Include="Dictionaries\WhenMappingFromDictionariesOnToEnumerableMembers.cs" />

0 commit comments

Comments
 (0)