Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Components Reconsidered

itosho
October 08, 2021

Components Reconsidered

On CakeFest 2021 Virtual / By Sho Ito, Connehito Inc

itosho

October 08, 2021
Tweet

More Decks by itosho

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. Summary so far - Two Components use cases - As

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

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

    code and reuse it (Repeated) - Expand the controller's behavior in horizontal direction
  11. 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
  12. Summary so far - Two Components use cases - As

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

    the controller (Repeated) - Make a loose coupling between the HTTP client and the controller.
  14. 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
  15. 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
  16. 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
  17. When to use Components - Better to use Components for

    a more three-dimensional shared logic
  18. - Augment the request life-cycle. - initialize() 👉 beforeFilter(Component) 👉

    beforeFilter(Controller) 👉 startup() 👉 … Component Callbacks Inheritance (y) Traits (x) Components (z)
  19. 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!
  20. 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
  21. - 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
  22. - 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