Unity 派生类中的拦截
我遇到了一种情况,当我使用派生类时,策略注入不再起作用。
涉及的类如下所示(基本上是一个接口、一个抽象基类和一个实现类):
public interface IRepository<T>
{
void Create(T iItem);
}
public abstract class ElmtRepository<T> : IRepository<T>
{
protected List<T> Items { get; set; }
public ElmtRepository()
{
Items = new List<T>();
}
public void Create(T iItem)
{
Items.Add(iItem);
}
}
public class AcctPgmRepository : ElmtRepository<AcctPgm>
{
}
配置如下所示:
<container>
<extension type="Interception"/>
<register type="IRepository[AcctPgm]" mapTo="AcctPgmRepository">
<interceptor type="InterfaceInterceptor"/>
<interceptionBehavior type="PolicyInjectionBehavior"/>
</register>
<interception>
<policy name="policy-create">
<matchingRule name="create-rule1" type="TypeMatchingRule">
<constructor>
<param name="typeName">
<value value="AcctPgmRepository"/>
</param>
</constructor>
</matchingRule>
<matchingRule name="create-rule2" type="MemberNameMatchingRule">
<constructor>
<param name="namesToMatch">
<array type="string[]">
<value value="Create"/>
</array>
</param>
</constructor>
</matchingRule>
<callHandler name="create-handler1" type="AcctPgmAuthorizationHandler">
<lifetime type="singleton"/>
<constructor>
<param name="allowedRoles">
<array type="string[]">
<value value="GroupController"/>
</array>
</param>
</constructor>
</callHandler>
</policy>
</interception>
</container>
如果我删除 ElmtRepository 基类,它会按预期工作。对于基类,注入不会发生。没有错误消息,但也没有策略。即使我在派生类中实现 Create() 方法,也会发生这种情况。
有没有办法让这种类层次结构与 Unity 策略注入一起工作?
谢谢, 吉姆
I've got a situation where policy injection no longer works when I'm using a derived class.
The classes involved look like this (basically an interface, an abstract base class, and an implementation class):
public interface IRepository<T>
{
void Create(T iItem);
}
public abstract class ElmtRepository<T> : IRepository<T>
{
protected List<T> Items { get; set; }
public ElmtRepository()
{
Items = new List<T>();
}
public void Create(T iItem)
{
Items.Add(iItem);
}
}
public class AcctPgmRepository : ElmtRepository<AcctPgm>
{
}
The configuration looks like this:
<container>
<extension type="Interception"/>
<register type="IRepository[AcctPgm]" mapTo="AcctPgmRepository">
<interceptor type="InterfaceInterceptor"/>
<interceptionBehavior type="PolicyInjectionBehavior"/>
</register>
<interception>
<policy name="policy-create">
<matchingRule name="create-rule1" type="TypeMatchingRule">
<constructor>
<param name="typeName">
<value value="AcctPgmRepository"/>
</param>
</constructor>
</matchingRule>
<matchingRule name="create-rule2" type="MemberNameMatchingRule">
<constructor>
<param name="namesToMatch">
<array type="string[]">
<value value="Create"/>
</array>
</param>
</constructor>
</matchingRule>
<callHandler name="create-handler1" type="AcctPgmAuthorizationHandler">
<lifetime type="singleton"/>
<constructor>
<param name="allowedRoles">
<array type="string[]">
<value value="GroupController"/>
</array>
</param>
</constructor>
</callHandler>
</policy>
</interception>
</container>
If I remove the ElmtRepository base class, it works as expected. With the base class, the injection doesn't happen. No error messages, but no policies either. This happens even if I implement the Create() method in the derived class.
Is there a way to make this sort of class hierarchy work with Unity policy injection?
Thanks,
Jim
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Unity 的这种类继承通常不会有问题。然而,配置泛型会带来无尽的麻烦,而且错误消息也很糟糕。我敢打赌这就是你真正的问题。但是,由于您没有发布错误消息(或 AcctPgm 类或 AcctPgmAuthorizationHandler),我无法确定。
我将包含的类型更改为
int
并让此版本的代码正常工作:使用配置:
给出输出:
This kind of class inheritance Unity usually does without a problem. However configuring generics is endless headaches with poor error messages. I bet that’s your real problem. However since you didn’t post your error message (or the AcctPgm class, or the AcctPgmAuthorizationHandler) I can’t be sure.
I changed the contained type to
int
and got this version of your code working:with config:
Gives output: