问题
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