Allow custom services construction

pull/7592/head
liangshiwei 5 years ago
parent 22a5c0ead9
commit 45f778cb94

@ -25,7 +25,7 @@ namespace Volo.Abp.AutoMapper
{
context.Services.AddAutoMapperObjectMapper();
context.Services.AddSingleton<MapperAccessor>(provider => CreateMappings(provider));
context.Services.AddSingleton<MapperAccessor>(CreateMappings);
context.Services.AddSingleton<IMapperAccessor>(provider => provider.GetRequiredService<MapperAccessor>());
}
@ -43,6 +43,8 @@ namespace Volo.Abp.AutoMapper
}
}
options.Configurators.Insert(0, ctx => ctx.MapperConfiguration.ConstructServicesUsing(serviceProvider.GetService));
void ValidateAll(IConfigurationProvider config)
{
foreach (var profileType in options.ValidatingProfiles)
@ -60,7 +62,7 @@ namespace Volo.Abp.AutoMapper
return new MapperAccessor
{
Mapper = new Mapper(mapperConfiguration, serviceProvider.GetService)
Mapper = new Mapper(mapperConfiguration)
};
}
}

@ -0,0 +1,74 @@
using System;
using AutoMapper;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Volo.Abp.AutoMapper.SampleClasses;
using Volo.Abp.Modularity;
using Volo.Abp.Testing;
using Xunit;
using IObjectMapper = Volo.Abp.ObjectMapping.IObjectMapper;
namespace Volo.Abp.AutoMapper
{
public class AutoMapper_CustomServiceConstruction_Tests : AbpIntegratedTest<AutoMapper_CustomServiceConstruction_Tests.TestModule>
{
private readonly IObjectMapper _objectMapper;
public AutoMapper_CustomServiceConstruction_Tests()
{
_objectMapper = ServiceProvider.GetRequiredService<IObjectMapper>();
}
[Fact]
public void Should_Custom_Service_Construction()
{
var myEntity = new MyEntity
{
Number = 2
};
_objectMapper.Map<MyEntity, MyEntityDto>(myEntity).Number.ShouldBe(1);
}
[DependsOn(typeof(AbpAutoMapperModule))]
public class TestModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
Configure<AbpAutoMapperOptions>(options =>
{
options.AddMaps<TestModule>();
options.Configurators.Add(configurationContext =>
{
configurationContext.MapperConfiguration.ConstructServicesUsing(type =>
type.Name.Contains(nameof(CustomMappingAction))
? new CustomMappingAction(1)
: Activator.CreateInstance(type));
});
});
}
}
public class MapperActionProfile : Profile
{
public MapperActionProfile()
{
CreateMap<MyEntity, MyEntityDto>().AfterMap<CustomMappingAction>();
}
}
public class CustomMappingAction : IMappingAction<MyEntity, MyEntityDto>
{
private readonly int _number;
public CustomMappingAction(int number)
{
_number = number;
}
public void Process(MyEntity source, MyEntityDto destination, ResolutionContext context)
{
destination.Number = _number;
}
}
}
}

@ -19,7 +19,7 @@ namespace Volo.Abp.AutoMapper
[Fact]
public void Should_Registered_AutoMapper_Service()
{
GetService<CustomMappingActionService>().ShouldNotBeNull();
GetService<CustomMappingAction>().ShouldNotBeNull();
}
[Fact]
@ -47,15 +47,30 @@ namespace Volo.Abp.AutoMapper
{
public MapperActionProfile()
{
CreateMap<SourceModel, DestModel>().AfterMap<CustomMappingActionService>();
CreateMap<SourceModel, DestModel>().AfterMap<CustomMappingAction>();
}
}
public class CustomMappingActionService : IMappingAction<SourceModel, DestModel>
public class CustomMappingAction : IMappingAction<SourceModel, DestModel>
{
private readonly CustomMappingActionService _customMappingActionService;
public CustomMappingAction(CustomMappingActionService customMappingActionService)
{
_customMappingActionService = customMappingActionService;
}
public void Process(SourceModel source, DestModel destination, ResolutionContext context)
{
destination.Name = nameof(CustomMappingActionService);
destination.Name = _customMappingActionService.GetName();
}
}
public class CustomMappingActionService : ITransientDependency
{
public string GetName()
{
return nameof(CustomMappingActionService);
}
}
}

Loading…
Cancel
Save