Creating Clean Code with AOP (WebExpo 2010)

777 views
743 views

Published on

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.

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
777
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Creating Clean Code with AOP (WebExpo 2010)

  1. 1. Robert LemkeCreate Clean Code with Aspect-Oriented ProgrammingWebExpo 2010, Prague Photo: Robert Szlivka
  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. www.typo3.org
  4. 4. www.typo3.org
  5. 5. www.typo3.org Create Clean Code with AOP WebExpo 2010, Prague
  6. 6. = PHP 5.3 Full Stack Application Framework
  7. 7. DI Dependency Injection DRY YAA OOP Object-Oriented ProgrammingYAGNI AOP Aspect-Oriented Programming CoC MVC Model View Controller POPO Plain Old PHP Object TDD
  8. 8. /** * 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
  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) { 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
  10. 10. Create Clean Code with AOP WebExpo 2010, Prague
  11. 11. AOPCreate Clean Code with AOP WebExpo 2010, Prague
  12. 12. 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 WebExpo 2010, Prague
  13. 13. /** * 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
  14. 14. Concerns?Create Clean Code with AOP WebExpo 2010, Prague
  15. 15. 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 WebExpo 2010, Prague
  16. 16. Cross-Cutting Concerns Logging Security Persistence Global Business Logic Dirty HacksCreate Clean Code with AOP WebExpo 2010, Prague
  17. 17. We dont want infrastructure codein our models.Create Clean Code with AOP WebExpo 2010, Prague
  18. 18. We want to unit-test evencross-cutting concernsCreate Clean Code with AOP WebExpo 2010, Prague
  19. 19. We want to centralizesecurity-related codeCreate Clean Code with AOP WebExpo 2010, Prague
  20. 20. AOP LingoCreate Clean Code with AOP WebExpo 2010, Prague
  21. 21. Aspect Part of the application where cross-cutting concerns are implemented In FLOW3 aspects are classes annotated with @aspectCreate Clean Code with AOP WebExpo 2010, Prague
  22. 22. Join Point Is a single point in the call graph Method Execution Exception Represents an event, not a locationCreate Clean Code with AOP WebExpo 2010, Prague
  23. 23. Pointcut A set of join points where advices could be executed can be composed can be namedCreate Clean Code with AOP WebExpo 2010, Prague
  24. 24. Advice Action to take at a join points defined by the point cutCreate Clean Code with AOP WebExpo 2010, Prague
  25. 25. DEMO Inspiring people toHitchhikers Guide to FLOW3 share
  26. 26. Kinds of AdviceAdvice types supported by FLOW3:@before@afterreturning@afterthrowing@after@around Inspiring people toHitchhikers Guide to FLOW3 share
  27. 27. 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
  28. 28. Runtime Evaluationsevaluate(coffee.kind == "Arabica") Inspiring people toHitchhikers Guide to FLOW3 share
  29. 29. 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
  30. 30. DEMO Inspiring people toHitchhikers Guide to FLOW3 share
  31. 31. 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
  32. 32. Inspiring people toHitchhikers Guide to FLOW3 share
  33. 33. Inspiring people toHitchhikers Guide to FLOW3 share
  34. 34. Progress FLOW3 1.0.0Create Clean Code with AOP WebExpo 2010, Prague
  35. 35. Further Reading FLOW3 Website http://flow3.typo3.org FLOW3 Download http://flow3.typo3.org/download git://git.typo3.org/FLOW3/Distributions/Base.git TYPO3 Forge http://forge.typo3.org Further Reading http://flow3.typo3.org/about/principles/further-readingCreate Clean Code with AOP WebExpo 2010, Prague
  36. 36. QuestionsEmail: robert@typo3.orgBlog: http://robertlemke.de/blogTwitter: @t3robSlides: http://slideshare.net/rlmpCreate Clean Code with AOP WebExpo 2010, Prague

×