エンティティ・値オブジェクト・サービス・UseCase をざっくり整理
まぁRailsというか、DDD(ドメイン駆動設計)やクリーンアーキテクチャの範囲ですけどな 一覧 用語 ひとことで 例 エンティティ ID で見分けるもの ユーザー、案件、注文 値オブジェクト 中身で見分けるもの 金額、電話番号、住所、期間 サービスオブジェクト 手続きを入れる箱 税額を計算する、S3 にアップロードする UseCase ユーザーがやりたいこと1つ 案件を登録する、注文を確定する エンティティ ─ ID で見分けるもの 人は、名字が変わっても引っ越しても 同じ人 です。逆に、同姓同名・同い年の人が2人いても 別人 です。 このように、中身ではなく ID(その人であること)で見分けるもの がエンティティです。 user = User . find ( 1 ) user . update! ( name: '鈴木' ) # 名前が変わっても id: 1 のまま。同じユーザー…
In essence, this article presents an overview of Entity, Value, Service and UseCase objects within the context of Ruby on Rails development, specifically in the context of Domain Driven Design and Clean Architecture.
Entities are the core of this system. They are identified by an ID, which distinguishes one entity from another, regardless of their attributes. For example, two people with the same name and birthdate are considered separate entities because their IDs are different. In Rails, most ActiveRecord models are entities.
Value objects, on the other hand, are defined by their attributes, not their identity. Take a ¥1000 note; two such notes are interchangeable irrespective of their source or any other attribute. This is the essence of a value object. For instance, a phone number could be a value object, where the number itself determines its identity.
It adheres to certain rules (like starting with '0' followed by 9 or 10 digits for a mobile number), cannot be changed once created, and has specific rules to determine its nature (mobile or landline).
Service objects, referred to as "boxes" for procedures, encapsulate specific actions, such as calculating tax or uploading a file to S3. They are designed to keep the models lean and focused on business logic. These objects can interact with entities and value objects, but never call services directly.
Use cases are classes representing the specific tasks a user wants to achieve in the application. For example, "register a case" or "confirm an order". These classes orchestrate the interaction between entities, value objects and services to accomplish the user's task. They are essentially the "director" of these operations, orchestrating the interaction without handling the underlying details.
In Rails, these use cases might also be referred to as operations, interactors or commands, but they all serve the same purpose - to manage a single user operation.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.