Saturday, October 15, 2011

Aspect Oriented Programming

After some years of programming in top down (C language) and object oriented (C++, Java and C#) approaches, recently in one my assignment I had to work on new type of programming approach. Yes, as the blog title suggest, the new approach is known/called as Aspect Oriented Programming or simply AOP. When I heard this for the first time, I thought new kind of programming approach but I realized this approach is basically adding meta data to MSIL and then altering the execution behavior accordingly. But why do we need to do this? Why cannot we achieve the same with normal programming? Like these, there were many questions popped up in my mind. To get answers to these questions, I dived deep into this area and learned how we can use this model for enterprise applications. (AOP in .NET is achieved via custom attributes in .NET)

The Technical Problem – Cross-Cutting Concerns

All good programming practice aims to organize software code according the principle of separation of concerns. Ideally, each software requirement is implemented in a separate source code artifact. In object-oriented programming (OOP), the basic unit of organization is the class. Code in classes is segmented internally by fields (discrete units of information), methods (discrete units of functionality), and events (discrete units of reaction). Common families of classes are themselves organized into namespaces.

The standard OOP approach has proven an excellent model for code architecture and organization. It falls short, however, when developers must implement cross-cutting concerns – behaviors that are used across a large number of business requirements, and cannot easily be isolated.

Some of the cross-cutting concerns are logging, security auditing, transactions, multithreading etc.

The Effect of Cross-Cutting Concerns

In a typical OOP approach, cross-cutting behaviors are factored into separate classes, which are then instantiated and called directly by business logic code. This forces business logic code to be intimately aware of these cross-cutting concerns and creates numerous problems for the architecture and code quality of an OOP application

  1. The number of lines of code increase
  2. An enormous amount of code is duplicated
  3. Business logic code becomes entangled with cross-cutting code
  4. Business logic code is tightly coupled to cross-cutting concerns

The end result of this approach to cross-cutting concerns is a code base that is hard to author, hard to maintain, and hard to debug.

A Framework for Cross-Cutting Behaviors

These shortcomings aren't an indictment of the object-oriented paradigm, which has proven highly successful as a model of code architecture. What's needed is an enhancement to this paradigm that enables the addition of cross-cutting behaviors. Ideally, such an enhancement or framework would enable:

  • adding cross-cutting concerns with little or no modification to existing business logic code;
  • applying these behaviors to multiple methods, classes, or even namespaces and assemblies with minimal additional code;
  • enhancing cross-cutting logic without the need to revise code in the business object;
  • eliminating redundant boilerplate code that must be copied from method to method.

Meta Programming - Aspect Oriented Programming comes to rescue

Meta programming, which has been made popular by dynamic languages, allows programs to change their behavior extensively at both runtime and build time.

AOP is a specific case of meta-programming. However, AOP operates at a higher level of abstraction.

Aspect Oriented Programming – Introduction

The sole purpose of aspect-oriented programming (AOP) is to provide a cleaner separation of concerns in complex applications, especially where cross-cutting behavior is concerned.

AOP Terminology

Aspect: a modularization of a concern that cuts across multiple classes.Join point: a point during the execution of a program, such as the execution of a method or the handling of an exception. Advice: action taken by an aspect at a particular join point. Different types of advice include "around," "before" and "after" advice. Weaving: linking aspects with other application types or objects to create an advised object. This can be done at compile time, load time, or at runtime.

PostSharp – .NET framework for Aspect Oriented Programming

PostSharp is a .NET framework for Aspect Oriented Programming from SharpCrafters. It has very simple attributes for instrumentation, security, user interface, multithreading and many more.

PostSharp is the leading AOP framework in the Microsoft ecosystem. Several Microsoft products, such as Windows Communication Framework and ASP.NET MVC, provide a subset of AOP features, but Microsoft has always been reluctant to use the term aspect-oriented programming, preferring the terms “filters” or “behaviors” instead. And these are noting but custom attributes in .NET.

It takes more effort and time and also difficult for me to explain everything in this blog post, so posting some of the screenshots for easy overview. 

 

image

 

Logging:

image

Exception Handling:

image

Authorization:

image

NotifyPropertyChanged support (User Interface):

image

Undo and Redo:

image

Multithreading:

image

Thread synchronization:

image

Note: Some of the content in this blog post is from different internet resources.

No comments:

Post a Comment