Value Object vs Entity

What is Value Object, what is Entity and why should we care? Let’s take two examples, Currency and Person.

But before we start, let me give you an answer right away. Currency is value object and Person is entity.

Why?

Currency is something that has clear value that does not change easily. For example, Dollar is a currency and his ISO code is USD. It’s always going to be USD and that’s not changing any time soon. When we initiate the new dollar we want to be able to reuse that object all the time from now on in our application, because they should all be the same. We don’t distinguish between each dollar.

On the other hand each person is unique. When we create a new Person object, we have distinguishable properties like, name, age, birth day, picture, etc…

This is the first characteristic of recognizing what should be Value Object and what should be Entity. Value object is object that can be kept immutable.

Next, the object is value object if it’s conceptualy composing related attributes as an integral unit. Since this is a bit complicated definition let’s take an example and continue with currency. Let’s say we have a bank transaction. Each bank transaction has it’s currency.  Transaction is clearly an entity because each transaction is unique with unique id. We can make currency just one more attribute of transaction like this:

We can do this and it would be perfectly OK solution. The problem that we have here is that we will need to validate currency. If we know that objects should be responsible for validating itself, does it feel OK to You to have Transaction object be responsible for validating ISO code of currency? I didn’t think so. Btw, in this example amount should be extracted to a value object – Money. If we do that, Currency should not even be connected to transaction at all. Let’s see concrete example of this:

Value objects do behave as basic types in may cases but not in all. If you would do this you would get results that are not the same as if you would use basic types instead.

To avoid this we can just implement a simple equals method that would check if ISO code is equal.

One of the most important characteristic of value object, as we already said, is immutability. The question is how to achieve this?

I will try to explain again with an example. We have money value object with dollar currency and we want to add some money on top of it. We create new money value object with amount we want to add and currency. What would happen if that currency is euro. The request should fail.

This way we can be sure that immutability will be guaranteed. Each time two instances of Money are added, new instance of Money is returned. It will always be free of side-effects which is also one of the most important characteristic of value objects.

When testing value objects we need to take care that we also test immutability and side-effects free behavior. Let’s test our add method in Money class.

Using value object is highly recommended as it helps you plan conceptually your application. It’s important part of Domain Driven Design.

If you have questions, leave a comment or hit me up on twitter @housefrommars