[英]AutoMapper with Ninject
我一直在嘗試設置AutoMapper來通過Ninject實例化所有對象。 我在global.asax文件中有以下代碼
Mapper.Configuration.ConstructServicesUsing(x => kernel.Get(x));
作為一個例子,我有以下映射
Mapper.CreateMap<TestModel, IndexViewModel>();
但是,這似乎不起作用。 我得到一個'IndexViewModel'沒有默認構造函數的錯誤。
我可以通過顯式告訴automapper在映射中使用ninject來使映射器工作。
Mapper.CreateMap<TestModel, IndexViewModel>().ConstructUsingServiceLocator();
但是,我寧願不必為每個映射執行此操作。 我錯過了什么嗎?
只需在初始化代碼中的某處創建一個為您執行此操作的函數
void CreateMapWithServiceLocator<T1,T2>()
{
Mapper.CreateMap<T1,T2>().ConstructUsingServiceLocator();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.