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))]
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<IOptions<AbpAutoMapperOptions>>().Value;
var options = scope.ServiceProvider.GetRequiredService<IOptions<AbpAutoMapperOptions>>().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<MapperAccessor>().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<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 bool UseStaticMapper { get; set; }
public AbpAutoMapperOptions()
{
UseStaticMapper = true;
Configurators = new List<Action<IAbpAutoMapperConfigurationContext>>();
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 =>
{
options.UseStaticMapper = false;
options.AddProfile<ValidatedProfile>(true);
options.AddProfile<NonValidatedProfile>();
});

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