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