2
我想用CastleWindsor註冊AutoMapper 5.1.1,但我不知道在哪裏正確調用Mapper.Initialize()。註冊AutoMapper 5.1.1與溫莎城堡
AutoMapper簡介:
namespace AutoMapper_DI.Mappings
{
public class WebMappingProfile : Profile
{
public WebMappingProfile()
{
CreateMap<Person, PersonDTO>();
}
}
}
溫莎城堡登記:
class MainInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.AddFacility<TypedFactoryFacility>();
container.Register(Component.For<IMapper>().UsingFactoryMethod(x =>
{
return new MapperConfiguration(c =>
{
c.AddProfile<WebMappingProfile>();
}).CreateMapper();
}));
container.Register(Component.For<MainClass>());
}
}
,然後當我用_mapper我映射器未初始化例外:
class MainClass
{
private readonly IMapper _mapper;
public MainClass(IMapper mapper)
{
_mapper = mapper;
}
public void Start()
{
Person p = new Person
{
Name = "Somebody",
Surname = "Nobody",
Birth = new DateTime(1984, 06, 18)
};
var personDTO = Mapper.Map<Person, PersonDTO>(p);
}
}
感謝您的任何建議。
您是否考慮單獨創建映射器配置並將其作爲「實例」註冊爲Component.For().Instance(mapper);' –
stuartd