Sponsored By Aspose - File Format APIs for .NET

Aspose are the market leader of .NET APIs for file business formats – natively work with DOCX, XLSX, PPT, PDF, MSG, MPP, images formats and many more!

Simple AOP Example Using Castle DynamicProxy 2.0

Unity 1.2 is about to be released anytime now, and one of the exciting new features is that the core interception technology from the Policy Injection Application Block has been moved to Unity. This allows one to do AOP with Unity without the need for Enterprise Library and the Policy Injection Application Block.


I created a couple examples of using interception with Unity:



You will notice from above that Unity also contains a VirtualMethodInterceptor that is a lighter weight alternative to the TransparentProxyInterceptor that is in the Policy Injection Application Block. There is also an InterfaceInterceptor as well, which again, is a lightweight alternative. I have been looking forward to these alternatives for a long time, so it is great to see them in there.


As I have been playing with the new version of Unity I started thinking about how much I also really like Castle DynamicProxy, which I have used quite a bit in my applications to create proxies for use with interceptors for AOP functionality.


Below is a simple console application that displays “Hello, world.” using Castle DynamicProxy 2.0 to create a Logger Proxy Class that invokes the TestInterceptor’s Intercept Method before invoking the Logger’s Write Method.


 



using System;


using Castle.Core.Interceptor;


using Castle.DynamicProxy;


 


namespace DynamicProxyExample


{


    class Program


    {


        static void Main(string[] args)


        {


            ProxyGenerator generator = new ProxyGenerator();


            Logger logger = generator.CreateClassProxy<Logger>(new TestInterceptor());


            logger.Write(“World.”);


        }


    }


 


    public class TestInterceptor : IInterceptor


    {


        public void Intercept(IInvocation invocation)


        {


            Console.Write(“Hello, “);


            invocation.Proceed();


        }


    }


 


    public class Logger


    {


        public virtual void Write(string message)


        {


            Console.Write(message);


        }


    }


}


 


Again, this simply displays the following:


 


 


 


Very simple I know, but if you are new to AOP and DynamicProxy 2.0 this might blow your mind :)


You can download DynamicProxy 2.0 at CastleProject.org.


 


Recent Posts:



 

This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

One Response to Simple AOP Example Using Castle DynamicProxy 2.0

  1. Stephen says:

    Very cool, I really like how unity is developing!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>