我正在尝试向 CaSTLeWindsor 注册 AutoMapper 5.1.1,但我不知道在哪里正确调用 Mapper.Initialize()。
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 时,我得到了 Mapper not initialized 异常:
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);
}
}
感谢您的任何建议。
最佳答案
所以,上面的代码是有效的。问题是,我是个白痴。因为我不应该调用 Mapper.Map,而是调用 _mapper.Map。
关于c# - 向 CaSTLe Windsor 注册 AutoMapper 5.1.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39662607/