Urgent.News

What's breaking now, across thousands of outlets.

Tech

エンティティ・値オブジェクト・サービス・UseCase をざっくり整理

まぁRailsというか、DDD(ドメイン駆動設計)やクリーンアーキテクチャの範囲ですけどな 一覧 用語 ひとことで 例 エンティティ ID で見分けるもの ユーザー、案件、注文 値オブジェクト 中身で見分けるもの 金額、電話番号、住所、期間 サービスオブジェクト 手続きを入れる箱 税額を計算する、S3 にアップロードする UseCase ユーザーがやりたいこと1つ 案件を登録する、注文を確定する エンティティ ─ ID で見分けるもの 人は、名字が変わっても引っ越しても 同じ人 です。逆に、同姓同名・同い年の人が2人いても 別人 です。 このように、中身ではなく ID(その人であること)で見分けるもの がエンティティです。 user = User . find ( 1 ) user . update! ( name: '鈴木' ) # 名前が変わっても id: 1 のまま。同じユーザー…

Original Japanese Read in English

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.

Read the original at dev.to →

More in Tech

I Optimized a Button From O(n) to O(1). The Speed Didn't Matter.

A bug report, a route map, and three rounds of making it faster — a story about what Big O really measures, why constants win at small n, and why the best algorithm is sometimes deleting the question.

  • Original button had O(n) time complexity
  • Optimized with hash map to O(1) average time
  • Author concluded best algorithm may be deleting question

Why do we use primary keywords and hashtags in content creation and digital marketing?

Primary keyword This is the main phrase a page is built around, the thing you want it to rank for on Google. A write-up on your site might target “TryCloudflare phishing detection.” Why it matters…

  • Primary keywords centralize a page's focus to rank for specific search terms.
  • Hashtags categorize content on social media, expanding post reach and engagement.
  • Overuse of keywords or hashtags can negatively impact rankings and appear spammy.

Your Order API Needs More Than an Idempotency Key

An order API can accept an Idempotency-Key header on every request and still create a second order for the same intended purchase.

  • Idempotency key alone insufficient for single order guarantee
  • SQLite unique constraint on key ensures durability
  • Python script maintains single transaction for lookup & insertion

More from Saturday 31 October →