Skip to content

Commit c94a7c1

Browse files
Copilotjbogard
andcommitted
Address review feedback: fix double semicolon, add DI condition test, update docs
Co-authored-by: jbogard <104498+jbogard@users.noreply.github.com>
1 parent 493e864 commit c94a7c1

File tree

7 files changed

+88
-12
lines changed

7 files changed

+88
-12
lines changed

docs/source/Dependency-injection.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,22 @@ public class MyCondition : ICondition<Source, Destination, int>
7878
}
7979
}
8080

81-
var configuration = new MapperConfiguration(cfg =>
81+
public class ConditionProfile : Profile
8282
{
83-
cfg.CreateMap<Source, Destination>()
84-
.ForMember(d => d.Value, o =>
85-
{
86-
o.Condition<MyCondition>();
87-
o.MapFrom(s => s.Value);
88-
});
89-
}, loggerFactory);
83+
public ConditionProfile()
84+
{
85+
CreateMap<Source, Destination>()
86+
.ForMember(d => d.Value, o =>
87+
{
88+
o.Condition<MyCondition>();
89+
o.MapFrom(s => s.Value);
90+
});
91+
}
92+
}
93+
94+
// In Startup.cs / Program.cs:
95+
services.AddTransient<IMyService, MyService>();
96+
services.AddAutoMapper(typeof(ConditionProfile).Assembly);
9097
```
9198

9299
Or dynamic service location, to be used in the case of instance-based containers (including child/nested containers):

src/AutoMapper.DI.Tests/AppDomainResolutionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void ShouldResolveConfiguration()
3030
[Fact]
3131
public void ShouldConfigureProfiles()
3232
{
33-
_provider.GetService<IConfigurationProvider>().Internal().GetAllTypeMaps().Count.ShouldBe(4);
33+
_provider.GetService<IConfigurationProvider>().Internal().GetAllTypeMaps().Count.ShouldBe(5);
3434
}
3535

3636
[Fact]

src/AutoMapper.DI.Tests/AssemblyResolutionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void ShouldResolveConfiguration()
3737
[Fact]
3838
public void ShouldConfigureProfiles()
3939
{
40-
_provider.GetService<IConfigurationProvider>().Internal().GetAllTypeMaps().Count.ShouldBe(4);
40+
_provider.GetService<IConfigurationProvider>().Internal().GetAllTypeMaps().Count.ShouldBe(5);
4141
}
4242

4343
[Fact]

src/AutoMapper.DI.Tests/DependencyTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,40 @@ public void ShouldConvertWithDependency()
4141
dest.ConvertedValue.ShouldBe(10);
4242
}
4343
}
44+
45+
public class ConditionDependencyTests
46+
{
47+
private readonly IServiceProvider _provider;
48+
49+
public ConditionDependencyTests()
50+
{
51+
IServiceCollection services = new ServiceCollection();
52+
services.AddTransient<ISomeService>(sp => new FooService(5));
53+
services.AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance);
54+
services.AddAutoMapper(_ => { }, typeof(ConditionSource));
55+
_provider = services.BuildServiceProvider();
56+
57+
_provider.GetService<IConfigurationProvider>().AssertConfigurationIsValid();
58+
}
59+
60+
[Fact]
61+
public void ShouldApplyConditionWithDependency_WhenConditionPasses()
62+
{
63+
var mapper = _provider.GetService<IMapper>();
64+
// FooService.Modify(10) = 10 + 5 = 15 > 0, condition passes
65+
var dest = mapper.Map<ConditionSource, ConditionDest>(new ConditionSource { Value = 10 });
66+
67+
dest.Value.ShouldBe(10);
68+
}
69+
70+
[Fact]
71+
public void ShouldSkipMappingWithDependency_WhenConditionFails()
72+
{
73+
var mapper = _provider.GetService<IMapper>();
74+
// FooService.Modify(-10) = -10 + 5 = -5, which is not > 0, condition fails
75+
var dest = mapper.Map<ConditionSource, ConditionDest>(new ConditionSource { Value = -10 });
76+
77+
dest.Value.ShouldBe(0);
78+
}
79+
}
4480
}

src/AutoMapper.DI.Tests/Profiles.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,37 @@ internal class DependencyValueConverter : IValueConverter<int, int>
150150
public int Convert(int sourceMember, ResolutionContext context)
151151
=> _service.Modify(sourceMember);
152152
}
153+
154+
public class ConditionSource
155+
{
156+
public int Value { get; set; }
157+
}
158+
159+
public class ConditionDest
160+
{
161+
public int Value { get; set; }
162+
}
163+
164+
public class DependencyCondition : ICondition<ConditionSource, ConditionDest, int>
165+
{
166+
private readonly ISomeService _service;
167+
168+
public DependencyCondition(ISomeService service) => _service = service;
169+
170+
public bool Evaluate(ConditionSource source, ConditionDest destination, int sourceMember, int destMember, ResolutionContext context)
171+
=> _service.Modify(sourceMember) > 0;
172+
}
173+
174+
internal class ConditionProfile : Profile
175+
{
176+
public ConditionProfile()
177+
{
178+
CreateMap<ConditionSource, ConditionDest>()
179+
.ForMember(d => d.Value, o =>
180+
{
181+
o.Condition<DependencyCondition>();
182+
o.MapFrom(s => s.Value);
183+
});
184+
}
185+
}
153186
}

src/AutoMapper.DI.Tests/TypeResolutionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void ShouldResolveConfiguration()
3333
[Fact]
3434
public void ShouldConfigureProfiles()
3535
{
36-
_provider.GetService<IConfigurationProvider>().Internal().GetAllTypeMaps().Count.ShouldBe(4);
36+
_provider.GetService<IConfigurationProvider>().Internal().GetAllTypeMaps().Count.ShouldBe(5);
3737
}
3838

3939
[Fact]

src/AutoMapper/Licensing/LicenseAccessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ private Claim[] ValidateKey(string licenseKey)
5454
Exponent = Convert.FromBase64String("AQAB"),
5555
Modulus = Convert.FromBase64String(
5656
"2LTtdJV2b0mYoRqChRCfcqnbpKvsiCcDYwJ+qPtvQXWXozOhGo02/V0SWMFBdbZHUzpEytIiEcojo7Vbq5mQmt4lg92auyPKsWq6qSmCVZCUuL/kpYqLCit4yUC0YqZfw4H9zLf1yAIOgyXQf1x6g+kscDo1pWAniSl9a9l/LXRVEnGz+OfeUrN/5gzpracGUY6phx6T09UCRuzi4YqqO4VJzL877W0jCW2Q7jMzHxOK04VSjNc22CADuCd34mrFs23R0vVm1DVLYtPGD76/rGOcxO6vmRc7ydBAvt1IoUsrY0vQ2rahp51YPxqqhKPd8nNOomHWblCCA7YUeV3C1Q==")
57-
}; ;
57+
};
5858

5959
var key = new RsaSecurityKey(rsa)
6060
{

0 commit comments

Comments
 (0)