Aspect Oriented Programming (AOP
) is a term I’ve stumbled across a couple times before. My 2-second understanding of it was basically that you use attributes in your code to define how the code should behave. These attributes could do things like wrap the method in a transaction or log information about the method call.
So what’s the big deal about attributes? We already do that all over the place in .NET as we write web services, etc.
Well, I stumbled on a framework called Spring.NET
and read up on it. For some background… Spring.NET is a “spiritual port” of the widely used Spring framework for Java
. In the Java community, the framework helped reduce complexity and encourage best practices for web applications. It is widely accepted and used in many systems.
In reading about the framework, I learned about it’s core features of AOP, Inversion of Control
(IoC) and Dependency Injection
(DI). These features bring a pretty cool and very powerful feature to the table.
With AOP, I can apply any type of cross-cutting concern
to my code. I can do it with attributes on the methods, or I can do it via a config file. These cross-cutting concerns could be things like:
- Logging / auditing
- Exception handling / logging
- Instrumentation and Performance monitoring
How does this help?
Well, as a developer, now I don’t have to write this code inside every method. I don’t have to clutter it up with all this standard code that I’ve cut and pasted a thousand times. All I have to do is write the stuff that get’s the task done. The rest is a matter of attributes on the method or config file settings.
Taking this a step further…
The Spring.NET framework uses AOP for some very nice features:
- Make a Plain Old .Net Object (PONO) into a web service, remote object, or COM+ object. No special coding required. Just configure it via a config file.
- Cache the results of any method.
Caching is very simple via the various cache blocks, but it still takes about 15 lines of code to do it right. With an AOP attribute, I can cache any method via a single attribute or a config file setting.
- Automatic Retry of a method on failure.
This is useful for calls across a network boundary or in cases where a resource might be busy. Just add an attribute to have it try again.
Spring.NET is at version 1.1 and appears to be gaining traction in the community. It is Open Source, but is backed by Spring Source
who can provide paid support if needed. This makes it a viable option for enterprises and commercial use.
I plan to explore AOP a bit. More to come soon!