Remove the static API from the Automapper module.

pull/1430/head
maliming 6 years ago
parent 12f0d9bace
commit 797b011c93

@ -14,9 +14,6 @@ namespace Volo.Abp.AutoMapper
[DependsOn(typeof(AbpObjectMappingModule))] [DependsOn(typeof(AbpObjectMappingModule))]
public class AbpAutoMapperModule : AbpModule public class AbpAutoMapperModule : AbpModule
{ {
private static volatile bool _createdMappingsBefore;
private static readonly object SyncObj = new object();
public override void ConfigureServices(ServiceConfigurationContext context) public override void ConfigureServices(ServiceConfigurationContext context)
{ {
var mapperAccessor = new MapperAccessor(); var mapperAccessor = new MapperAccessor();
@ -30,8 +27,6 @@ namespace Volo.Abp.AutoMapper
} }
private void CreateMappings(IServiceProvider serviceProvider) private void CreateMappings(IServiceProvider serviceProvider)
{
lock (SyncObj)
{ {
using (var scope = serviceProvider.CreateScope()) using (var scope = serviceProvider.CreateScope())
{ {
@ -54,35 +49,14 @@ namespace Volo.Abp.AutoMapper
} }
} }
if (options.UseStaticMapper) var mapperConfiguration = new MapperConfiguration(mapperConfigurationExpression =>
{
//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<MapperAccessor>().Mapper = Mapper.Instance;
}
else
{
var config = new MapperConfiguration(mapperConfigurationExpression =>
{ {
ConfigureAll(new AbpAutoMapperConfigurationContext(mapperConfigurationExpression, scope.ServiceProvider)); ConfigureAll(new AbpAutoMapperConfigurationContext(mapperConfigurationExpression, scope.ServiceProvider));
}); });
ValidateAll(config); ValidateAll(mapperConfiguration);
scope.ServiceProvider.GetRequiredService<MapperAccessor>().Mapper = config.CreateMapper(); scope.ServiceProvider.GetRequiredService<MapperAccessor>().Mapper = mapperConfiguration.CreateMapper();
}
}
} }
} }

@ -11,11 +11,8 @@ namespace Volo.Abp.AutoMapper
public ITypeList<Profile> ValidatingProfiles { get; set; } public ITypeList<Profile> ValidatingProfiles { get; set; }
public bool UseStaticMapper { get; set; }
public AbpAutoMapperOptions() public AbpAutoMapperOptions()
{ {
UseStaticMapper = true;
Configurators = new List<Action<IAbpAutoMapperConfigurationContext>>(); Configurators = new List<Action<IAbpAutoMapperConfigurationContext>>();
ValidatingProfiles = new TypeList<Profile>(); ValidatingProfiles = new TypeList<Profile>();
} }

@ -1,32 +0,0 @@
using AutoMapper;
namespace Volo.Abp.AutoMapper
{
public static class AutoMapExtensions
{
/// <summary>
/// Converts an object to another using AutoMapper library. Creates a new object of <typeparamref name="TDestination"/>.
/// There must be a mapping between objects before calling this method.
/// </summary>
/// <typeparam name="TDestination">Type of the destination object</typeparam>
/// <param name="source">Source object</param>
public static TDestination MapTo<TDestination>(this object source)
{
return Mapper.Map<TDestination>(source);
}
/// <summary>
/// Execute a mapping from the source object to the existing destination object
/// There must be a mapping between objects before calling this method.
/// </summary>
/// <typeparam name="TSource">Source type</typeparam>
/// <typeparam name="TDestination">Destination type</typeparam>
/// <param name="source">Source object</param>
/// <param name="destination">Destination object</param>
/// <returns></returns>
public static TDestination MapTo<TSource, TDestination>(this TSource source, TDestination destination)
{
return Mapper.Map(source, destination);
}
}
}

@ -30,8 +30,6 @@ namespace Volo.Abp.AutoMapper
{ {
Configure<AbpAutoMapperOptions>(options => Configure<AbpAutoMapperOptions>(options =>
{ {
options.UseStaticMapper = false;
options.AddProfile<ValidatedProfile>(true); options.AddProfile<ValidatedProfile>(true);
options.AddProfile<NonValidatedProfile>(); options.AddProfile<NonValidatedProfile>();
}); });

@ -8,10 +8,7 @@ namespace Volo.Abp.AutoMapper
{ {
public override void ConfigureServices(ServiceConfigurationContext context) public override void ConfigureServices(ServiceConfigurationContext context)
{ {
Configure<AbpAutoMapperOptions>(options =>
{
options.UseStaticMapper = false;
});
} }
} }
} }
Loading…
Cancel
Save