mirror of https://github.com/abpframework/abp.git
committed by
GitHub
3 changed files with 110 additions and 12 deletions
@ -0,0 +1,34 @@ |
|||
using System; |
|||
using System.Linq; |
|||
using AutoMapper; |
|||
using AutoMapper.Internal; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.DependencyInjection.Extensions; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.AutoMapper |
|||
{ |
|||
public class AbpAutoMapperConventionalRegistrar : ConventionalRegistrarBase |
|||
{ |
|||
protected readonly Type[] OpenTypes = { |
|||
typeof(IValueResolver<,,>), |
|||
typeof(IMemberValueResolver<,,,>), |
|||
typeof(ITypeConverter<,>), |
|||
typeof(IValueConverter<,>), |
|||
typeof(IMappingAction<,>) |
|||
}; |
|||
|
|||
public override void AddType(IServiceCollection services, Type type) |
|||
{ |
|||
if (IsConventionalRegistrationDisabled(type)) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
if (type.IsClass && !type.IsAbstract && OpenTypes.Any(type.ImplementsGenericInterface)) |
|||
{ |
|||
services.TryAddTransient(type); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,62 @@ |
|||
using AutoMapper; |
|||
using Shouldly; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Testing; |
|||
using Xunit; |
|||
using IObjectMapper = Volo.Abp.ObjectMapping.IObjectMapper; |
|||
|
|||
namespace Volo.Abp.AutoMapper |
|||
{ |
|||
public class AutoMapper_Dependency_Injection_Tests : AbpIntegratedTest<AutoMapperTestModule> |
|||
{ |
|||
private readonly IObjectMapper _objectMapper; |
|||
|
|||
public AutoMapper_Dependency_Injection_Tests() |
|||
{ |
|||
_objectMapper = GetRequiredService<IObjectMapper>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_Registered_AutoMapper_Service() |
|||
{ |
|||
GetService<CustomMappingActionService>().ShouldNotBeNull(); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Custom_MappingAction_Test() |
|||
{ |
|||
var sourceModel = new SourceModel |
|||
{ |
|||
Name = "Source" |
|||
}; |
|||
|
|||
_objectMapper.Map<SourceModel, DestModel>(sourceModel).Name.ShouldBe(nameof(CustomMappingActionService)); |
|||
} |
|||
|
|||
public class SourceModel |
|||
{ |
|||
public string Name { get; set; } |
|||
} |
|||
|
|||
public class DestModel |
|||
{ |
|||
public string Name { get; set; } |
|||
} |
|||
|
|||
public class MapperActionProfile : Profile |
|||
{ |
|||
public MapperActionProfile() |
|||
{ |
|||
CreateMap<SourceModel, DestModel>().AfterMap<CustomMappingActionService>(); |
|||
} |
|||
} |
|||
|
|||
public class CustomMappingActionService : IMappingAction<SourceModel, DestModel> |
|||
{ |
|||
public void Process(SourceModel source, DestModel destination, ResolutionContext context) |
|||
{ |
|||
destination.Name = nameof(CustomMappingActionService); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue