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
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
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
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
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
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
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
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
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
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!
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
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
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