AOP in .NET

A few months ago I did some research into dependency injection frameworks. One of the interesting features provided by many of the DI frameworks was the support for AOP. This is really interesting stuff and great for injecting logging and caching implementations without having to modify existing code.

There are several AOP implementations around (Oren Eini identified seven in this post) and I have always wondered how some of them worked. Unfortunately I never got the time to research it.

Using aspects in .NET - Part 1 is a post by Istvan that just popped up in my feed reader. He describes how to create an AOP implementation using ContextBoundObject and ProxyAttribute classes. I didn't previously know that combining these types in the .Net framework allows you to override the new statement. This is extremely powerful. I had assumed that this was the method that TypeMock used for creating unit testing mocks, but Oren's post identifies it as using a profiling API.

I think the centerpiece of Istvan's implementation is the RealProxy class. This is another one of those .Net classes that is incredibly powerful. I have used RealProxy before to create a wrapper for WCF channel management. Thankfully this helped me to understand Istvan's post quickly. I am looking forward to his Part 2 post.

The only thing I don't like about this style of AOP is that it requires the aspected class to derive from ContextBoundObject and have a ProxyAttribute derived attribute decorated on the class. This has the disadvantage that the aspected class must be not only designed for AOP, but coded for this specific type of AOP. I'm a fan of POCO code design, so at this stage I think the profiling API version of AOP used by TypeMock is so much more powerful and has less impact on the code.

BTW, Castle Windsor was my pick of the dependency injection frameworks as it was the most suitable for my requirements. I also did some research into mocking frameworks about the same time. While TypeMock was certainly the most powerful, its particular implementation isn't very flexible. Oren's RhinoMocks was my pick on that one.

blog comments powered by Disqus