$30 off During Our Annual Pro Sale. View Details »

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. Components Reconsidered
    by itosho / Connehito Inc.
    CakeFest 2021 Virtual🍰

    View Slide

  2. Hello, everyone!

    View Slide

  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

    View Slide

  4. About Our Company

    View Slide

  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

    View Slide

  6. Brand that supports mothers' each step
    Business Lineup

    View Slide

  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

    View Slide

  8. Have you ever used Components?

    View Slide

  9. When do you use Components?

    View Slide

  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

    View Slide

  11. Appendix
    - Code examples
    - https://github.com/itosho/x-cakefest21
    - PHP8.0, CakePHP4.2

    View Slide

  12. 1. What are Components?

    View Slide

  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

    View Slide

  14. Basic Usage of a Paginator Component

    View Slide

  15. General use case

    View Slide

  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

    View Slide

  17. Component as a shared logic package

    View Slide

  18. Unit testing of Component as a shared logic package

    View Slide

  19. Other use case

    View Slide

  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

    View Slide

  21. A HTTP Client and its unit testing

    View Slide

  22. Component as a service-like class

    View Slide

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

    View Slide

  24. 2. Other approaches

    View Slide

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

    View Slide

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

    View Slide

  27. As a shared logic package Pattern
    - Clean the controller code and reuse it (Repeated)
    - Expand the controller's behavior in horizontal direction

    View Slide

  28. Can we do something similar to it with Traits?

    View Slide

  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

    View Slide

  30. A Trait Pattern

    View Slide

  31. We did it!

    View Slide

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

    View Slide

  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.

    View Slide

  34. Can we do something similar to it with DI?

    View Slide

  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

    View Slide

  36. A HTTP Client using an interface

    View Slide

  37. A DI Pattern

    View Slide

  38. Unit testing of a DI Pattern

    View Slide

  39. We did it!

    View Slide

  40. 3. When to use Components

    View Slide

  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

    View Slide

  42. Whether you should use Components or Traits?

    View Slide

  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

    View Slide

  44. When to use Components
    - Better to use Components for a more three-dimensional shared logic

    View Slide

  45. When to use Components

    View Slide

  46. - Augment the request life-cycle.
    - initialize() 👉 beforeFilter(Component) 👉 beforeFilter(Controller) 👉 startup() 👉 …
    Component Callbacks
    Inheritance (y)
    Traits (x)
    Components (z)

    View Slide

  47. Whether you should use Components or DI?

    View Slide

  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!

    View Slide

  49. Compare with unit testing(Repeated)

    View Slide

  50. Components inferior to Traits and DI?

    View Slide

  51. Of course not

    View Slide

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

    View Slide

  53. It’s “Convention”

    View Slide

  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

    View Slide

  55. Summary

    View Slide

  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

    View Slide

  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

    View Slide

  58. Our journey to seek better software will continue

    View Slide

  59. Thank you!

    View Slide