Try to understand difference between Entity vs Value Object, with Examples

If you ever worked on Entity Framework or Domain-driven design architecture pattern then you might be aware of entity vs value objects.

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest

Hi guys, Welcome back to Beetechnical. In this article, we will try to figure out the difference between Entity and Value object, using some examples.

If you ever worked on Entity Framework or Domain-driven design architecture pattern then you might be aware of entity vs value object(What is Entity and What is Value Object).

Value objects are nothing but, complex objects in Entity Framework.

On 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.

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

What is an Entity?

In terms of programing language, An entity can be any container class which has few properties with 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, 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");
           }

What will be the output?

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

What is a Value Object?

In simple terms, 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.

Let see some example of a Value object class.

  
  [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 carefully, 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 private set; means nobody can set any individual property by calling the instance of the project.
  4. By adding 2nd and 3rd point in the class. We are making FullName as immutable.

Why FullName is a Value object?

FullName is a value object because you can not change the 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 Value object is Address. You can not change the single property of Address object like only country, state or not even a zip code.

 

Let’s try to compare the Value objects

            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");
            }

 

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

Output : Value objects fullName and fullName2 are not equal

 

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

 

 

I hope you like this article. If you find any part is missing. Let me know in the below comment section. I will be more than happy to update this.

 

 

 

 

 

 

 

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on whatsapp
Share on email
Author Bio

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recommended for you
Connect With Us
Stay Connected

Subscribe to our newsletter today to receive updates on latest tech news, tutorials SEO tips many more.

Latest Articles
Featured image
Business

The Rise of Virtual Assistants (Infographic)

Small companies and start-up businesses resort to hiring virtual assistants as a solution to the limited time they have available. Here are some of the trends of why hiring or being a virtual assistant may be a good idea:

Free hub for guest blogging nerds and newbie writers

  • Submit Guest Post
  • Share your Knowledge
  • Build your Backlinks
  • Advertise your products
  • Make money by writing
Subscribe to our newsletter today to receive updates on latest tech news, tutorials SEO tips many more.