c#-向Castle Windsor注册AutoMapper 5.1.1
作者:互联网
我正在尝试向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未初始化的异常:
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.
标签:castle-windsor,automapper-5,c 来源: https://codeday.me/bug/20191118/2025897.html