ay.metallo
ay.metallo

Reputation: 825

Specify policy for an interceptable type

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

Upvotes: 1

Views: 672

Answers (0)

Related Questions