In Object Oriented programming classes and methods are designed for performing specific operations and common/duplicate functionality are factored out into common classes. However, there are cross-cutting concerns that span across all classes and methods, like logging, caching, exception handling, security etc.
The separation of concerns is a major consideration for any programming language. In the simplest terms, a concern is a modular unit of code that has a specific purpose. All programming methodologies, in some way or another, handle the separation of concerns by encapsulating them into some entity. In procedural languages, concerns are defined within procedures. In object-oriented languages, concerns are defined within classes. However, some concerns cannot be bound to these constructs.
For example, in my User class, I have used the exception handling code for the methods as given below.
public class User : IUser
{
#region IUser Members
public void Insert(IUser user)
{
try
{
//Implementation code goes here
}
catch (Exception ex)
{
ExceptionPolicy.HandleException(ex,"My Exception Policy");
}
}
public void Remove(int index)
{
try
{
//Implementation code goes here
}
catch (Exception ex)
{
ExceptionPolicy.HandleException(ex, "My Exception Policy");
}
}
#endregion
}
During the process it was necessary for me to repeat the same code for Exception Handling in different functions. These types of cross-cutting concerns can be found in various regions of my code. Cross cutting concerns can occur in coding validation, caching etc also. Aspect oriented programming attempts to address these cross cutting concerns and eliminate repetitive and intermixing code in your application.
Aspect Oriented Programming model encapsulates these cross-cutting concerns using the following concepts
• Join-points: The points in the structure of base-code where the cross-cutting functionality needs to execute. This can be method call, functions, properties etc.
• Point-cut: A logical description of join-points using some specific syntax. Point-cut tells us when we should match a join point.
• Advice: Decides the sequence of execution of advice code with respect to joint point. Additional code like exception, logging, validation and security check that each of these methods need to perform
Implementing Aspect Oriented Programming in .NET
In .NET, two approaches can be taken in aspect-oriented programming, Compile-time weaving, and Runtime weaving. Weaving is injecting instructions into the application.
Compile-time weaving happens at the compiler level and Code is injected into the source code of an application to address the cross-cutting concerns. Runtime weaving is done by using the .NET runtime. Instructions are injected at runtime, typically through the use of a proxy object that the consuming object sees as the original object. This behaves the same way as Remoting does.
There are lot of posts and blogs available for implementing AOP in C#. In this article series (Part 2) I will try to explain how the Policy Injection Application Block (PIAB) can be used to separate the core concerns of the entity, such as business logic, from the cross-cutting concerns (Exception, Logging etc) that are required to develop an application.
No comments:
Post a Comment