Generic DefaultInterceptor

Nov 22, 2011 at 4:34 AM

Hi, i am curious when i used the generic DefaultInterceptor, why there is a constraint on the generic type "TInterceptor" that it should be a "ITypeInterceptor"

public class DefaultInterceptor<TInterceptor> : DefaultInterceptor where TInterceptor: ITypeInterceptor

As base DefaultInterceptor has a ctor. as follows:

public DefaultInterceptor(IInterceptor interceptor)
 ... ...

means that it can use all three kind of interceptors. 


So, is there any special consideration for the Generic DefaultInterceptor?

Nov 22, 2011 at 5:08 AM

The generic version of DefaultInterceptor is provided so that you can use generics to specify the default interceptor for a type irrespective of any naming keys.

When using the generic version it doesn't actually use the constructor that accepts an instance of IInterceptor.  It uses the constructor that accepts a Type which is validated in the DefaultInterceptor constructor to verify that the type argument implements IInterceptor.

e.g. generic: 

public class DefaultInterceptor<TInterceptor> : DefaultInterceptor
        where TInterceptor : ITypeInterceptor
    /// <summary>
    /// Create a new instance of <see cref="DefaultInterceptor{TInterceptor}"/>.
    /// </summary>
    /// <param name="name">Name to use when resolving interceptor.</param>
    public DefaultInterceptor(string name)
            : base(typeof(TInterceptor), name)

 and the base constructor in DefaultInterceptor:

public DefaultInterceptor(Type interceptorType, string name)
    Guard.ArgumentNotNull(interceptorType, "interceptorType");
    Guard.TypeIsAssignable(typeof (IInterceptor), interceptorType, "interceptorType");
    this.interceptorKey = new NamedTypeBuildKey(interceptorType, name);

The DefaultInterceptor ensures that the correct types are passed to the constructor at runtime via a validation check.  In contrast, the generic version with the constraints allows
the compiler to perform design time type validation.  

Functionally, the two classes are equivalent since DefaultInterceptor<T> is just a wrapper around DefaultInterceptor.  

Randy Levy
Enterprise Library support engineer

Nov 22, 2011 at 5:25 AM
Edited Nov 22, 2011 at 5:50 AM

Thanks for reply , but I think i did no write so clearly, English is not my main language :(

But what i want to know is why the generic DefaultInterceptor not defined as follows:

public class DefaultInterceptor<TInterceptor> : DefaultInterceptor
        where TInterceptor : IInterceptor

Because base DefaultInterceptor's ctor. accepts any concrete interceptor that is IInterceptor but not only ITypeInterceptor right?

Nov 23, 2011 at 12:17 AM

OK, I see what you are saying.  The DefaultInterceptor also validates that the type is an IInterceptor and not ITypeInterceptor which seems inconsistent.  Basically, this means that the generic version can only be used with the VirtualMethodInterceptor.  

I'm not sure why if I use the generic DefaultInterceptor I have to use DefaultInterceptor<VirtualMethodInterceptor> but if I use the non-generic DefaultInterceptor
I can say DefaultInterceptor(typeof(TransparentProxyInterceptor)).

    var interceptor1 = new DefaultInterceptor(typeof(TransparentProxyInterceptor)); // OK
    var interceptor2 = new DefaultInterceptor<VirtualMethodInterceptor>(); //OK
    var interceptor3 = new DefaultInterceptor<TransparentProxyInterceptor>(); //Compile error



Randy Levy
Enterprise Library support engineer