c# – 使用Ninject的AutoMapper
发布时间:2020-12-15 21:34:10 所属栏目:百科 来源:网络整理
导读:我一直在尝试设置AutoMapper来通过Ninject实例化所有对象. 我在global.asax文件中有以下代码 Mapper.Configuration.ConstructServicesUsing(x = kernel.Get(x)); 作为一个例子,我有以下映射 Mapper.CreateMapTestModel,IndexViewModel(); 但是,这似乎不起作
我一直在尝试设置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(); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |