Friday 13 February 2009

Domain entities and value objects

Three weeks after my last post, it is time to go on with the DDD series.
In this short post I will talk about entities and value objects. These concepts are not new, but are the very important in DDD.

Value Objects

Value Objects are very simple objects which are in the domain for convenience. Nobody cares about the lifetime of these objects because they are very simple to build up again. Some examples of value objects are:
  • Color
  • Date/time
  • Money
The reason why they don't have a lifetime is because we don't care what instance we have. new Euro(10); and new Euro(10); have the same value, we don't care that they don't have the same reference, as long as we can say "We have both 10 euro" it is fine.

Entities

Entities are different. Entities have an identity. new Person("Jan"); and new Person("Jan"); are not the same. They have the same name, yes, but they don't have the same age, or the same length, perhaps they don't even have the same nationality.
This means an Entity is unique and a lifetime should be maintained (even when the application is closed). This means that we should be able to store the entity somewhere and request it again when we need it. And when we request the same entity twice, we need to get the same object.
In my monitoring system Devices and Services are entities. One device can only exist at one place and one Service can only be on one device (even though services can look the same, how we can solve this I will explain in another post :))

Lifetime management

Because entities have a lifetime we need some lifetime management. Although I will explain the following concepts in detail in another topic, I will mention them now to avoid some questions.
The lifetime of an entity is as follow:
  • An entity is created, this can be done by using the constructor, or, when it is more difficult and more than one entity has to be created, a Factory can be used.
  • An entity is registered to a Repository
  • To get a registered entity, we ask the Repository. We can also ask for a list of entities or search through entities.
  • When we want to delete an entity we tell the Repository to delete the entity.
In the next post we will talk more about the Factory and the Repository.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.