Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Create Clean Code with AOP

1,005 views

Published on

Slides from my talk at the International PHP Conference (Spring Edition) 2010 in Berlin.

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.

  • Be the first to comment

Create Clean Code with AOP

  1. 1. Robert LemkeCreate Clean Code with Aspect-Oriented ProgrammingInternational PHP Conference, Spring Edition 2010 Photo: Wolfgang Staudt
  2. 2. Robert Lemkechief architect of TYPO3 Phoenix and FLOW3co-founder of the TYPO3 Association34 years oldlives in Lübeck, Germany1 wife, 1 daughter, 1 espresso machinelikes drumming
  3. 3. = Full Stack Application Framework
  4. 4. DI Dependency Injection DRY LLATIP OOP Object-Oriented ProgrammingYAGNI AOP Aspect-Oriented Programming CoC MVC Model View Controller POPO Plain Old PHP Object TDD
  5. 5. /** * 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 IPC SE 2010, Berlin
  6. 6. /** * 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->authorizationService->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); $this->flashMessageContainer->add(Access denied.); } $this->redirect(index); } Create Clean Code with AOP IPC SE 2010, Berlin
  7. 7. AOPCreate Clean Code with AOP IPC SE 2010, Berlin
  8. 8. Aspect-Oriented Programming programming paradigm separates concerns to improve modularization OOP modularizes concerns into objects AOP modularizes cross-cutting concerns into aspectsCreate Clean Code with AOP IPC SE 2010, Berlin
  9. 9. /** * 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 IPC SE 2010, Berlin
  10. 10. Concerns?Create Clean Code with AOP IPC SE 2010, Berlin
  11. 11. 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 everythingCreate Clean Code with AOP IPC SE 2010, Berlin
  12. 12. Cross-Cutting Concerns Logging Security Persistence Global Business Logic Dirty HacksCreate Clean Code with AOP IPC SE 2010, Berlin
  13. 13. We dont want infrastructure codein our models.Create Clean Code with AOP IPC SE 2010, Berlin
  14. 14. We want to unit-test evencross-cutting concernsCreate Clean Code with AOP IPC SE 2010, Berlin
  15. 15. We want to centralizesecurity-related codeCreate Clean Code with AOP IPC SE 2010, Berlin
  16. 16. AOP LingoCreate Clean Code with AOP IPC SE 2010, Berlin
  17. 17. Aspect Part of the application where cross-cutting concerns are implemented In FLOW3 aspects are classes annotated with @aspectCreate Clean Code with AOP IPC SE 2010, Berlin
  18. 18. Join Point Is a single point in the call graph Method Execution Exception Represents an event, not a locationCreate Clean Code with AOP IPC SE 2010, Berlin
  19. 19. Pointcut A set of join points where advices could be executed can be composed can be namedCreate Clean Code with AOP IPC SE 2010, Berlin
  20. 20. Advice Action to take at a join points defined by the point cutCreate Clean Code with AOP IPC SE 2010, Berlin
  21. 21. DEMO Inspiring people toHitchhikers Guide to FLOW3 share
  22. 22. Kinds of AdviceAdvice types supported by FLOW3:@before@afterreturning@afterthrowing@after@around Inspiring people toHitchhikers Guide to FLOW3 share
  23. 23. Pointcut Designatorsmethod(F3MyPackageMyClass->myMethod())class(F3MyPackageMyClass)within(F3MyPackageMyInterface)classTaggedWith(someTag)methodTaggedWith(anotherTag)setting(Demo.Stuff.SomeSetting = "yeah, do it")filter(F3MyPackageMyCustomFilterImplementation) fi Inspiring people toHitchhikers Guide to FLOW3 share
  24. 24. Runtime Evaluationsevaluate(coffee.kind == "Arabica") Inspiring people toHitchhikers Guide to FLOW3 share
  25. 25. Compound Pointcuts! /**!  * @around method(.*Controller->(new|create|edit|update|delete)Action()) &&  !methodTaggedWith(anybodyMayAccessThis)!  */! public function interceptMethodCalls($joinPoint) { ... } Inspiring people to Hitchhikers Guide to FLOW3 share
  26. 26. DEMO Inspiring people toHitchhikers Guide to FLOW3 share
  27. 27. FLOW3s 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 toHitchhikers Guide to FLOW3 share
  28. 28. Inspiring people toHitchhikers Guide to FLOW3 share
  29. 29. Progress FLOW3 1.0.0Create Clean Code with AOP IPC SE 2010, Berlin
  30. 30. Further Reading FLOW3 Website http://flow3.typo3.org TYPO3 Forge http://forge.typo3.org Further Reading http://flow3.typo3.org/about/principles/further-reading http://bit.ly/GoodAOPArticleCreate Clean Code with AOP IPC SE 2010, Berlin
  31. 31. QuestionsEmail: robert@typo3.orgBlog: http://robertlemke.de/blogTwitter: @t3robFeedback: http://joind.in/1743Create Clean Code with AOP IPC SE 2010, Berlin

×