Value Object vs Entity in DDD Understand With Examples | 2023

In domain-driven design, a value object represents an attribute without identity, and it’s immutable. An entity has a unique identity, is mutable, and represents distinct objects in the domain.

Value Object vs Entity
Value Object vs Entity

What is an Entity?

In terms of programing language, An entity can be any container class that has few properties with a unique Id on it. Where Id represents the uniqueness of the entity class.

    public class Employee
    {
        public int EmpId { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
    }

As you can see in the above code block Employee class has three properties EmpId, Name, and Email. Where EmpId is the unique id in this class.

            Employee emp1 = new Employee()
            {
                EmpId =1,
                Name ="John",
                Email ="[email protected]"
            };
            Employee emp2 = new Employee()
            {
                EmpId = 1,
                Name = "Smith",
                Email = "[email protected]"
            };
            if (emp1 == emp2)
            {
                Console.Write("both the objects emp1 and emp2 are equal");
            }
           else
           {
              Console.Write("emp1 and emp2 are not equal");
           }
both the objects emp1 and emp2 are equal

The output will be both the objects emp1 and emp2 are equal. Even though both the objects have different names and emails, it will return true in comparison. Because as we already discussed Entity classes purely rely on the UniqueId column. In this case, our unique id column is EmpId, which is 1 for both objects.

What is a Value Object?

In simple terms, a value object is an object which relies on each property inside that class. It does not depend on any kind of unique property(Id property in case of Entity class) inside it.

Storing Entity vs Value object in Entity Framework?

Value objects are nothing but, complex objects in Entity Framework. On a High-level comparison, I will say Entities have an Id property while Value objects do not have an Id property, it purely relies on all the properties available in the class. You can mark the value object by using attributes or fluent API in EF Core.

Now, Let’s deep dive into each topic and  try to understand it one by one

  [DataContract]
  public class FullName
  {
      protected FullName()
      {
      }
      /// <summary>
      ///
      /// </summary>
      /// <param name="firstname"></param>
      /// <param name="middlename"></param>
      /// <param name="lastname"></param>
      public FullName(string firstname,string middlename, string lastname)
      {
          FirstName = firstname;
          MiddleName = middlename;
          LastName = lastname;
      }
      [DataMember]
      public string FirstName { get;private set; }
      [DataMember]
      public string MiddleName { get;private set; }
     [DataMember]
      public string LastName { get;private set; }
  }

As you can see in the above code block we have a class(Value Object) called FullName. It looks like a simple class. But, If you observe, you will find a few things

  1. FullName class does not have any Id property
  2. We don’t have a public constructor
  3. All the properties are private set; which means nobody can set any individual property by calling the instance of the project.
  4. By adding 2nd and 3rd points in the class. We are making FullName immutable.
Value object vs Entity Class in Entity Framework
Value Objects Are Immutable Objects

Why FullName is a Value Object?

FullName is a value object because you can not change a single property in it. you can not update only the first name, middle name, or last name of any person.
For Example :

John Stephen Hawking

If you update only the last name, it becomes. Which is completely a different name itself.

John Stephen Jones

Another best example of a Value object is the Address. You can not change the single property of Address objects like only country, state, or even zip code.

Value Object Compared to Entity Class Check all the properties for Comparison

            FullName fullName = new FullName("John", "Stephen", "Hawking");
            FullName fullName2 = new FullName("John", "Stephen", "Jones");
            if (fullName == fullName2)
            {
                Console.Write("both the value objects fullName and fullName2 are equal");
            }
            else
            {
                Console.Write("Value objects fullName and fullName2 are not equal");
            }

As we have already discussed. The value object relies on all its properties, not on a single unique identifier. So for sure output will be the statement written in the else block.

Value objects fullName and fullName2 are not equal

Quick Summary

So, in this article, we understood what is the Entity class, Valu Object, and Why the Value object is not the same as Entity classes. If you still have any doubts regarding this let me know in the below comment section. I will be happy to answer that.

Value Object using Record Keyword in C# 9

In C# you can create the value object using the record keyword introduced in C# 9.

In case you are interested in how to store the Value object in the database?

Conclusion

In conclusion, the choice between using a value object or an entity in software design depends on the specific requirements and characteristics of the domain model you are working with.


Frequently Asked Questions

What is the record keyword in C# 9?

In C# you can create the value object using the record keyword introduced in C# 9.

What is a Value Object?

In simple terms, a value object is an object which relies on each property inside that class. It does not depend on any kind of unique property(Id property in case of Entity class) inside it.

What is an Entity?

In terms of programing language, An entity can be any container class that has few properties with a unique Id on it. Where Id represents the uniqueness of the entity class.

Comments are closed.

Scroll to Top