Robert Lemke gave a presentation titled "Create Clean Code with Aspect-Oriented Programming" at the WebExpo 2010 conference in Prague. The presentation introduced aspect-oriented programming (AOP) and how it can be used to separate cross-cutting concerns in code to improve modularity. It provided an overview of AOP concepts like aspects, join points, pointcuts, and advices. Lemke also demonstrated AOP using the FLOW3 framework, explaining its implementation and features like advice types and pointcut designators. He argued that AOP allows infrastructure code to be removed from models and cross-cutting concerns to be centralized and unit tested.
1. Robert Lemke
Create Clean Code with Aspect-Oriented Programming
WebExpo 2010, Prague
Photo: Robert Szlivka
2. Robert Lemke
chief architect of TYPO3 Phoenix and FLOW3
co-founder of the TYPO3 Association
34 years old
lives in Lübeck, Germany
1 wife, 1 daughter, 1 espresso machine
likes drumming
10. DI Dependency Injection DRY
YAA
OOP Object-Oriented Programming
YAGNI
AOP Aspect-Oriented Programming
CoC
MVC Model View Controller
POPO Plain Old PHP Object TDD
11. /**
* Creates a new post
*
* @param F3BlogDomainModelPost $newPost A fresh Post object which has not yet been
* added to the repository
* @return void
*/
public function createAction(F3BlogDomainModelPost $newPost) {
if ($this->policyService->isGranted($this->currentUser, __CLASS__, __METHOD__) {
$this->blog->addPost($newPost);
$this->flashMessageContainer->add('Your new post was created.');
$this->systemLogger->log('A new post was created.', LOG_INFO);
$this->notificationService->notify('A new post was created.', 'robert@typo3.org');
} else {
$this->systemLogger->log('Someone tried to create a post.', LOG_WARNING);
throw new F3FLOW3SecurityExceptionAccessDeniedException('Tried to create.');
}
$this->redirect('index');
}
Create Clean Code with AOP WebExpo 2010, Prague
12. /**
* Creates a new post
*
* @param F3BlogDomainModelPost $newPost A fresh Post object which has not yet been
* added to the repository
* @return void
*/
public function createAction(F3BlogDomainModelPost $newPost) {
if ($this->policyService->isGranted($this->currentUser, __CLASS__, __METHOD__) {
$this->blog->addPost($newPost);
$this->flashMessageContainer->add('Your new post was created.');
$this->systemLogger->log('A new post was created.', LOG_INFO);
$this->notificationService->notify('A new post was created.', 'robert@typo3.org');
} else {
$this->systemLogger->log('Someone tried to create a post.', LOG_WARNING);
throw new F3FLOW3SecurityExceptionAccessDeniedException('Tried to create.');
}
$this->redirect('index');
}
Create Clean Code with AOP WebExpo 2010, Prague
15. Aspect-Oriented Programming
programming paradigm
separates concerns to improve modularization
OOP modularizes concerns into objects
AOP modularizes cross-cutting concerns into aspects
Create Clean Code with AOP WebExpo 2010, Prague
16. /**
* Creates a new post
*
* @param F3BlogDomainModelPost $newPost A fresh Post object which has not yet been
* added to the repository
* @return void
*/
public function createAction(F3BlogDomainModelPost $newPost) {
$this->blog->addPost($newPost);
$this->flashMessageContainer->add('Your new post was created.');
$this->redirect('index');
}
Create Clean Code with AOP WebExpo 2010, Prague
18. Concerns
Separation of Concerns
group features and behavior into manageable parts
have a specific purpose and business to take care of
Cross-Cutting Concerns
are the party poopers who want to have a say in everything
Create Clean Code with AOP WebExpo 2010, Prague
19. Cross-Cutting Concerns
Logging
Security
Persistence
Global Business Logic
Dirty Hacks
Create Clean Code with AOP WebExpo 2010, Prague
20. We don't want infrastructure code
in our models.
Create Clean Code with AOP WebExpo 2010, Prague
21. We want to unit-test even
cross-cutting concerns
Create Clean Code with AOP WebExpo 2010, Prague
22. We want to centralize
security-related code
Create Clean Code with AOP WebExpo 2010, Prague
24. Aspect
Part of the application where cross-cutting concerns are implemented
In FLOW3 aspects are classes annotated with @aspect
Create Clean Code with AOP WebExpo 2010, Prague
25. Join Point
Is a single point in the call graph
Method Execution
Exception
Represents an event, not a location
Create Clean Code with AOP WebExpo 2010, Prague
26. Pointcut
A set of join points where advices could be executed
can be composed
can be named
Create Clean Code with AOP WebExpo 2010, Prague
27. Advice
Action to take at a join points defined by the point cut
Create Clean Code with AOP WebExpo 2010, Prague
28. DEMO
Inspiring people to
Hitchhiker's Guide to FLOW3 share
29. Kinds of Advice
Advice types supported by FLOW3:
@before
@afterreturning
@afterthrowing
@after
@around
Inspiring people to
Hitchhiker's Guide to FLOW3 share
32. Compound Pointcuts
! /**
! * @around method(.*Controller->(new|create|edit|update|delete)Action()) &&
!methodTaggedWith(anybodyMayAccessThis)
! */
! public function interceptMethodCalls($joinPoint) {
...
}
Inspiring people to
Hitchhiker's Guide to FLOW3 share
33. DEMO
Inspiring people to
Hitchhiker's Guide to FLOW3 share
34. FLOW3's AOP implementation
based on proxy classes
unlike with most pre-processors line numbers stay the same
no scaffolding
0 Lines of generated code which need to be maintained by you
fast (on the second hit)
Inspiring people to
Hitchhiker's Guide to FLOW3 share