我正在尝试为automapper创建一个自定义解析器,它需要访问我的一个数据存储库来检索登录用户帐户。
到目前为止,这是我的代码......
public class FollowingResolver : ValueResolver<Audio, bool>
{
readonly IIdentityTasks identityTasks;
public FollowingResolver(IIdentityTasks identitTasks)
{
this.identityTasks = identitTasks;
}
protected override bool ResolveCore(Audio source)
{
var user = identityTasks.GetCurrentIdentity();
if (user != null)
return user.IsFollowingUser(source.DJAccount);
return false;
}
}
但是我收到了这个错误:
FollowingResolver' does not have a default constructor
我尝试添加默认的contrstructor,但我的存储库从未初始化。
这是我的autoampper初始化代码:
public static void Configure(IWindsorContainer container)
{
Mapper.Reset();
Mapper.Initialize(x =>
{
x.AddProfile<AccountProfile>();
x.AddProfile<AudioProfile>();
x.ConstructServicesUsing(container.Resolve);
});
Mapper.AssertConfigurationIsValid();
}
我错过了什么,甚至可以这样做,还是我在这里错过了船?
答案 0 :(得分:4)
我在使用Castle Windsor尝试注入服务时遇到了同样的错误。
我不得不补充道:
Mapper.Initialize(map =>
{
map.ConstructServicesUsing(_container.Resolve);
});
在Mapper.CreateMap
来电之前。
像这样创建ValueResolverInstaller
:
public class ValueResolverInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(Classes.FromThisAssembly()
.BasedOn<IValueResolver>()
.LifestyleTransient());
}
}
和ValueResolver
本身:
public class DivergencesResolver : ValueResolver<MyClass, int>
{
private AssessmentService assessmentService;
public DivergencesResolver(AssessmentService assessmentService)
{
this.assessmentService = assessmentService;
}
protected override int ResolveCore(MyClass c)
{
return assessmentService.GetAssessmentDivergences(c.AssessmentId).Count();
}
}
答案 1 :(得分:3)
之后不久找到了解决方案......我忘记将我的解析器添加为IoC容器。
现在很棒!