Sup*_*JMN 0 inversion-of-control unity-container automapper constructor-injection
我已经试过几乎所有的东西,但我不能让AutoMapper映射A =>乙当B没有参数的构造函数.
我正在使用Unity并且所有依赖项都被方便地注册了,但是,我怎么说AutoMapper"嘿,如果目标实例需要构造函数中的某些依赖项,请让Unity构建它,然后再进行映射.
我试过了
Mapper.Initialize(configuration =>
{
configuration.ConstructServicesUsing(container.Resolve);
configuration.CreateMap<Person, PersonViewModel>();
});
Run Code Online (Sandbox Code Playgroud)
但它不起作用:(
编辑:事实上,我撒谎了一下.我没有使用Unity.我正在使用格雷斯,但不想提出一个相对未知的容器询问有关进展主题:)
我已经解决了这个问题,它和丝绸一样光滑.确切的代码是这样的.请记住,我正在使用Grace IoC Container(我热切推荐).
Bootstrapper.Instance.Configure(new CompositionRoot());
Mapper.Configuration.ConstructServicesUsing(type => Bootstrapper.Instance.Container.Locate(type));
Mapper.CreateMap<Person, PersonViewModel>()
.ConstructUsingServiceLocator();
Run Code Online (Sandbox Code Playgroud)
像这样:
configuration.CreateMap<Person, PersonViewModel>()
.ConstructUsingServiceLocator();
Run Code Online (Sandbox Code Playgroud)
对应由服务定位器创建的每个映射执行此操作.