Value Objects and Their Usage with Entity Framework

Adem Catamak
.Net Programming
Published in
4 min readMar 23, 2021

Yazının Türkçe versiyonuna bu link ile ulaşabilirsiniz.

In this post, we will briefly talk about what Value Objects are and how we can use value objects when using Entity Framework.

Photo by Franck on Unsplash

What are Value Objects?

They are especially important in Domain Driven Design method. Structures that do not make sense on their own but carry information are called value objects. We can consider the date structure as an example. The combination of 3 numbers; day, month, and year, creates information that indicates a specific date. However, it is unclear what this date means. This issue will be more understandable when we talk about two main features of value objects.

1 —Identityless

Value objects are anonymous entities. So they keep data, but they don’t make sense on their own. If we consider the DateTime structure, it carries a date information. However, we cannot know what this date means when used alone. Value objects gain meaning when they are used by an entity. Let’s imagine user, order or shipment entities. Along with these entitties, the date information can have meanings such as the date of birth, the date the order was created, or the delivery date of a shipment.

2 — Immutable

While objects are being created, they receive data through constructor methods and these data cannot be changed again. We can explain this situation with the DateTime example. After creating the date object with day, month and year values, we cannot change these values. If we want to set a new date, we need to create a DateTime object again.

Benefits of Using Value Objects

Makes the design easier to understand

Looking at the snippet above, the second version of the user class is more readable. Grouping the data together into information will have a positive impact on the maintainability of the code.

Security in parallel programming

During parallel code execution, different threads may want to change the values that objects carry. In this case, “race condition” occurs. Due to the race condition, the program may produce different results in each execution. Since value objects are immutable, they cannot cause a race condition to occur.

Using Value Objects with Entity Framework

Under this section, we will take a look at the steps required to insert entities which hold value objects into the database and read these entities from database using Entity Framework.

You can access the code sample via the GitHub link.

Getting Started

For our example, we will create a class called Order. We will store where the order will be delivered by simply generating an Address value object that will have the country and city values.

1 — Creating the BaseValueObject Class

First of all, let’s start by designing a class called BaseValueObject. Below is a slightly modified version of the code snippet in the implement-value-objects article. This class contains the basic operations required for our value objects. We can see a piece of code for comparing whether two value objects are equal or not in the below example.

2 — Creating Address Value Object

Address value object obtains the necessary parameters through the constructor method. As it can be seen above, there is no other place where values ​​are set to Country and City fields other than the constructor method.

NOTE: Country and City fields must have private setter. The reason for this is that the values ​​brought from the database are required by the Entity Framework to be placed in these fields.

3 — Creating Order Entity

Let’s create a simple Order class with only username and address information.

4 — Configuring Relation Between Order Entity and Database

Let’s continue the process by creating an instance from the IEntityTypeConfiguration interface to determine the relationship of our order entity with the database tables and columns. With FluentApi, we can make an adjustment as follows.

We use OwnsOne method to use our value object with Entity Framework. If we do not specify the column name with the HasColumnNamemethod, by default the columns named as ‘Address_Country’ and ‘Address_City’ will be associated with the fields of our address value object. In the example above, we have stated that there are columns named Country and City in the database and our address value object is associated with these columns.

Insert Order Entity

As can be seen in the code snippet above, creating an instance of the Order class and saving it to the database is done in the same way as saving an entity that does not have a value object.

Query Order Entity

Equals method can be used to check equality of two value objects loaded in memory. I could not use the query method in the bottom line of the code snippet. Instead, I did my query for the fields corresponding to each column in the database, so the Entity Framework could successfully create and run my queries.

I hope it has been a useful post. You can click on this link to browse my other articles.

--

--