Câu hỏi phỏng vấn DDD
Câu hỏi

What is the distinction between Value Types and Entities in DDD?

Câu trả lời

In Domain-Driven Design (DDD), the distinction between Value Types (or Value Objects) and Entities is fundamental to modeling the domain effectively. Here are the key differences:

Entities

  1. Identity: Entities have a unique identity that runs through time and different states. This identity distinguishes one entity from another, even if all their attributes are identical. For example, two cars with the same make, model, and color are still different entities if they have different Vehicle Identification Numbers (VINs)[1][2][4][9].
  2. Mutability: Entities are typically mutable. Their state can change over time, but they remain the same entity because of their unique identity. For instance, a customer entity can change their address, but they are still the same customer[4][6][11].
  3. Lifecycle: Entities have a lifecycle that includes creation, modification, and deletion. They represent concepts that are tracked over time within the domain[4][11][14].

Value Objects

  1. No Identity: Value Objects do not have a unique identity. They are defined only by their attributes. If two Value Objects have the same attributes, they are considered equal. For example, two $5 bills are considered the same in terms of value, even though they might have different serial numbers[1][2][4][9].
  2. Immutability: Value Objects are usually immutable. Once created, their state cannot change. If a change is needed, a new instance of the Value Object is created. This immutability makes them inherently thread-safe and easier to reason about[2][4][10][16].
  3. Equality: Value Objects are compared based on their attributes (structural equality). If all attributes of two Value Objects are the same, the objects are considered equal. This is different from Entities, which are compared based on their identity (identifier equality)[9][10][14].
  4. Lifecycle: Value Objects do not have a lifecycle in the same sense as Entities. They are often created as part of an operation and then discarded. They represent a snapshot of a state rather than something that evolves over time[4][11][14].

Practical Examples

  • Entity Example: A "Customer" in a banking system is an entity. Each customer has a unique identifier (e.g., customer ID) and can change their address, phone number, etc., over time while remaining the same customer[11].
  • Value Object Example: An "Address" can be a Value Object. If two addresses have the same street, city, and postal code, they are considered the same...
junior

junior

Gợi ý câu hỏi phỏng vấn

senior

What is Core Domain, Supporting Subdomain, and Generic Subdomain in DDD?

senior

What is the difference between Factory and Repository in DDD?

junior

What is a Specification in DDD?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào