diff --git a/docs/source/Dependency-injection.md b/docs/source/Dependency-injection.md index 2a707e9e01..b4a5c8ca3a 100644 --- a/docs/source/Dependency-injection.md +++ b/docs/source/Dependency-injection.md @@ -78,15 +78,22 @@ public class MyCondition : ICondition } } -var configuration = new MapperConfiguration(cfg => +public class ConditionProfile : Profile { - cfg.CreateMap() - .ForMember(d => d.Value, o => - { - o.Condition(); - o.MapFrom(s => s.Value); - }); -}, loggerFactory); + public ConditionProfile() + { + CreateMap() + .ForMember(d => d.Value, o => + { + o.Condition(); + o.MapFrom(s => s.Value); + }); + } +} + +// In Startup.cs / Program.cs: +services.AddTransient(); +services.AddAutoMapper(typeof(ConditionProfile).Assembly); ``` Or dynamic service location, to be used in the case of instance-based containers (including child/nested containers): diff --git a/src/AutoMapper.DI.Tests/AppDomainResolutionTests.cs b/src/AutoMapper.DI.Tests/AppDomainResolutionTests.cs index e8f76b9377..3a0dee5ac2 100644 --- a/src/AutoMapper.DI.Tests/AppDomainResolutionTests.cs +++ b/src/AutoMapper.DI.Tests/AppDomainResolutionTests.cs @@ -30,7 +30,7 @@ public void ShouldResolveConfiguration() [Fact] public void ShouldConfigureProfiles() { - _provider.GetService().Internal().GetAllTypeMaps().Count.ShouldBe(4); + _provider.GetService().Internal().GetAllTypeMaps().Count.ShouldBe(5); } [Fact] diff --git a/src/AutoMapper.DI.Tests/AssemblyResolutionTests.cs b/src/AutoMapper.DI.Tests/AssemblyResolutionTests.cs index 9cb6d5a053..97f516b0ce 100644 --- a/src/AutoMapper.DI.Tests/AssemblyResolutionTests.cs +++ b/src/AutoMapper.DI.Tests/AssemblyResolutionTests.cs @@ -37,7 +37,7 @@ public void ShouldResolveConfiguration() [Fact] public void ShouldConfigureProfiles() { - _provider.GetService().Internal().GetAllTypeMaps().Count.ShouldBe(4); + _provider.GetService().Internal().GetAllTypeMaps().Count.ShouldBe(5); } [Fact] diff --git a/src/AutoMapper.DI.Tests/DependencyTests.cs b/src/AutoMapper.DI.Tests/DependencyTests.cs index c897c4e862..9756f6378b 100644 --- a/src/AutoMapper.DI.Tests/DependencyTests.cs +++ b/src/AutoMapper.DI.Tests/DependencyTests.cs @@ -41,4 +41,40 @@ public void ShouldConvertWithDependency() dest.ConvertedValue.ShouldBe(10); } } + + public class ConditionDependencyTests + { + private readonly IServiceProvider _provider; + + public ConditionDependencyTests() + { + IServiceCollection services = new ServiceCollection(); + services.AddTransient(sp => new FooService(5)); + services.AddSingleton(NullLoggerFactory.Instance); + services.AddAutoMapper(_ => { }, typeof(ConditionSource)); + _provider = services.BuildServiceProvider(); + + _provider.GetService().AssertConfigurationIsValid(); + } + + [Fact] + public void ShouldApplyConditionWithDependency_WhenConditionPasses() + { + var mapper = _provider.GetService(); + // FooService.Modify(10) = 10 + 5 = 15 > 0, condition passes + var dest = mapper.Map(new ConditionSource { Value = 10 }); + + dest.Value.ShouldBe(10); + } + + [Fact] + public void ShouldSkipMappingWithDependency_WhenConditionFails() + { + var mapper = _provider.GetService(); + // FooService.Modify(-10) = -10 + 5 = -5, which is not > 0, condition fails + var dest = mapper.Map(new ConditionSource { Value = -10 }); + + dest.Value.ShouldBe(0); + } + } } diff --git a/src/AutoMapper.DI.Tests/Profiles.cs b/src/AutoMapper.DI.Tests/Profiles.cs index 5e9b14e61e..d61cbdebab 100644 --- a/src/AutoMapper.DI.Tests/Profiles.cs +++ b/src/AutoMapper.DI.Tests/Profiles.cs @@ -150,4 +150,37 @@ internal class DependencyValueConverter : IValueConverter public int Convert(int sourceMember, ResolutionContext context) => _service.Modify(sourceMember); } + + public class ConditionSource + { + public int Value { get; set; } + } + + public class ConditionDest + { + public int Value { get; set; } + } + + public class DependencyCondition : ICondition + { + private readonly ISomeService _service; + + public DependencyCondition(ISomeService service) => _service = service; + + public bool Evaluate(ConditionSource source, ConditionDest destination, int sourceMember, int destMember, ResolutionContext context) + => _service.Modify(sourceMember) > 0; + } + + internal class ConditionProfile : Profile + { + public ConditionProfile() + { + CreateMap() + .ForMember(d => d.Value, o => + { + o.Condition(); + o.MapFrom(s => s.Value); + }); + } + } } \ No newline at end of file diff --git a/src/AutoMapper.DI.Tests/TypeResolutionTests.cs b/src/AutoMapper.DI.Tests/TypeResolutionTests.cs index 924d62d2ab..7e3a6c4df0 100644 --- a/src/AutoMapper.DI.Tests/TypeResolutionTests.cs +++ b/src/AutoMapper.DI.Tests/TypeResolutionTests.cs @@ -33,7 +33,7 @@ public void ShouldResolveConfiguration() [Fact] public void ShouldConfigureProfiles() { - _provider.GetService().Internal().GetAllTypeMaps().Count.ShouldBe(4); + _provider.GetService().Internal().GetAllTypeMaps().Count.ShouldBe(5); } [Fact] diff --git a/src/AutoMapper/Licensing/LicenseAccessor.cs b/src/AutoMapper/Licensing/LicenseAccessor.cs index d56a0159b6..bae3bf6b73 100644 --- a/src/AutoMapper/Licensing/LicenseAccessor.cs +++ b/src/AutoMapper/Licensing/LicenseAccessor.cs @@ -54,7 +54,7 @@ private Claim[] ValidateKey(string licenseKey) Exponent = Convert.FromBase64String("AQAB"), Modulus = Convert.FromBase64String( "2LTtdJV2b0mYoRqChRCfcqnbpKvsiCcDYwJ+qPtvQXWXozOhGo02/V0SWMFBdbZHUzpEytIiEcojo7Vbq5mQmt4lg92auyPKsWq6qSmCVZCUuL/kpYqLCit4yUC0YqZfw4H9zLf1yAIOgyXQf1x6g+kscDo1pWAniSl9a9l/LXRVEnGz+OfeUrN/5gzpracGUY6phx6T09UCRuzi4YqqO4VJzL877W0jCW2Q7jMzHxOK04VSjNc22CADuCd34mrFs23R0vVm1DVLYtPGD76/rGOcxO6vmRc7ydBAvt1IoUsrY0vQ2rahp51YPxqqhKPd8nNOomHWblCCA7YUeV3C1Q==") - }; ; + }; var key = new RsaSecurityKey(rsa) {