Entity vs ValueObject (Notepad Diaries #1)

Mesut Atasoy
2 min readDec 14, 2020

--

https://tr.pinterest.com/pin/844493653351673/

I’ve decided to write as “Notepad Diaries” series the diary notes that I kept in. These posts will be short and clear as far as possible. It will be included theoretical and practical subjects about Software Development.

In this post, We’ll discuss about difference between Entity and Value Objects.

Before the discussion, I’d like to tell three types of object equality in simple way.

  • Identifier equality: Objects are compared with Id field.
  • Reference equality: Objects are compared with their addresses in memory.
  • Structural equality: Objects are compared with their all members.

Identifier equality refers only Entity. Structural equality refers only Value Object. That’s way, Value Object do not have Id.

As we compare with mutuable and immutable, Value object must be immutable and will not have as table in database. So, In order to change any attributes in value object, you need to have a new instance of it. Entities can be mutable. Value objects should belong to entities.

Last thing I can say, I’d like to remind that Value object is a very important concept in DDD.

So, let’s practice with Entity and Value object in .Net CORE Framework. It has supported in Entity Framework Core 2.0 and later versions. Further more information about value object, please take a look offical document in Microsoft.

Value Object with equal operator functions.
Person Entity
Person Entity Mapping Configuration

While we configure person entity, We define value object with OwsOne.

I hope you enjoy while reading the post.

Thanks!

--

--