Creating a dynamic proxy generator with c# – Part 2 – Interceptor Design

Posted by SeanMcAlinden on ASP.net Weblogs See other posts from ASP.net Weblogs or by SeanMcAlinden
Published on Wed, 24 Mar 2010 21:47:00 GMT Indexed on 2010/03/24 22:03 UTC
Read the original article Hit count: 880

  1. Creating a dynamic proxy generator – Part 1 – Creating the Assembly builder, Module builder and caching mechanism

For the latest code go to http://rapidioc.codeplex.com/

Before getting too involved in generating the proxy, I thought it would be worth while going through the intended design, this is important as the next step is to start creating the constructors for the proxy.

  • Each proxy derives from a specified type
  • The proxy has a corresponding constructor for each of the base type constructors
  • The proxy has overrides for all methods and properties marked as Virtual on the base type
  • For each overridden method, there is also a private method whose sole job is to call the base method.
  • For each overridden method, a delegate is created whose sole job is to call the private method that calls the base method.

The following class diagram shows the main classes and interfaces involved in the interception process.

I’ll go through each of them to explain their place in the overall proxy.

ProxyDiagram

 

IProxy Interface

The proxy implements the IProxy interface for the sole purpose of adding custom interceptors.

This allows the created proxy interface to be cast as an IProxy and then simply add Interceptors by calling it’s AddInterceptor method.

This is done internally within the proxy building process so the consumer of the API doesn’t need knowledge of this.

IInterceptor Interface

The IInterceptor interface has one method: Handle.

The handle method accepts a IMethodInvocation parameter which contains methods and data for handling method interception.

Multiple classes that implement this interface can be added to the proxy.

Each method override in the proxy calls the handle method rather than simply calling the base method.

How the proxy fully works will be explained in the next section MethodInvocation.

IMethodInvocation Interface & MethodInvocation class

The MethodInvocation will contain one main method and multiple helper properties.

Continue Method

The method Continue() has two functions hidden away from the consumer.

When Continue is called, if there are multiple Interceptors, the next Interceptors Handle method is called.

If all Interceptors Handle methods have been called, the Continue method then calls the base class method.

Properties

The MethodInvocation will contain multiple helper properties including at least the following:

  • Method Name (Read Only)
  • Method Arguments (Read and Write)
  • Method Argument Types (Read Only)
  • Method Result (Read and Write) – this property remains null if the method return type is void
  • Target Object (Read Only)
  • Return Type (Read Only)

DefaultInterceptor class

The DefaultInterceptor class is a simple class that implements the IInterceptor interface.

Here is the code:

DefaultInterceptor
  1. namespace Rapid.DynamicProxy.Interception
  2. {
  3.     /// <summary>
  4.     /// Default interceptor for the proxy.
  5.     /// </summary>
  6.     /// <typeparam name="TBase">The base type.</typeparam>
  7.     public class DefaultInterceptor<TBase> : IInterceptor<TBase> where TBase : class
  8.     {
  9.         /// <summary>
  10.         /// Handles the specified method invocation.
  11.         /// </summary>
  12.         /// <param name="methodInvocation">The method invocation.</param>
  13.         public void Handle(IMethodInvocation<TBase> methodInvocation)
  14.         {
  15.             methodInvocation.Continue();
  16.         }
  17.     }
  18. }

This is automatically created in the proxy and is the first interceptor that each method override calls.

It’s sole function is to ensure that if no interceptors have been added, the base method is still called.

Custom Interceptor Example

A consumer of the Rapid.DynamicProxy API could create an interceptor for logging when the FirstName property of the User class is set.

Just for illustration, I have also wrapped a transaction around the methodInvocation.Coninue() method.

This means that any overriden methods within the user class will run within a transaction scope.

MyInterceptor
  1. public class MyInterceptor : IInterceptor<User<int, IRepository>>
  2. {
  3.     public void Handle(IMethodInvocation<User<int, IRepository>> methodInvocation)
  4.     {
  5.         if (methodInvocation.Name == "set_FirstName")
  6.         {
  7.             Logger.Log("First name seting to: " + methodInvocation.Arguments[0]);
  8.         }
  9.         using (TransactionScope scope = new TransactionScope())
  10.         {
  11.             methodInvocation.Continue();
  12.         }
  13.         if (methodInvocation.Name == "set_FirstName")
  14.         {
  15.             Logger.Log("First name has been set to: " + methodInvocation.Arguments[0]);
  16.         }
  17.     }
  18. }

Overridden Method Example

To show a taster of what the overridden methods on the proxy would look like, the setter method for the property FirstName used in the above example would look something similar to the following (this is not real code but will look similar):

set_FirstName
  1. public override void set_FirstName(string value)
  2. {
  3.     set_FirstNameBaseMethodDelegate callBase =
  4.         new set_FirstNameBaseMethodDelegate(this.set_FirstNameProxyGetBaseMethod);
  5.     object[] arguments = new object[] { value };
  6.     IMethodInvocation<User<IRepository>> methodInvocation =
  7.         new MethodInvocation<User<IRepository>>(this, callBase, "set_FirstName", arguments, interceptors);
  8.     
  9.     this.Interceptors[0].Handle(methodInvocation);
  10. }

As you can see, a delegate instance is created which calls to a private method on the class, the private method calls the base method and would look like the following:

calls base setter
  1. private void set_FirstNameProxyGetBaseMethod(string value)
  2. {
  3.     base.set_FirstName(value);
  4. }

The delegate is invoked when methodInvocation.Continue() is called within an interceptor.

The set_FirstName parameters are loaded into an object array.

The current instance, delegate, method name and method arguments are passed into the methodInvocation constructor (there will be more data not illustrated here passed in when created including method info, return types, argument types etc.)

The DefaultInterceptor’s Handle method is called with the methodInvocation instance as it’s parameter.

Obviously methods can have return values, ref and out parameters etc. in these cases the generated method override body will be slightly different from above. I’ll go into more detail on these aspects as we build them.

Conclusion

I hope this has been useful, I can’t guarantee that the proxy will look exactly like the above, but at the moment, this is pretty much what I intend to do.

Always worth downloading the code at http://rapidioc.codeplex.com/ to see the latest.

There will also be some tests that you can debug through to help see what’s going on.

Cheers,

Sean.

© ASP.net Weblogs or respective owner

Related posts about c#

Related posts about .NET