You are on page 1of 2

AOP

---
Application Architecture
~~~~~~~~~~~~~~~~~~~~~~~~~
Account Controller <---> Account Service <---> Account DAO <--->Database
Scenario

Code Snippets for DAO - code snippet for save an entity


~~~~~~~~~~~~~~~~~~~~~
public void addAccount(Account theAccount, String userId){
Session currentSession=SessionFactory.getCurrentSession();
currentSession.save(theAccount);

Requirement 1 : From Boss


~~~~~~~~~~~~~~~~~~~~~~~~~
Need to add logging to our DAO methods.
- Add some logging statements before the start of the method.

How do we add logging in to code


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public void addAccount(Account theAccount, String userId){

//add code for logging

Session currentSession=SessionFactory.getCurrentSession();
currentSession.save(theAccount);

Requirement 2: From Boss


~~~~~~~~~~~~~~~~~~~~~~~~
Need to add security code to our DAO.
- Make sure user is authorized before running DAO method.

code snippets
~~~~~~~~~~~~~
public void addAccount(Account theAccount, String userId){

//add code for logging

//add code for security check

Session currentSession=SessionFactory.getCurrentSession();
currentSession.save(theAccount);

Now you can think of there are 3 layers in your application and all layers needs
to loggingand security
eg

Account Controller --> Account Service --> Account DAO --> DB


logging logging
logging
security security security

you will be copying code and paste it everywhere.

Two main Problems


~~~~~~~~~~~~~~~~~
For a given method : addAccount(...)
- We have to add logging and security to be implemented.
in line no 47 we have to write 2*3=6 times we have to repeat the code.

What is the solution?


Delegation.
~~~~~~~~~~
1. Classes would delegate logging , security calls.
2. Still would need to update classes if we wanted to
- add/remove logging or security
- add new feature like auditing , API management , instrumentation.

Programming technique based on concept of an Aspect.


Aspect encapsulates cross-cutting logic
- Cross-cutting concerns

"concern" means logic/functionality

You might also like