指定可拦截类型的策略
假设我有以下代码:
class Program
{
static void Main(string[] args)
{
IUnityContainer container = new UnityContainer();
container.AddNewExtension<Interception>();
container.RegisterType<Interceptable>(
new Interceptor<VirtualMethodInterceptor>(),
new InterceptionBehavior<PolicyInjectionBehavior>());
container
.Configure<Interception>()
.AddPolicy("PolicyName")
.AddMatchingRule(new CustomAttributeMatchingRule(typeof(SomeAttribute), true))
.AddMatchingRule(new PropertyMatchingRule("*", PropertyMatchingOption.Set))
.AddCallHandler<PropertySetterCallHandler>();
.Interception
.AddPolicy("AnotherPolicyName")
.AddMatchingRule(new CustomAttributeMatchingRule(typeof(SomeOTHERAttribute), true))
.AddMatchingRule(new PropertyMatchingRule("*M", PropertyMatchingOption.Set))
.AddCallHandler<ANOTHERPropertySetterCallHandler>();
var ic = container.Resolve<Interceptable>();
//property setter is invoked - the matching rules from BOTH the policies will be tried
ic.Property = 2;
Console.ReadLine();
}
class Interceptable
{
public virtual int Property { get; [SomeAttribute]set; }
}
}
按照现在的配置方式,每次我解析 Interceptable 实例并在其上调用公共虚拟方法时,都会尝试 4 个匹配规则(来自两个策略)。
这可能会在现实应用程序中产生开销。我想要做的是指定我只想(例如)“PolicyName”策略应用于 Interceptable 类的实例。有什么办法可以做到这一点吗?
谢谢
Suppose I have the following code:
class Program
{
static void Main(string[] args)
{
IUnityContainer container = new UnityContainer();
container.AddNewExtension<Interception>();
container.RegisterType<Interceptable>(
new Interceptor<VirtualMethodInterceptor>(),
new InterceptionBehavior<PolicyInjectionBehavior>());
container
.Configure<Interception>()
.AddPolicy("PolicyName")
.AddMatchingRule(new CustomAttributeMatchingRule(typeof(SomeAttribute), true))
.AddMatchingRule(new PropertyMatchingRule("*", PropertyMatchingOption.Set))
.AddCallHandler<PropertySetterCallHandler>();
.Interception
.AddPolicy("AnotherPolicyName")
.AddMatchingRule(new CustomAttributeMatchingRule(typeof(SomeOTHERAttribute), true))
.AddMatchingRule(new PropertyMatchingRule("*M", PropertyMatchingOption.Set))
.AddCallHandler<ANOTHERPropertySetterCallHandler>();
var ic = container.Resolve<Interceptable>();
//property setter is invoked - the matching rules from BOTH the policies will be tried
ic.Property = 2;
Console.ReadLine();
}
class Interceptable
{
public virtual int Property { get; [SomeAttribute]set; }
}
}
The way it's configured now, every time I resolve an instance of Interceptable and call a public virtual method on it, 4 matching rules get tried (from both the policies).
This may create an overhead in a real-world app. What I'd like to do is to specify that I want only (for example) 'PolicyName' policy to be applied to instances of Interceptable class. Is there any way to do this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论