diff --git a/framework/src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AbpAutoMapperModule.cs b/framework/src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AbpAutoMapperModule.cs index 479173b72d..25707e3899 100644 --- a/framework/src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AbpAutoMapperModule.cs +++ b/framework/src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AbpAutoMapperModule.cs @@ -25,12 +25,11 @@ namespace Volo.Abp.AutoMapper { context.Services.AddAutoMapperObjectMapper(); - var mapperAccessor = new MapperAccessor(); - context.Services.AddSingleton(_ => mapperAccessor); - context.Services.Add(new ServiceDescriptor(typeof(IMapperAccessor), CreateMappings, ServiceLifetime.Singleton)); + context.Services.AddSingleton(provider => CreateMappings(provider)); + context.Services.AddSingleton(provider => provider.GetRequiredService()); } - private IMapperAccessor CreateMappings(IServiceProvider serviceProvider) + private MapperAccessor CreateMappings(IServiceProvider serviceProvider) { using (var scope = serviceProvider.CreateScope()) { @@ -59,10 +58,10 @@ namespace Volo.Abp.AutoMapper ValidateAll(mapperConfiguration); - var mapperAccessor = scope.ServiceProvider.GetRequiredService(); - mapperAccessor.Mapper = new Mapper(mapperConfiguration, serviceProvider.GetService); - - return mapperAccessor; + return new MapperAccessor + { + Mapper = new Mapper(mapperConfiguration, serviceProvider.GetService) + }; } } } diff --git a/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/AutoMapper_Dependency_Injection_Tests.cs b/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/AutoMapper_Dependency_Injection_Tests.cs new file mode 100644 index 0000000000..891ef0ddbb --- /dev/null +++ b/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/AutoMapper_Dependency_Injection_Tests.cs @@ -0,0 +1,111 @@ +using AutoMapper; +using Shouldly; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Testing; +using Xunit; +using IObjectMapper = Volo.Abp.ObjectMapping.IObjectMapper; + +namespace Volo.Abp.AutoMapper +{ + public class AutoMapper_Dependency_Injection_Tests : AbpIntegratedTest + { + private readonly IObjectMapper _objectMapper; + + public AutoMapper_Dependency_Injection_Tests() + { + _objectMapper = GetRequiredService(); + } + + [Fact] + public void Should_Registered_AutoMapper_Service() + { + GetService>().ShouldBeNull(); + GetService>().ShouldBeNull(); + GetService>().ShouldBeNull(); + GetService>().ShouldBeNull(); + GetService>().ShouldBeNull(); + } + + [Fact] + public void Custom_MappingAction_Test() + { + var sourceModel = new SourceModel + { + Name = "Source" + }; + + _objectMapper.Map(sourceModel).Name.ShouldBe(GetRequiredService().Name); + } + + public class SourceModel + { + public string Name { get; set; } + } + + public class DestModel + { + public string Name { get; set; } + } + + public class TestService : + IValueResolver, + IMemberValueResolver, + ITypeConverter, + IValueConverter, + IMappingAction + { + public string Resolve(string source, string destination, string destMember, ResolutionContext context) + { + return source; + } + + public string Resolve(string source, string destination, string sourceMember, string destMember, ResolutionContext context) + { + return source; + } + + public string Convert(string source, string destination, ResolutionContext context) + { + return source; + } + + public string Convert(string sourceMember, ResolutionContext context) + { + return sourceMember; + } + + public void Process(string source, string destination, ResolutionContext context) + { + + } + } + + public class MapperActionProfile : Profile + { + public MapperActionProfile() + { + CreateMap().AfterMap(); + } + } + + public class CustomMappingAction : IMappingAction + { + private readonly TestNameService _testNameService; + + public CustomMappingAction(TestNameService testNameService) + { + _testNameService = testNameService; + } + + public void Process(SourceModel source, DestModel destination, ResolutionContext context) + { + destination.Name = _testNameService.Name; + } + } + + public class TestNameService : ITransientDependency + { + public string Name => nameof(TestNameService); + } + } +} diff --git a/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/AutoMapper_MapperAction_Tests.cs b/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/AutoMapper_MapperAction_Tests.cs deleted file mode 100644 index aa1324927a..0000000000 --- a/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/AutoMapper_MapperAction_Tests.cs +++ /dev/null @@ -1,87 +0,0 @@ -using AutoMapper; -using Microsoft.Extensions.DependencyInjection; -using Shouldly; -using Volo.Abp.DependencyInjection; -using Volo.Abp.Testing; -using Xunit; -using IObjectMapper = Volo.Abp.ObjectMapping.IObjectMapper; - -namespace Volo.Abp.AutoMapper -{ - public class AutoMapper_MapperAction_Tests : AbpIntegratedTest - { - private readonly IObjectMapper _objectMapper; - - public AutoMapper_MapperAction_Tests() - { - _objectMapper = ServiceProvider.GetRequiredService(); - } - - [Fact] - public void Should_Name_DIMappingAction() - { - var mapperActionEntity = new MapperActionModel {Name = "MapperActionEntity"}; - _objectMapper.Map(mapperActionEntity) - .Name.ShouldBe(nameof(DIMappingAction)); - } - - [Fact] - public void Should_Name_NotDIMappingAction() - { - var mapperActionEntity = new MapperActionModel2 {Name = "MapperActionEntity"}; - _objectMapper.Map(mapperActionEntity) - .Name.ShouldBe(nameof(NotDIMapperAction)); - } - - public class MapperActionModel - { - public string Name { get; set; } - } - - public class MapperActionModel2 - { - public string Name { get; set; } - } - - public class MapperActionProfile : Profile - { - public MapperActionProfile() - { - CreateMap().AfterMap(); - CreateMap().AfterMap(); - } - } - - public class DIMappingAction : IMappingAction - { - private readonly MapperActionService _mapperActionService; - - public DIMappingAction(MapperActionService mapperActionService) - { - _mapperActionService = mapperActionService; - } - - public void Process(MapperActionModel source, MapperActionModel2 destination, ResolutionContext context) - { - destination.Name = _mapperActionService.GetName(); - } - } - - public class NotDIMapperAction : IMappingAction - { - public void Process(MapperActionModel2 source, MapperActionModel destination, - ResolutionContext context) - { - destination.Name = nameof(NotDIMapperAction); - } - } - - public class MapperActionService : ITransientDependency - { - public string GetName() - { - return nameof(DIMappingAction); - } - } - } -}