我正在使用
我想使用自定义的
在我的配置文件类中:
然后是我的
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进行对象创建?
更新 我的代码添加automapper配置:
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));
});
}
}