Jan 17, 2009

Value object and Persistance

It is confusing to mixed value object with its persistence. We know value has has no identity. How about primary key in database. Every table in database has an primary key. Isn't that key the identity of value object if we want to persist the value object to database? Here we mix the domain model with the persistence.

In domain model, we care about object's behavior, persistence is irrelevant. For example, an order is an entity because we have a use case that get an order by an order id. But order row, it is not an entity because we don't have a use case to get order row without reference to other object. We need to have an order id, to get an order row. Although we can argue that in the order row table, each record still has an primery key( orderId, productId), but that key does not make the order row object an entity. Because it does not make sense to get the an order row without an order. Oder row is parasite of order. Contrary to this, customer is an entity, event Order (an entity object reference customer). Customer does not depend on the existence of an order.

Aggregates are groups of objects that work and live together. We group them along natural operational lines, and one entity serves as the aggregate root. The aggregate root serves as the entry point and the hub of operations for all objects in the aggregate. An aggregate can have many objects, or it can just be a single entity, but the aggregate root is always an entity since the aggregate root must be able to stand on its own, and only entities can stand on their own.