Creating Clean Code with AOP (WebExpo 2010)

40
WebExpo 2010, Prague Robert Lemke Create Clean Code with Aspect-Oriented Programming Photo: Robert Szlivka

description

OOP helps us creating a clearly laid out and intuitive model of the reality by means of objects. However, concerns like security, logging or transactions need to be implemented virtually anywhere, resulting in scattered error-prone code. Aspect-Oriented Programming separates these cross-cutting concerns from the rest of the code and lets you handle them in a well-known, central location.In this session you’ll get a first-hand introduction into FLOW3’s AOP implementation as well as related techniques such as Dependency Injection. You’ll be able to try out the examples given right after the presentation.FLOW3 is an application framework which was designed to be the foundation for the upcoming major version of TYPO3 CMS and in the meantime has become an Open-Source Project on its own. It brings a range of new development concepts to the PHP world which all aim for making development easier, faster and reduce complexity in big applications.

Transcript of Creating Clean Code with AOP (WebExpo 2010)

Page 1: Creating Clean Code with AOP (WebExpo 2010)

WebExpo 2010, Prague

Robert Lemke

Create Clean Code with Aspect-Oriented Programming

Photo: Robert Szlivka

Page 2: Creating Clean Code with AOP (WebExpo 2010)

Robert Lemkechief 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

Page 3: Creating Clean Code with AOP (WebExpo 2010)

www.typo3.org

Page 4: Creating Clean Code with AOP (WebExpo 2010)

www.typo3.org

Page 5: Creating Clean Code with AOP (WebExpo 2010)

Create Clean Code with AOP WebExpo 2010, Prague

www.typo3.org

Page 6: Creating Clean Code with AOP (WebExpo 2010)
Page 7: Creating Clean Code with AOP (WebExpo 2010)
Page 8: Creating Clean Code with AOP (WebExpo 2010)
Page 9: Creating Clean Code with AOP (WebExpo 2010)

= PHP 5.3 Full Stack Application Framework

Page 10: Creating Clean Code with AOP (WebExpo 2010)

OOP Object-Oriented Programming

AOP Aspect-Oriented Programming

MVC Model View Controller

POPO Plain Old PHP Object

DI Dependency Injection DRY

YAGNI

CoC

TDD

YAA

Page 11: Creating Clean Code with AOP (WebExpo 2010)

Create Clean Code with AOP WebExpo 2010, Prague

