Browse Source

DI support of AutoMapper

pull/6868/head
liangshiwei 6 years ago
parent
commit
03551252f6
  1. 20
      framework/src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AbpAutoMapperModule.cs

20
framework/src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AbpAutoMapperModule.cs

@ -13,7 +13,7 @@ namespace Volo.Abp.AutoMapper
typeof(AbpObjectMappingModule),
typeof(AbpObjectExtendingModule),
typeof(AbpAuditingModule)
)]
)]
public class AbpAutoMapperModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
@ -21,16 +21,11 @@ namespace Volo.Abp.AutoMapper
context.Services.AddAutoMapperObjectMapper();
var mapperAccessor = new MapperAccessor();
context.Services.AddSingleton<IMapperAccessor>(_ => mapperAccessor);
context.Services.AddSingleton<MapperAccessor>(_ => mapperAccessor);
context.Services.AddSingleton(_ => mapperAccessor);
context.Services.Add(new ServiceDescriptor(typeof(IMapperAccessor), CreateMappings, ServiceLifetime.Singleton));
}
public override void OnPreApplicationInitialization(ApplicationInitializationContext context)
{
CreateMappings(context.ServiceProvider);
}
private void CreateMappings(IServiceProvider serviceProvider)
private IMapperAccessor CreateMappings(IServiceProvider serviceProvider)
{
using (var scope = serviceProvider.CreateScope())
{
@ -48,7 +43,7 @@ namespace Volo.Abp.AutoMapper
{
foreach (var profileType in options.ValidatingProfiles)
{
config.AssertConfigurationIsValid(((Profile)Activator.CreateInstance(profileType)).ProfileName);
config.AssertConfigurationIsValid(((Profile) Activator.CreateInstance(profileType)).ProfileName);
}
}
@ -59,7 +54,10 @@ namespace Volo.Abp.AutoMapper
ValidateAll(mapperConfiguration);
scope.ServiceProvider.GetRequiredService<MapperAccessor>().Mapper = mapperConfiguration.CreateMapper();
var mapperAccessor = scope.ServiceProvider.GetRequiredService<MapperAccessor>();
mapperAccessor.Mapper = new Mapper(mapperConfiguration, serviceProvider.GetService);
return mapperAccessor;
}
}
}

Loading…
Cancel
Save