What are Aspects ?
- Aspects are reusable blocks of code that are injected into your application at runtime.
- These are very powerful tools for adding behavior to an application.
- Solve cross-cutting concerns in one place .
What is cross-cutting concern ?
Is a simple behavior that applies to many different points of our code base . in the other hand it is a set of business requirements that have words like every or always. moreover it look for system requirements that apply to multiple business requirements .
So when using Aspects, in addition to reduce the need to replicate that code many times, we have reduction the need to call into that code. Aspects by their nature are injected into our code at specific points of operation.
The common applications of Aspecting
There’s a few common applications of Aspecting, but you will find many more in your day-to-day operations. Some of the most common used cases that I’ve come into are things like :
- Transaction management
- Security .
Example of Logging
If we have a system level requirement that says any time the users logs in, log all of the actions that they perform, or things like every time we execute a database method, we want specific logging written out to our system logs.
These are cross-cutting concerns that apply to system requirements and these are great places to solve that concern with an Aspect.
Why using Aspects ?
- Remove duplication (E.g : Logging after any database action ).
- Prevent mixing concerns (Eg : If we have a method whose sole purpose is to go to the database and load the customer from the database, and then we add into it the security concerns and the logging concerns, we’re now mixing into our database access method other concerns that don’t apply to database access itself ) .
- Maintain application logic .