Browse Source

Resolved #1705: Replace AddProfile by AddMaps for AbpAutoMapperOptions

pull/1710/head
Halil İbrahim Kalkan 7 years ago
parent
commit
2292cf67f6
  1. 35
      framework/src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AbpAutoMapperOptions.cs
  2. 4
      framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/AutoMapper_ConfigurationValidation_Tests.cs

35
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<Profile>();
}
public void AddProfile<TProfile>(bool validate = false)
where TProfile: Profile, new()
public void AddMaps<TModule>(bool validate = false)
{
var assembly = typeof(TModule).Assembly;
Configurators.Add(context =>
{
context.MapperConfiguration.AddProfile<TProfile>();
context.MapperConfiguration.AddMaps(assembly);
});
if (validate)
{
ValidatingProfiles.Add<TProfile>();
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<TProfile>(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);
}
}
}

4
framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/AutoMapper_ConfigurationValidation_Tests.cs

@ -30,8 +30,8 @@ namespace Volo.Abp.AutoMapper
{
Configure<AbpAutoMapperOptions>(options =>
{
options.AddProfile<ValidatedProfile>(true);
options.AddProfile<NonValidatedProfile>();
options.AddMaps<TestModule>(validate: true); //Adds all profiles in the TestModule assembly by validating configurations
options.ValidateProfile<NonValidatedProfile>(validate: false); //Exclude a profile from the configuration validation
});
}
}

Loading…
Cancel
Save