Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AOP Fundamentals

Tags:

aop

Aspect-oriented programming is a subject matter that has been very difficult for me to find any good information on. My old Software Engineering textbook only mentions it briefly (and vaguely), and the wikipedia and various other tutorials/articles I've been able to find on it give ultra-academic, highly-abstracted definitions of just what it is, how to use it, and when to use it. Definitions I just don't seem to understand.

My (very poor) understanding of AOP is that there are many aspects of producing a high-quality software system that don't fit neatly into a nice little cohesive package. Some classes, such as Loggers, Validators, DatabaseQueries, etc., will be used all over your codebase and thus will be highly-coupled. My (again, very poor) understanding of AOP is that it is concerned with the best practices of how to handle these types of "universally-coupled" packages.

Question : Is this true, or am I totally off? If I'm completely wrong, can someone please give a concise, laymen explanation for what AOP is, an example of a so-called aspect, and perhaps even provide a simple code example?

like image 929
Eugie Avatar asked Jan 27 '11 17:01

Eugie


People also ask

What are the principles of AOP?

Aspect-Oriented Programming (AOP) presents the principle of the separation of concerns, allowing less interdependence, and more transparency. Thereby, an aspect is a module that encapsulates a crosscutting concern, and it is composed of pointcuts and advice bodies.

What are the types of AOP?

Types of AOP AdvicesAfter throwing advice: Advice to be executed if a method exits by throwing an exception. After advice: Advice to be executed regardless of the means by which a join point exits (normal or exceptional return). Around advice: Advice that surrounds a join point such as a method invocation.

What is AOP and examples?

AOP is like triggers in programming languages such as Perl, . NET, Java, and others. Spring AOP module provides interceptors to intercept an application. For example, when a method is executed, you can add extra functionality before or after the method execution.

What is this AOP?

An Acknowledgment of Paternity (AOP) is a legal document that allows parents who aren't married to establish legal paternity.


2 Answers

Separation of Concerns is a fundamental principle in software development, there is a classic paper by David Parnas On the Criteria To Be Used in Decomposing Systems into Modules that may introduce you to the subject and also read Uncle Bob's SOLID Principles.

But then there are Cross Cutting concerns that might be included in many use cases like authentication, authorization, validation, logging, transaction handling, exception handling, caching, etc that spawn all the layers in software. And if you want to tackle the problem without duplication and employing the DRY principle, you must handle it in a sophisticated way.

You must use declarative programming, that simply in .net could be annotating a method or a property by an attribute and what happened later is changing the behavior of code in runtime depending of those annotations.

You can find a nice chapter on this topic in Sommerville's Software engineering book

Useful links C2 wiki CrossCuttingConcern, MSDN, How to Address Crosscutting Concerns in Aspect Oriented Software Development

like image 129
Jahan Zinedine Avatar answered Sep 28 '22 00:09

Jahan Zinedine


AOP is a technique where we extract and remove the cross cutting concerns (logging, Exception handling, ....) from our code into it’s own aspect. leaving our original code focusing only on the business logic. not only this makes our code more readable, maintainable but also the code is DRY.

This can be better explained by an example:

Aspect Oriented Programming (AOP) in the .net world using Castle Windsor or Aspect Oriented Programming (AOP) in the .net world using Unity

like image 26
Mahmoud Ibrahim Avatar answered Sep 27 '22 22:09

Mahmoud Ibrahim