Aspect Oriented Programming (AOP)
Overview
The Aspect: Aspect is usually non functional functional requirement of system such as logging , security , monitoring etc.
Type of Aspects
Crosscutting Aspect
Code Repetition (DRY) Aspect
Code Generation Aspect
Aspect Oriented Programming
It is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting (Non Functional Requirements) concerns.
Core objective is to increase code reuse that are not possible by conventional OOPS techniques.
It helps to implement DRY Principle in more advance way beyond the capabilities of OOPS.
There are several frameworks that facilitates to AOP, for example WCF exposes several hooks (extention points) that we can leverage for the purpose of attaching aspects like logging.
AOP is a specific case of meta-programming.
Getting Started With AOP
AOP Implementation Approaches
AOP implementations are technically interceptors and can be implemented using two fundamental approaches.
Proxy/Decorator Based
Uses some short of decorator /proxy (Dynamic Proxy) combination to achieve the goal.
Easy to implement and no post compilation steps required.
Implementation is based on common interface requirement that restrict the uses scenario.
But advantage is that No Post Compilation steps are required.
Useful for limited number of classes and methods.
Most of the time implementation (Proxy/decorator) is achive via some IOC Containers.
Post Compilation (Instrumentation) Based
Uses IL Weaving to achieve the goal.
Support very complex scenarios and aspects.
Needs Post Compilation steps.
Can suport large number of classes and meathod.
Weaving Approachs
There are basically two approaches to change the behavior of a program in .NET (i.e. to weave a .NET program):
Compile-Time Weaving: the program is modified during the build process on the development machine, before deployment.
Runtime Weaving: the program is modified during its execution, after deployment.
Popular AOP Frameworks(Dot.Net)
Post Shorp (Top Rated)
Unity (Enterprise Library)
Spring.Net
Casel Windser
NInject
Comparison of AOP frameworks
Specific Considerations
AOP Vs Decorator Vs Proxy : AOP usually uses decorators and dynamic proxy to implement achieve it`s goal
AOP and Dynamic Proxy: AOP usually uses decorators and dynamic proxy to implement achieve it`s goal
AOP Vs Interceptors : Backbone of AOP is Interceptors.
Interceptor Vs Proxy : Interceptors can use proxy patterns as implementation technique.
Interceptor Vs Decorator : Interceptors can use decorators patterns as implementation technique that in turn may further use proxy for implementation.
Decorator Vs Proxy : Technically decorator is a series of chained proxies that are defined at compile time and loaded at run time.
AOP Vs AOD
AOP includes programming methods and tools that support the modularization of concerns at the level of the source code,
"aspect-oriented software development" refers to a whole engineering discipline.
AOP and DI:Several AOP implementation technique uses DI for attaching aspect to target objects.
Dynamic Proxy Vs Transparent Proxy : Transparent Proxy is a kind of Instance Proxy Generated dynamically (i.e. Dynamic Proxy) that is mainly used as interception technique by Unity Framework.
AOP Books
Aspectj in Action: Practical Aspect-Oriented Programming : Though this book is in JAVA but explains fundamentals very well.
AOP in .NET: Practical Aspect-Oriented Programming :Explains AOP implementation in Dot.Net, most implementation is based on PostSharp
References