问题描述
我想创建一个自定义解析为automapper这需要访问我的数据存储库中检索登录的用户帐户之一。
I am trying to create a custom resolver for automapper which needs to access one of my data repositories to retreive the logged in users account.
下面是我的代码,所以远...
Here is my code so far...
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;
}
}
但是我得到这个错误:
However I am getting this error:
FollowingResolver' does not have a default constructor
我曾尝试添加一个默认contrstructor但我的仓库永远不会被初始化,然后
I have tried adding a default contrstructor but my repository never gets initialised then.
这是我的autoampper初始化代码:
This is my autoampper initialisation code:
public static void Configure(IWindsorContainer container)
{
Mapper.Reset();
Mapper.Initialize(x =>
{
x.AddProfile<AccountProfile>();
x.AddProfile<AudioProfile>();
x.ConstructServicesUsing(container.Resolve);
});
Mapper.AssertConfigurationIsValid();
}
我缺少的东西,它甚至有可能做到像这样还是我我在这里错过了船?
Am I missing something, is it even possible to do it like this or am I missing the boat here?
推荐答案
我正在使用温莎城堡,而试图注入一个服务同样的错误。
I was getting the same error using Castle Windsor while trying to inject a service.
我不得不添加:
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();
}
}
这篇关于Automapper自定义解析 - 注入到库构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!