/** * Creates a new post * * @param \F3\Blog\Domain\Model\Post $newPost A fresh Post object which has not yet been * added to the repository * @return void */public function createAction(\F3\Blog\Domain\Model\Post $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.', '[email protected]'); } else { $this->systemLogger->log('Someone tried to create a post.', LOG_WARNING); throw new \F3\FLOW3\Security\Exception\AccessDeniedException('Tried to create.'); } $this->redirect('index');}

Page 12: Creating Clean Code with AOP (WebExpo 2010)

Create Clean Code with AOP WebExpo 2010, Prague

/** * Creates a new post * * @param \F3\Blog\Domain\Model\Post $newPost A fresh Post object which has not yet been * added to the repository * @return void */public function createAction(\F3\Blog\Domain\Model\Post $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.', '[email protected]'); } else { $this->systemLogger->log('Someone tried to create a post.', LOG_WARNING); throw new \F3\FLOW3\Security\Exception\AccessDeniedException('Tried to create.'); } $this->redirect('index');}

Page 13: Creating Clean Code with AOP (WebExpo 2010)

Create Clean Code with AOP WebExpo 2010, Prague

Page 14: Creating Clean Code with AOP (WebExpo 2010)

Create Clean Code with AOP WebExpo 2010, Prague

AOP

Page 15: Creating Clean Code with AOP (WebExpo 2010)

Create Clean Code with AOP WebExpo 2010, Prague

Aspect-Oriented Programmingprogramming paradigm

separates concerns to improve modularization

OOP modularizes concerns into objects

AOP modularizes cross-cutting concerns into aspects

Page 16: Creating Clean Code with AOP (WebExpo 2010)

Create Clean Code with AOP WebExpo 2010, Prague

/** * Creates a new post * * @param \F3\Blog\Domain\Model\Post $newPost A fresh Post object which has not yet been * added to the repository * @return void */public function createAction(\F3\Blog\Domain\Model\Post $newPost) { $this->blog->addPost($newPost); $this->flashMessageContainer->add('Your new post was created.'); $this->redirect('index');}

Page 17: Creating Clean Code with AOP (WebExpo 2010)

Create Clean Code with AOP WebExpo 2010, Prague

Concerns?

Page 18: Creating Clean Code with AOP (WebExpo 2010)

Create Clean Code with AOP WebExpo 2010, Prague

ConcernsSeparation 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

Page 19: Creating Clean Code with AOP (WebExpo 2010)

Create Clean Code with AOP WebExpo 2010, Prague

Cross-Cutting ConcernsLogging

Security

Persistence

Global Business Logic

Dirty Hacks

Page 20: Creating Clean Code with AOP (WebExpo 2010)

Create Clean Code with AOP WebExpo 2010, Prague

We don't want infrastructure codein our models.

Page 21: Creating Clean Code with AOP (WebExpo 2010)

Create Clean Code with AOP WebExpo 2010, Prague

We want to unit-test even cross-cutting concerns

Page 22: Creating Clean Code with AOP (WebExpo 2010)

Create Clean Code with AOP WebExpo 2010, Prague

We want to centralize security-related code

Page 23: Creating Clean Code with AOP (WebExpo 2010)

Create Clean Code with AOP WebExpo 2010, Prague

AOP Lingo

Page 24: Creating Clean Code with AOP (WebExpo 2010)

Create Clean Code with AOP WebExpo 2010, Prague

AspectPart of the application where cross-cutting concerns are implemented

In FLOW3 aspects are classes annotated with @aspect

Page 25: Creating Clean Code with AOP (WebExpo 2010)

Create Clean Code with AOP WebExpo 2010, Prague

Join PointIs a single point in the call graph

Method Execution

Exception

Represents an event, not a location

Page 26: Creating Clean Code with AOP (WebExpo 2010)

Create Clean Code with AOP WebExpo 2010, Prague

PointcutA set of join points where advices could be executed

can be composed

can be named

Page 27: Creating Clean Code with AOP (WebExpo 2010)

Create Clean Code with AOP WebExpo 2010, Prague

AdviceAction to take at a join points defined by the point cut

Page 28: Creating Clean Code with AOP (WebExpo 2010)

Inspiring people toshareHitchhiker's Guide to FLOW3

DEMO

Page 29: Creating Clean Code with AOP (WebExpo 2010)

Inspiring people toshareHitchhiker's Guide to FLOW3

Kinds of AdviceAdvice types supported by FLOW3:

@before@afterreturning@afterthrowing@after@around

Page 30: Creating Clean Code with AOP (WebExpo 2010)

Inspiring people toshareHitchhiker's Guide to FLOW3

Pointcut Designatorsmethod(F3\MyPackage\MyClass->myMethod())class(F3\MyPackage\MyClass)within(F3\MyPackage\MyInterface)classTaggedWith(someTag)methodTaggedWith(anotherTag)setting(Demo.Stuff.SomeSetting = "yeah, do it")ffiilter(F3\MyPackage\MyCustomFilterImplementation)

Page 31: Creating Clean Code with AOP (WebExpo 2010)

Inspiring people toshareHitchhiker's Guide to FLOW3

Runtime Evaluationsevaluate(coffee.kind == "Arabica")

Page 32: Creating Clean Code with AOP (WebExpo 2010)

Inspiring people toshareHitchhiker's Guide to FLOW3

Compound Pointcuts

! /**!  * @around method(.*Controller->(new|create|edit|update|delete)Action()) && ⏎ !methodTaggedWith(anybodyMayAccessThis)!  */! public function interceptMethodCalls($joinPoint) {

...}

Page 33: Creating Clean Code with AOP (WebExpo 2010)

Inspiring people toshareHitchhiker's Guide to FLOW3

DEMO

Page 34: Creating Clean Code with AOP (WebExpo 2010)

Inspiring people toshareHitchhiker's Guide to FLOW3

FLOW3's AOP implementationbased 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)

Page 35: Creating Clean Code with AOP (WebExpo 2010)

Inspiring people toshareHitchhiker's Guide to FLOW3

Page 36: Creating Clean Code with AOP (WebExpo 2010)

Inspiring people toshareHitchhiker's Guide to FLOW3

Page 37: Creating Clean Code with AOP (WebExpo 2010)

Create Clean Code with AOP WebExpo 2010, Prague

Progress

FLOW3 1.0.0

Page 38: Creating Clean Code with AOP (WebExpo 2010)

Create Clean Code with AOP WebExpo 2010, Prague

Further ReadingFLOW3 Websitehttp://flow3.typo3.org

FLOW3 Downloadhttp://flow3.typo3.org/downloadgit://git.typo3.org/FLOW3/Distributions/Base.git

TYPO3 Forgehttp://forge.typo3.org

Further Readinghttp://flow3.typo3.org/about/principles/further-reading

Page 39: Creating Clean Code with AOP (WebExpo 2010)

Create Clean Code with AOP WebExpo 2010, Prague

Questions

Email: [email protected]: http://robertlemke.de/blogTwitter: @t3rob

Slides: http://slideshare.net/rlmp

Page 40: Creating Clean Code with AOP (WebExpo 2010)