Performing an interception using a structure

I am trying to do some attribute-based interception using the structure structure, but I'm struggling to tie the last free ends.

I have a custom registry that scans my assemblies and in this registry I defined the following ITypeInterceptor, the purpose of which is to match the types decorated with this attribute, and then apply an interceptor if they match. A class is defined as such:

public class AttributeMatchTypeInterceptor<TAttribute, TInterceptor> : TypeInterceptor where TAttribute : Attribute where TInterceptor : IInterceptor { private readonly ProxyGenerator m_proxyGeneration = new ProxyGenerator(); public object Process(object target, IContext context) { return m_proxyGeneration.CreateInterfaceProxyWithTarget(target, ObjectFactory.GetInstance<TInterceptor>()); } public bool MatchesType(Type type) { return type.GetCustomAttributes(typeof (TAttribute), true).Length > 0; } } //Usage [Transactional] public class OrderProcessor : IOrderProcessor{ } ... public class MyRegistry : Registry{ public MyRegistry() { RegisterInterceptor( new AttributeMatchTypeInterceptor<TransactionalAttribute, TransactionInterceptor>()); ... } } 

I use DynamicProxy from Castle.Core to create interceptors, but my problem is that the object returned from the call to CreateInterfaceProxyWithTarget (...) does not implement the interface that caused the creation of the target instance in the map structure (for example, IOrderProcessor in the example above) . I was hoping the IContext parameter would open this interface, but I can only seem to grab onto a specific type (i.e. the OrderProcessor in the example above).

I am looking for guidance on working with this scenario by calling ProxyGenerator to return an instance that implements all the interfaces as the target instance, by retrieving the requested interface from the structure structure or using some other mechanism.

+4
source share
1 answer

I really have something working with a little caution, so I will just post this as an answer. The trick was to get the interface and pass it to CreateInterfaceProxyWithTarget. My only problem was that I could not find a way to ask IContext which interface it currently resolves, so I just looked at the first interface of the target that worked for me. See code below

 public class AttributeMatchTypeInterceptor<TAttribute, TInterceptor> : TypeInterceptor where TAttribute : Attribute where TInterceptor : IInterceptor { private readonly ProxyGenerator m_proxyGeneration = new ProxyGenerator(); public object Process(object target, IContext context) { //NOTE: can't query IContext for actual interface Type interfaceType = target.GetType().GetInterfaces().First(); return m_proxyGeneration.CreateInterfaceProxyWithTarget( interfaceType, target, ObjectFactory.GetInstance<TInterceptor>()); } public bool MatchesType(Type type) { return type.GetCustomAttributes(typeof (TAttribute), true).Length > 0; } } 

Hope this helps someone

+2
source

All Articles