AutoMapper with Ninject

廉价感情. 提交于 2019-12-07 08:47:32

问题


I've been trying to setup AutoMapper to instantiate all objects via Ninject. I've got the following code in my global.asax file

Mapper.Configuration.ConstructServicesUsing(x => kernel.Get(x));

And as an example I have the following mapping

Mapper.CreateMap<TestModel, IndexViewModel>();

However, this does not appear to be working. I get an error that 'IndexViewModel' does not have a default constructor.

I can get the mapper to work by explicitly telling automapper to use ninject in the mapping.

Mapper.CreateMap<TestModel, IndexViewModel>().ConstructUsingServiceLocator();

However, I'd rather not have to do this for every single mapping. Am I missing something?


回答1:


Just create a function to do this for you somewhere in your initialisation code

void CreateMapWithServiceLocator<T1,T2>()
{
     Mapper.CreateMap<T1,T2>().ConstructUsingServiceLocator();
} 


来源:https://stackoverflow.com/questions/10559026/automapper-with-ninject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!