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

What is the difference between Value vs Entity Objects in DDD?

Câu trả lời

In Domain-Driven Design (DDD), Value Objects and Entities are two fundamental building blocks that serve different purposes and have distinct characteristics. Here is a detailed explanation of the differences between them:

Identity

  • Entities: Entities have a unique identity that runs through their lifecycle. This identity distinguishes one entity from another, even if all their attributes are identical. For example, a User entity might be identified by a unique user ID[2][3][4].
  • Value Objects: Value Objects do not have a unique identity. They are defined solely by their attributes. Two Value Objects are considered equal if all their attributes are the same. For instance, an Address Value Object is identified by its properties like street, city, and postal code, not by a unique identifier[2][3][4].

Mutability

  • Entities: Entities are typically mutable. Their state can change over time, but their identity remains constant. For example, a Product entity might have its price or description updated, but it remains the same product[2][3][4].
  • Value Objects: Value Objects are immutable. Once created, their state cannot be changed. If you need to change a Value Object, you create a new instance with the new values. This immutability ensures that Value Objects can be safely shared and reused without unintended side effects[2][3][4].

Lifecycle

  • Entities: Entities have a lifecycle that includes creation, modification, and deletion. They often persist across different states and transactions. For example, a Customer entity might be created when a new customer registers, updated when their details change, and deleted if they close their account[2][3][4].
  • Value Objects: Value Objects have a limited lifecycle, often scoped to the lifecycle of the entity they belong to or a specific transaction. They do not exist independently and are typically created and destroyed as part of the operations on entities[2][3][4].

Equality

  • Entities: Equality for entities is based on their identity. Two entities are considered equal if they have the same unique identifier, regardless of their other attributes[2][3][4].
  • Value Objects: Equality for Value Objects is based on their attributes. Two Value Objects are considered equal if all their attributes are identical. This is known as structural equality[2][3][4].

Usage

  • Entities: Entities are used when the concept of identity and continuity across states is essential. They are suitable for modeling objects that have a distinct identity and can change over time, such as User, Order, or Product[2][3][4].
  • Value Objects: Value Objects are used to represent domain concepts that do not require distinct identity. They are often used as attributes of entities to encapsulate re...
middle

middle

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

junior

What is a Specification in DDD?

middle

What types of issues does an Aggregate solve in DDD?

senior

What is the difference between DAO and Repository in DDD?

Bình luận

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

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