From 2292cf67f6ab7897119cffa3d92249a723d04291 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Mon, 2 Sep 2019 19:57:36 +0300 Subject: [PATCH] Resolved #1705: Replace AddProfile by AddMaps for AbpAutoMapperOptions --- .../Abp/AutoMapper/AbpAutoMapperOptions.cs | 35 ++++++++++++++++--- ...utoMapper_ConfigurationValidation_Tests.cs | 4 +-- 2 files changed, 33 insertions(+), 6 deletions(-) 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 cdd407e03c..742cc91d42 100644 --- a/framework/src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AbpAutoMapperOptions.cs +++ b/framework/src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AbpAutoMapperOptions.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using AutoMapper; using Volo.Abp.Collections; @@ -17,17 +18,43 @@ namespace Volo.Abp.AutoMapper ValidatingProfiles = new TypeList(); } - public void AddProfile(bool validate = false) - where TProfile: Profile, new() + public void AddMaps(bool validate = false) { + var assembly = typeof(TModule).Assembly; + Configurators.Add(context => { - context.MapperConfiguration.AddProfile(); + context.MapperConfiguration.AddMaps(assembly); }); if (validate) { - ValidatingProfiles.Add(); + var profileTypes = assembly + .DefinedTypes + .Where(type => typeof(Profile).IsAssignableFrom(type) && !type.IsAbstract && !type.IsGenericType); + + foreach (var profileType in profileTypes) + { + ValidatingProfiles.Add(profileType); + } + } + } + + public void ValidateProfile(bool validate = true) + where TProfile : Profile + { + ValidateProfile(typeof(TProfile), validate); + } + + public void ValidateProfile(Type profileType, bool validate = true) + { + if (validate) + { + ValidatingProfiles.AddIfNotContains(profileType); + } + else + { + ValidatingProfiles.Remove(profileType); } } } 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 dba63ed8c9..7504bd7209 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,8 @@ namespace Volo.Abp.AutoMapper { Configure(options => { - options.AddProfile(true); - options.AddProfile(); + options.AddMaps(validate: true); //Adds all profiles in the TestModule assembly by validating configurations + options.ValidateProfile(validate: false); //Exclude a profile from the configuration validation }); } }