From 797b011c9391a67f2195861c890ab4bc4eea7c5e Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 4 Jul 2019 13:19:02 +0800 Subject: [PATCH] Remove the static API from the Automapper module. --- .../Abp/AutoMapper/AbpAutoMapperModule.cs | 64 ++++++------------- .../Abp/AutoMapper/AbpAutoMapperOptions.cs | 3 - .../Volo/Abp/AutoMapper/AutoMapExtensions.cs | 32 ---------- ...utoMapper_ConfigurationValidation_Tests.cs | 2 - .../Volo/Abp/AutoMapper/AutofacTestModule.cs | 5 +- 5 files changed, 20 insertions(+), 86 deletions(-) delete mode 100644 framework/src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AutoMapExtensions.cs 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 0c31fa31ce..152e4f9283 100644 --- a/framework/src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AbpAutoMapperModule.cs +++ b/framework/src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AbpAutoMapperModule.cs @@ -14,9 +14,6 @@ namespace Volo.Abp.AutoMapper [DependsOn(typeof(AbpObjectMappingModule))] public class AbpAutoMapperModule : AbpModule { - private static volatile bool _createdMappingsBefore; - private static readonly object SyncObj = new object(); - public override void ConfigureServices(ServiceConfigurationContext context) { var mapperAccessor = new MapperAccessor(); @@ -31,58 +28,35 @@ namespace Volo.Abp.AutoMapper private void CreateMappings(IServiceProvider serviceProvider) { - lock (SyncObj) + using (var scope = serviceProvider.CreateScope()) { - using (var scope = serviceProvider.CreateScope()) - { - var options = scope.ServiceProvider.GetRequiredService>().Value; + var options = scope.ServiceProvider.GetRequiredService>().Value; - void ConfigureAll(IAbpAutoMapperConfigurationContext ctx) + void ConfigureAll(IAbpAutoMapperConfigurationContext ctx) + { + FindAndAutoMapTypes(ctx); + foreach (var configurator in options.Configurators) { - FindAndAutoMapTypes(ctx); - foreach (var configurator in options.Configurators) - { - configurator(ctx); - } + configurator(ctx); } + } - void ValidateAll(IConfigurationProvider config) + void ValidateAll(IConfigurationProvider config) + { + foreach (var profileType in options.ValidatingProfiles) { - foreach (var profileType in options.ValidatingProfiles) - { - config.AssertConfigurationIsValid(((Profile)Activator.CreateInstance(profileType)).ProfileName); - } + config.AssertConfigurationIsValid(((Profile)Activator.CreateInstance(profileType)).ProfileName); } + } - if (options.UseStaticMapper) - { - //We should prevent duplicate mapping in an application, since Mapper is static. - if (!_createdMappingsBefore) - { - _createdMappingsBefore = true; - - Mapper.Initialize(mapperConfigurationExpression => - { - ConfigureAll(new AbpAutoMapperConfigurationContext(mapperConfigurationExpression, scope.ServiceProvider)); - }); - - ValidateAll(Mapper.Configuration); - } - - scope.ServiceProvider.GetRequiredService().Mapper = Mapper.Instance; - } - else - { - var config = new MapperConfiguration(mapperConfigurationExpression => - { - ConfigureAll(new AbpAutoMapperConfigurationContext(mapperConfigurationExpression, scope.ServiceProvider)); - }); + var mapperConfiguration = new MapperConfiguration(mapperConfigurationExpression => + { + ConfigureAll(new AbpAutoMapperConfigurationContext(mapperConfigurationExpression, scope.ServiceProvider)); + }); - ValidateAll(config); + ValidateAll(mapperConfiguration); - scope.ServiceProvider.GetRequiredService().Mapper = config.CreateMapper(); - } - } + scope.ServiceProvider.GetRequiredService().Mapper = mapperConfiguration.CreateMapper(); } } diff --git a/framework/src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AbpAutoMapperOptions.cs b/framework/src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AbpAutoMapperOptions.cs index 65a5cf211f..cdd407e03c 100644 --- a/framework/src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AbpAutoMapperOptions.cs +++ b/framework/src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AbpAutoMapperOptions.cs @@ -11,11 +11,8 @@ namespace Volo.Abp.AutoMapper public ITypeList ValidatingProfiles { get; set; } - public bool UseStaticMapper { get; set; } - public AbpAutoMapperOptions() { - UseStaticMapper = true; Configurators = new List>(); ValidatingProfiles = new TypeList(); } diff --git a/framework/src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AutoMapExtensions.cs b/framework/src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AutoMapExtensions.cs deleted file mode 100644 index 32c0ad93fa..0000000000 --- a/framework/src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AutoMapExtensions.cs +++ /dev/null @@ -1,32 +0,0 @@ -using AutoMapper; - -namespace Volo.Abp.AutoMapper -{ - public static class AutoMapExtensions - { - /// - /// Converts an object to another using AutoMapper library. Creates a new object of . - /// There must be a mapping between objects before calling this method. - /// - /// Type of the destination object - /// Source object - public static TDestination MapTo(this object source) - { - return Mapper.Map(source); - } - - /// - /// Execute a mapping from the source object to the existing destination object - /// There must be a mapping between objects before calling this method. - /// - /// Source type - /// Destination type - /// Source object - /// Destination object - /// - public static TDestination MapTo(this TSource source, TDestination destination) - { - return Mapper.Map(source, destination); - } - } -} diff --git a/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/AutoMapper_ConfigurationValidation_Tests.cs b/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/AutoMapper_ConfigurationValidation_Tests.cs index b0ecff941a..dba63ed8c9 100644 --- a/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/AutoMapper_ConfigurationValidation_Tests.cs +++ b/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/AutoMapper_ConfigurationValidation_Tests.cs @@ -30,8 +30,6 @@ namespace Volo.Abp.AutoMapper { Configure(options => { - options.UseStaticMapper = false; - options.AddProfile(true); options.AddProfile(); }); diff --git a/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/AutofacTestModule.cs b/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/AutofacTestModule.cs index 86eade5db8..dbac30a805 100644 --- a/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/AutofacTestModule.cs +++ b/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/AutofacTestModule.cs @@ -8,10 +8,7 @@ namespace Volo.Abp.AutoMapper { public override void ConfigureServices(ServiceConfigurationContext context) { - Configure(options => - { - options.UseStaticMapper = false; - }); + } } } \ No newline at end of file