Save 37% off PRO during our Black Friday Sale! »

Components Reconsidered

Ad4c3c738e57a9bbcb800871e81c232d?s=47 itosho
October 08, 2021

Components Reconsidered

On CakeFest 2021 Virtual / By Sho Ito, Connehito Inc

Ad4c3c738e57a9bbcb800871e81c232d?s=128

itosho

October 08, 2021
Tweet

Transcript

  1. Components Reconsidered by itosho / Connehito Inc. CakeFest 2021 Virtual🍰

  2. Hello, everyone!

  3. About Me Sho ITO / @itosho - Japanese, live in

    Tokyo󰏦 - Like Basketball🏀, Football⚽ - Backend Engineer, CakePHP Contributor - My OSS: https://github.com/itosho/easy-query - CTO, Connehito Inc. - We use CakePHP in our backend system
  4. About Our Company

  5. Creating services that people cannot live without Connehito uses the

    power of technology to provide support to families in their daily decision makings about their matter
  6. Brand that supports mothers' each step Business Lineup

  7. No.1 app for mothers in Japan! One third of mothers

    in Japan are using mamari Communication amongst users about their worries and experiences about childbearing and childcare Q&A Community App Delivery of useful articles about childbearing and childcare. Many articles are supervised by experts Web Media A subscription service provides more useful functions e.g. medical telephone consultation, premium search functions Mamari Premium
  8. Have you ever used Components?

  9. When do you use Components?

  10. Today’s Topics Seeking better software through reconsideration of Components -

    1. What are Components? - General use case / Other use case - 2. Other approaches - Traits / Dependency Injection - 3. When to use Components - And when not to
  11. Appendix - Code examples - https://github.com/itosho/x-cakefest21 - PHP8.0, CakePHP4.2

  12. 1. What are Components?

  13. In short - Packages of logic that are shared between

    controller classes - Core Components are … - Flash Component - FormProtection Component - Paginator Component - Request Handler Component - Auth Compoent - Security Component
  14. Basic Usage of a Paginator Component

  15. General use case

  16. Component as a shared logic package - When we have

    a logic that is used by many controllers 👇 - Creating a Component and move the logic to the Component 👇 - Keep controller code clean and allow us to reuse code - Run a unit testing focused on that logic by separating the logic from controllers
  17. Component as a shared logic package

  18. Unit testing of Component as a shared logic package

  19. Other use case

  20. Component as a service-like class - When you call an

    external Web API from a controller - e.g. Qiita API which Japanese weblog service for engineers 👇 - Creating a HTTP Client and a Component - This component is a wrapper for the HTTP client and the controller 👇 - Remove the business logic from the controller - Making the HTTP client and the controller independent eases a unit testing
  21. A HTTP Client and its unit testing

  22. Component as a service-like class

  23. Unit testing of Component as a service-like class

  24. 2. Other approaches

  25. Summary so far - Two Components use cases - As

    a shared logic package - As a service-like class
  26. Summary so far - Two Components use cases - As

    a shared logic package - As a service-like class
  27. As a shared logic package Pattern - Clean the controller

    code and reuse it (Repeated) - Expand the controller's behavior in horizontal direction
  28. Can we do something similar to it with Traits?

  29. What are Traits? - A mechanism for code reuse in

    single inheritance languages - Enables horizontal composition of behavior - It is not possible to instantiate a Trait on its own Parent Class Child Class A Child Class B Trait A Trait B
  30. A Trait Pattern

  31. We did it!

  32. Summary so far - Two Components use cases - As

    a shared logic package - As a service-like class
  33. As a service-like class - Remove the business logic from

    the controller (Repeated) - Make a loose coupling between the HTTP client and the controller.
  34. Can we do something similar to it with DI?

  35. What are DI and DI container? - A technique for

    making our code loose coupled - By injecting an object from the outside - Make testing and managing our software easier - Depend on abstractions, not on concretions - DI container is a box that manages DI objects in one place Caller (Client) Object Concrete Object Abstract Object
  36. A HTTP Client using an interface

  37. A DI Pattern

  38. Unit testing of a DI Pattern

  39. We did it!

  40. 3. When to use Components

  41. Summary so far - Two Components use cases - As

    a shared logic package - As a service-like class - Two Other approaches - As a shared logic package: Components or Traits - As a service-like class: Components or Dependency Injection
  42. Whether you should use Components or Traits?

  43. When to use Traits - Better to use Traits for

    a horizontal shared logic - In many use cases, Traits are usually sufficient - Originally, business logic should not be written in Components - Better move the previous method to the model layer - If the logic is about Requests or Responses, Middleware might be able to replace it
  44. When to use Components - Better to use Components for

    a more three-dimensional shared logic
  45. When to use Components

  46. - Augment the request life-cycle. - initialize() 👉 beforeFilter(Component) 👉

    beforeFilter(Controller) 👉 startup() 👉 … Component Callbacks Inheritance (y) Traits (x) Components (z)
  47. Whether you should use Components or DI?

  48. When to use DI - Better to use DI for

    a service class that calls a HTTP client - Looser coupling between modules than the Service Locator pattern(=Components) - 🙋 This doesn’t mean denying Components - Since Cake3, the change to the policy of creating a local registry has been better!
  49. Compare with unit testing(Repeated)

  50. Components inferior to Traits and DI?

  51. Of course not

  52. What is in Components and not in Traits or DI?

  53. It’s “Convention”

  54. Convention over configuration - Convention is very effective in team

    development - Get a unified developer experience - Traits and DI are more challenging to maintain consistency - In other words, a skilled team can use the best of Traits and DI
  55. Summary

  56. - When to use Components and when not to My

    conclusion Team / Case Shared logic (Horizontal) Shared logic (Three-dimensional) Service-like class (Using HTTP Clients) Skilled team Traits Compoents DI Not skilled team Components Components Components
  57. - When to use Components and when not to -

    What do you think? My Conclusion Team / Case Shared logic (Horizontal) Shared logic (Three-dimensional) Service-like class (Using HTTP Clients) Skilled team Traits Compoents DI Not skilled team Components Components Components
  58. Our journey to seek better software will continue

  59. Thank you!