I\'m a newcomer to the idea of aspect-oriented programming but I would like to explore the idea of using it on my project for handling logging, reporting, etc. To this end
I can't speak for the specifics of .NET, but AOP and the more general idea of being able to attach hooks to arbitrary methods, is a handy technique that can solve some otherwise hairy problems.
One example is design by contract. Let's say you have a bunch of methods upon which you want to apply some common contracts. You can add some "advice" (the AOP term) before and after each method is called without having to cut & paste it into every method.
While testing, it is often useful to know what is going on in some internal method. How many times it was called and maybe what it returned. You can add an aspect to do that monitoring without having to add distracting testing code to the method itself. Editing the code for that method might not even be possible.
Just watch out for how aspects are implemented. Some implementations are elaborate pre-processors which may make debugging your code more complicated. Others hook smoothly into the language. Dynamic languages handle AOP very well. Perl has Aspect.pm for AOP and the more general Hook::LexWrap to accomplish method hooks.
If you are going to look at Post Sharp you could download Google Book Downloader from CodePlex. I think this project uses it.
AOP is interesting to me as well. It seems to me that logging and performance monitoring, reporting is very much what AOP is designed to handle. For .NET, Post Sharp is a very good framework for AOP.
I've only experimented with it a little bit but it's seems very well implemented.
Aspect Oriented Programming is so much more than just logging, reporting et cetera, as you will see if you have a look at PostSharp's web site. Personally I haven't done so much static IL weaving, mostly dynamic IL generation to create AOP interceptors and when doing so I have mostly been using it to wrap and intercept resolves from inversion of control containers.
AOP can improve exception handling, improve tracing, improve transaction interception.
NHibernate for example has a sort of AOP, even though it's static at compile-time in terms of simple event handlers; but for certain events in the engine you can attach interceptors (aka aspects, the events being the point-cuts etc) -- I use this to inject, using IoC business entities into my domain objects.
Powerful AOP-frameworks allow you to generalize and even more powerful allow you to generalize w/o overhead at runtime; in principle you have a few different ways of doing it:
(0). (not really) "pre-processor" AOP aka templates in C++, ifdefs etc
Usages in transactions Have a look at Castle.Facilities.AutomaticTransactionManagement.TransactionFacility for a nice way of handling transactions using AOP and the intercepting abilities of DynamicProxy2. The transaction facility integrated with System.Transcations and System.EnterpriseServices is you are using the distributed transaction coordinator (COM-component) for managing transactions. Also, there are multiple examples of p/invoke into the kernel to take care of the TxF and TxR components of the Vista-kernel (aka Server 2008) which allow you to use transactions on NTFS and on the registry, thereby making sure CRUD you do is ACID, which also nicely integrates with System.Transactions for creating nested transactions.
Usages in invariant verification You can also use them for design by contract, by appending some attributes to your parameters.
public void PerformOperation([NotNull, NotEmpty] string value) {
// use string
[NotNull] return new string(' ', 5); // can return with attributes as well
}
The problem with this at the moment is the overhead of attaching this meta-data and checking it at runtime. However, you could specify the constraint-checking aspect only to be applied when you're compiling with DEBUG and then this meta data-wouldn't lead to much deterioration in performance.
If you are looking to get into axiomatic proofs, have a look at Sing#/Spec# instead, since that's more formal and the work is done by the compiler.
Things to be aware of The most important point to be aware of is that if a concern, i.e. some piece of code that runs before or after your method is altering the control-flow, possibly returning an unexpected type, returning too early or in general doesn't behave along the intents of the method you were calling you may get errors which are hard to debug.
Also, beware of the throwing of exceptions from attributes, because you never know when or from what assembly, reflection occurrs; the reflection on your attribute may not happen when you expect to. This happened to myself when I was attaching types in attributes and carefully checking them.
Also beware of the fact that you're opening a possible attack vector in adding global "point-cuts" which, if someone gets access to, can be used to redirect large portions of your system.
Other frameworks If you are interested in learning more about AOP in general I suggest you check out Rickard Öberg's presentations on Qi4J, it's a very good framework in Java for AOP (java has slightly different object-inherited semantics though which makes a wee bit tricker to use in C#/F#/Nermle/Boo whatever.
AOP + AddIns Another interesting possibility in using aspect oriented programming with runtime-generated assemblies such as those dynamicproxy2 creates, is that you can also use them to wrap objects that cross application boundaries, thereby simplifying the creation of a add-in-pipeline. I had secretly hoped that Microsoft would use this when they created their AddIn-framework for 3.5, but they chose to go the static code-gen way unfortunately, leading a a rather big overhead in creating add-ins, for the developer. The problem is that a type loaded for "more than reflection" into an AppDomain can't be unloaded again unless the complete AppDomain is unloaded, so you need 1) to reflect on the plugin without loading it to see what it is capable of unless you allow for lots of manual meta-data to be written or generated (and believe in that) and 2) some object to hold the handle to your object so that it's not GCed and you don't know the type (hence the IContract assembly and AddInHandle-class) -- this could probably be done in a nice way with a dynamic proxy/AOP.
Using AOP for global garbage collection ... in a distributed system running on linux/windows on the common language infrastructure. The paper was a bit hard to download so I uploaded it to my server so I know where it is.
Post Scriptum (If you are using a non-standard language on the CLR and not the DLR IL-weaving might create non-standards compliant code. Especially interesting for F#, I think, because the use a lot of non-standard code to great benefit of the language (tuples say) -- you could mark your assembly with [assembly: CLSCompliant] if you want to get compile-time warnings of this.)
Do not think its something totally different. AOP improves (IMO) your design by reducing coupling, increasing cohesion, separate concerns by giving an object of a certain type 1 single responsibility. If your are from .net world PostSharp makes use of custom attributes to weave advices. If you are from the Java world you can use the Java extension called AspectJ. AOP has more applications than what you see generally.