【发布时间】:2014-07-08 01:29:16
【问题描述】:
我正在使用automapper 库将我的Model 转换为我的ViewModel。对于每个Model,我创建配置文件,在其中使用CreateMap 添加我的地图。
我想使用自定义ValueResolver,它将从IContext 获取记录的用户ID,所以我需要使用Ninject 传递IContext 的实现。
在我的个人资料类中:
Mapper.CreateMap<ViewModel, BusinessModel>()
.ForMember(dest => dest.ManagerId, opt => opt.ResolveUsing<GetManagerResolver>());
然后我的GetManagerResolver:
public class GetManagerResolver : ValueResolver<BusinessModel, int>
{
private IContext context;
public GetManagerResolver(IContext context)
{
this.context = context;
}
protected override int GetManagerResolver(BusinessModel source)
{
return context.UserId;
}
}
但我收到此异常消息{"Type needs to have a constructor with 0 args or only optional args\r\nParameter name: type"}。
关于如何让 automapper 使用 ninject 创建对象有什么想法吗?
更新 我添加自动映射器配置的代码:
public static class AutoMapperWebConfiguration
{
public static void Configure()
{
Mapper.Initialize(cfg =>
{
cfg.AddProfile(new Profile1());
cfg.AddProfile(new Profile2());
// now i want to add this line, but how to get access to kernel in static class?
// cfg.ConstructServicesUsing(t => Kernel.Get(t));
});
}
}
【问题讨论】:
标签: asp.net-mvc-5 ninject automapper