Robert Lemke          Create Clean Code with Aspect-Oriented Programming          T3CON10 Frankfurt                       ...
Robert Lemke                chief architect of TYPO3 Phoenix and FLOW3                co-founder of the TYPO3 Association ...
= PHP 5.3 Full Stack Application FrameworkSamstag, 2. Oktober 2010
DI        Dependency Injection              DRY                                                                    YAA    ...
/**         * Creates a new post         *         * @param F3BlogDomainModelPost $newPost A fresh Post object which has n...
/**         * Creates a new post         *         * @param F3BlogDomainModelPost $newPost A fresh Post object which has n...
AOP               Create Clean Code with AOP   T3CON10 FrankfurtSamstag, 2. Oktober 2010
Aspect-Oriented Programming                           programming paradigm                           separates concerns to...
/**         * Creates a new post         *         * @param F3BlogDomainModelPost $newPost A fresh Post object which has n...
Concerns?               Create Clean Code with AOP   T3CON10 FrankfurtSamstag, 2. Oktober 2010
Concerns                           Separation of Concerns                             group features and behavior into man...
Cross-Cutting Concerns                           Logging                           Security                           Pers...
We dont want infrastructure code                in our models.               Create Clean Code with AOP           T3CON10 ...
We want to unit-test even                cross-cutting concerns               Create Clean Code with AOP   T3CON10 Frankfu...
We want to centralize                security-related code               Create Clean Code with AOP   T3CON10 FrankfurtSam...
AOP Lingo               Create Clean Code with AOP   T3CON10 FrankfurtSamstag, 2. Oktober 2010
Aspect                           Part of the application where cross-cutting concerns are implemented                     ...
Join Point                           Is a single point in the call graph                             Method Execution     ...
Pointcut                           A set of join points where advices could be executed                             can be...
Advice                           Action to take at a join points defined by the point cut               Create Clean Code w...
DEMO                                              Inspiring people to                Hitchhikers Guide to FLOW3   shareSam...
Kinds of Advice                Advice types supported by FLOW3:                @before                @afterreturning     ...
Pointcut Designators                method(F3MyPackageMyClass->myMethod())                class(F3MyPackageMyClass)       ...
Runtime Evaluations                evaluate(coffee.kind == "Arabica")                                                     ...
Compound Pointcuts!    /**!     * @around method(.*Controller->(new|create|edit|update|delete)Action()) &&             !me...
DEMO                                              Inspiring people to                Hitchhikers Guide to FLOW3   shareSam...
FLOW3s AOP implementation                           based on proxy classes                             unlike with most pr...
Inspiring people to                Hitchhikers Guide to FLOW3   shareSamstag, 2. Oktober 2010
Inspiring people to                Hitchhikers Guide to FLOW3   shareSamstag, 2. Oktober 2010
Progress                                  FLOW3 1.0.0               Create Clean Code with AOP       T3CON10 FrankfurtSams...
Further Reading                           FLOW3 Website                           http://flow3.typo3.org                   ...
Questions                Email:            robert@typo3.org                Blog:             http://robertlemke.de/blog   ...
Samstag, 2. Oktober 2010
Upcoming SlideShare
Loading in …5
×

Creating Clean Code with AOP (T3CON10)

683 views
618 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.

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

  • Be the first to like this

No Downloads
Views
Total views
683
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Creating Clean Code with AOP (T3CON10)

  1. 1. Robert Lemke Create Clean Code with Aspect-Oriented Programming T3CON10 Frankfurt /** Asp ect *S ecu rity *@ asp ect */ ityA spe ct { cla s s Se cur olic ies; tec ted $pSamstag, 2. Oktober 2010 pro
  2. 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 drummingSamstag, 2. Oktober 2010
  3. 3. = PHP 5.3 Full Stack Application FrameworkSamstag, 2. Oktober 2010
  4. 4. DI Dependency Injection DRY YAA OOP Object-Oriented Programming YAGNI AOP Aspect-Oriented Programming CoC MVC Model View Controller POPO Plain Old PHP Object TDDSamstag, 2. Oktober 2010
  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 T3CON10 FrankfurtSamstag, 2. Oktober 2010
  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->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 T3CON10 FrankfurtSamstag, 2. Oktober 2010
  7. 7. AOP Create Clean Code with AOP T3CON10 FrankfurtSamstag, 2. Oktober 2010
  8. 8. 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 T3CON10 FrankfurtSamstag, 2. Oktober 2010
  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 T3CON10 FrankfurtSamstag, 2. Oktober 2010
  10. 10. Concerns? Create Clean Code with AOP T3CON10 FrankfurtSamstag, 2. Oktober 2010
  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 everything Create Clean Code with AOP T3CON10 FrankfurtSamstag, 2. Oktober 2010
  12. 12. Cross-Cutting Concerns Logging Security Persistence Global Business Logic Dirty Hacks Create Clean Code with AOP T3CON10 FrankfurtSamstag, 2. Oktober 2010
  13. 13. We dont want infrastructure code in our models. Create Clean Code with AOP T3CON10 FrankfurtSamstag, 2. Oktober 2010
  14. 14. We want to unit-test even cross-cutting concerns Create Clean Code with AOP T3CON10 FrankfurtSamstag, 2. Oktober 2010
  15. 15. We want to centralize security-related code Create Clean Code with AOP T3CON10 FrankfurtSamstag, 2. Oktober 2010
  16. 16. AOP Lingo Create Clean Code with AOP T3CON10 FrankfurtSamstag, 2. Oktober 2010
  17. 17. Aspect Part of the application where cross-cutting concerns are implemented In FLOW3 aspects are classes annotated with @aspect Create Clean Code with AOP T3CON10 FrankfurtSamstag, 2. Oktober 2010
  18. 18. Join Point Is a single point in the call graph Method Execution Exception Represents an event, not a location Create Clean Code with AOP T3CON10 FrankfurtSamstag, 2. Oktober 2010
  19. 19. Pointcut A set of join points where advices could be executed can be composed can be named Create Clean Code with AOP T3CON10 FrankfurtSamstag, 2. Oktober 2010
  20. 20. Advice Action to take at a join points defined by the point cut Create Clean Code with AOP T3CON10 FrankfurtSamstag, 2. Oktober 2010
  21. 21. DEMO Inspiring people to Hitchhikers Guide to FLOW3 shareSamstag, 2. Oktober 2010
  22. 22. Kinds of Advice Advice types supported by FLOW3: @before @afterreturning @afterthrowing @after @around Inspiring people to Hitchhikers Guide to FLOW3 shareSamstag, 2. Oktober 2010
  23. 23. Pointcut Designators method(F3MyPackageMyClass->myMethod()) class(F3MyPackageMyClass) within(F3MyPackageMyInterface) classTaggedWith(someTag) methodTaggedWith(anotherTag) setting(Demo.Stuff.SomeSetting = "yeah, do it") filter(F3MyPackageMyCustomFilterImplementation) fi Inspiring people to Hitchhikers Guide to FLOW3 shareSamstag, 2. Oktober 2010
  24. 24. Runtime Evaluations evaluate(coffee.kind == "Arabica") Inspiring people to Hitchhikers Guide to FLOW3 shareSamstag, 2. Oktober 2010
  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 shareSamstag, 2. Oktober 2010
  26. 26. DEMO Inspiring people to Hitchhikers Guide to FLOW3 shareSamstag, 2. Oktober 2010
  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 to Hitchhikers Guide to FLOW3 shareSamstag, 2. Oktober 2010
  28. 28. Inspiring people to Hitchhikers Guide to FLOW3 shareSamstag, 2. Oktober 2010
  29. 29. Inspiring people to Hitchhikers Guide to FLOW3 shareSamstag, 2. Oktober 2010
  30. 30. Progress FLOW3 1.0.0 Create Clean Code with AOP T3CON10 FrankfurtSamstag, 2. Oktober 2010
  31. 31. 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-reading Create Clean Code with AOP T3CON10 FrankfurtSamstag, 2. Oktober 2010
  32. 32. Questions Email: robert@typo3.org Blog: http://robertlemke.de/blog Twitter: @t3rob Slides: http://slideshare.net/rlmp Create Clean Code with AOP T3CON10 FrankfurtSamstag, 2. Oktober 2010
  33. 33. Samstag, 2. Oktober 2010

×