• Home /
  • Blog /
  • Aspect Oriented Programming (AOP) logging: Castle DynamicProxy vs Microsoft Policy Injection vs Postsharp
Aop Logging

Aspect Oriented Programming (AOP) logging: Castle DynamicProxy vs Microsoft Policy Injection vs Postsharp

While researching around AOP framework I choose the following to test:

I have to say then PostSharp gave me the best AOP experience, I was able to produce clean and simple code to log calls to all methods and parameters, as well as return objects from my classes. But because that product is not free and licensing policy is too complex I have to eliminate PostSharp from further review.

NOTE: I do not mind to pay for software (in fact usually its company where you work pay for it), if the product is good, there is nothing wrong with making money on the product. The problem with license I have was then I can’t use PostSharp on build server. The build server is either managed by different department and you just don’t have access to install anything there or as it was in my case I use cloud TFS server at tfs.visualstudio.com so I just don’t have control over the server. If some time in the future PostSharp will have different license policy for their free version, probably I will use it.

AOP logging with Castle DynamicProxy vs Microsoft Policy Injection

So I have two frameworks left. Initially I tried Microsoft EnterpriceLibrary with Policy Injection. I like Microsoft products, so it was obvious for me to try Microsoft project first. It worked pretty well. You define interceptor class which will intercept calls to methods and create a proxy class by calling single methods. It also works very well in combination with various dependency injection framework. What I did not liked about that framework was the way how to configure it. I have to mark each class which required logging with special attribute and configure how to use that attribute in .config file. In fact from AOP point of view the framework behaves correct, you explicitly tells what classes should have logging and what does not require it, but I prefer the way where it will be less work for me.

On the other hand Castle DynamicProxy did not have such problems (but it still have ability to identify what classes should have logging and what should not have). The configuration was very easy and using StructureMap as a dependency injection framework I was able to wrap in proxy and intercept all classes implicitly. After working with this framework for some period of time I found it very convenient and don’t have any problems using it.

NOTE: also you can try Castle Windsor as a dependency injection framework, it’s pretty good and works very well with DynamicProxy, but I still prefer StructureMap.

What I did not liked in both frameworks is then wrapped class should have interface, so you can’t just take any class and wrap it in proxy. This limitation is not something then stop me from using it, in fact it force you to good practice of using interfaces instead of particular classes, which lead to better design and architecture. But that nice to have feature exists in PostSharp, where you can wrap any class, even if it does not have interface (in fact PostSharp works absolutely different, its doing his work during compile time while DynamicProxy and Policy Injection doing it during runtime, that’s is why it’s more powerful)

In conclusion, if money is not a problem and you have full control over the build server (I hope you are using build server J ) then PostSharp will provide you with best AOP experience. If you are already using Microsoft Enterprise Library in your project then choose Policy Injection block. Otherwise I suggest Castle DynamicProxy.

Yaplex - Freelance developer - white logo on black background

Yaplex ® is a custom software development company located in Mississauga, Ontario. Yaplex was founded in 2013 and provides website development, mobile development, and custom .NET software development services.

Contact Information

Mississauga, ON, Canada
Phone: +1 647 328 3809
Email: hello@yaplex.com