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
- Reflection "AOP"
- IL-generation at runtime through Reflection.Emit, requires high trust. This is the path DynamicProxy2 in the Castle project has taken. DynamicProxy2 is rather nice and lots of work has gone into it! Also, afaik PatternsAndPractices Policy Framework uses this approach as well, with lots of XML, although with their own generator. NHibernate has a dependency on DynProx2.
- To IL-compilation + Assembly.Load(...) at runtime through using System.CodeDom.Compiler, then loading your created assmblies, requires high trust. Compiling with any other compiler like Boo.Compiler is also possible as it creates "global function assemblies" which you can call in a 'scripted' way, but now we're moving out of AOP a bit.
- Profiler APIs (don't ask me about them)
- Relying on the runtime framework: extending MarshalByRef/ContextBoundObject see link and using the remoting-infrastructure in .Net to do AOP, which is quite complex and introduce dependencies you might not want.
- Post-compile static IL-weaving, PostSharp and Mono.Cecil has an equivalent of Reflection.Emit, but this one doesn't have bugs for virtual methods calls in concrete subclasses (if I remember correctly) like Reflection.Emit and will gladly inspect your code similar to Assembly.ReflectionOnlyLoad and will also allow you to output IL-operations into that code. This is a good candidate if you are looking for a rather low-level approach; doesn't require as high trust.
- Adding extension points in your managed code for unmanaged callbacks to C/C++ through p/invoke, but this requires some thought as exceptions don't cross m/um memory-boundaries happily (rather, it will mess your application up), and unless you're using VC++/C# in Windows with managed exception framework this could seg-fault quite badly. You can pass callback to C and p/invoke into C from C# and probably pass callbacks from C to C# as well as long as you define the delegate in C#. The extension points would probably have to be done through a static or dynamic IL-weaver + point-cuts.
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.)