【发布时间】:2015-05-18 18:42:45
【问题描述】:
我使用以下答案中描述的以下方法来创建映射器的实例:
var platformSpecificRegistry = AutoMapper.Internal.PlatformAdapter.Resolve<IPlatformSpecificMapperRegistry>();
platformSpecificRegistry.Initialize();
var autoMapperCfg = new AutoMapper.ConfigurationStore(new TypeMapFactory(), AutoMapper.Mappers.MapperRegistry.Mappers);
var mappingEngine = new AutoMapper.MappingEngine(_autoMapperCfg);
如以下问题所述:
AutoMapper How To Map Object A To Object B Differently Depending On Context.
我如何能够使用[重用]Automapper profile 类来创建映射器的实例?
public class ApiTestClassToTestClassMappingProfile : Profile
{
protected override void Configure()
{
base.Configure();
Mapper.CreateMap<ApiTestClass, TestClass>()
.IgnoreAllNonExisting();
}
}
只是为了让您了解我为什么需要这样的功能: 我使用以下方法将所有 Automapper Profile 类注册到我的 IoC 容器 [CastleWindsor] 中:
IoC.WindsorContainer.Register(Types.FromThisAssembly()
.BasedOn<Profile>()
.WithServiceBase()
.Configure(c => c.LifeStyle.Is(LifestyleType.Singleton)));
var profiles = IoC.WindsorContainer.ResolveAll<Profile>();
foreach (var profile in profiles)
{
Mapper.AddProfile(profile);
}
IoC.WindsorContainer.Register(Component.For<IMappingEngine>().Instance(Mapper.Engine));
虽然上面完全满足了初始化我的静态 Mapper 类的需要,但我真的不知道如何重用我的 Automapper 配置文件类来创建实例映射器 [使用非静态映射器]。
【问题讨论】:
标签: c# castle-windsor automapper ioc-container