What's the difference between entities and Value Objects in DDD?

Source: Internet
Author: User
Tags in domain

As you begin to delve deeper and deeper into the world's computer programming you start to uncover lots and lots of new t Heories and Concepts.

One such idea, that isn ' t intuitively obvious is the Value Objects. A Value Object is an important concept in Domain driven Design (DDD).

For this article you don ' t has to worry about Domain driven Design or any of the related concepts as I ' m going to be pure Ly focusing on Value Objects. However, hopefully this was the first step towards a better understand of Domain driven Design in general.

Note: I ' m Assuming you have a good understanding of Object oriented programming. If not, you'll probably want to read up on the that before reading this article.

Entities Verses Value Objects

In the object oriented programming, we represent related attributes and methods as an object.

So for example, a person could is an Object within our application. A person would have a name, e-mail address and password as well as many other attributes. Within our database The is represented by an ID. This means is the person could change their name, e-mail and password but it would still is the same person. when an object can change it's attributes but remain the same object we call it an entity. an Entity was mutable because it can change it's attributes without changing the identity of the object. The Entity object would maintain the identity because it has an id in the database.

Imagine that we application allows the person-to-track their of current location. When the person was able to successfully connect to the Internet and authenticate with our application a new location objec T is created. This is a location object with attributes for longitude and latitude. The Location object was a Value object because we don ' t care about the specific instance of the object we are only care that it is a location.

When the person is changes location, we don't have to update the location object and we can simply create a new location object. The location object never changes it's attributes from the moment it's created until the moment it is destroyed. When an object ' s attributes cannot was changed, it is known as immutable.

Another important distinction is, Value Objects equality was not based upon identity. So for example if you create a objects with the same longitude and latitude attributes those the objects wil L is equal to one another. The person object in the other hand does base equality on identity because it's a single representation with an id . If you had and people with the exact same name, they would is not being the same person.

How to identify Value Objects?

So hopefully you can see that we can generally make the distinction between an Entity and a Value object if an object is represented with an ID.

An Entity's attributes can change, but it remains the same representation within our system because of it ' s unique identif Ier. Whereas a Value object is a single instance of an object, which is created, and then destroyed. We don ' t care about a specific instance of a Value Object and we can ' t change it ' s attributes.

So how does know when to use an Entity and when to use a Value Object? Well the decision really comes down to the context of the application.

Imagine The example from earlier, we application is not just a generic social application, it's actually foursqu Is. Now every individual location object does has a unique identifier because many different users can checkin to that Locati On over time. Now the Location object was an Entity and not a Value object.

On the other hand, imagine that we is the owner of a power plant that records activity around it's security fence. The security fence has many locations where activity was recorded for monitoring purposes. Each location around the fence are an Entity because we care about recording activity at those specific locations. Whenever a suspicious person walks past one's locations an incident are recorded in the database. In this example, the a person is a Value Object because we don ' t care about any particular person, we are only care that a person Triggered one of the security locations.

So whether an object was an Entity or a Value object really depends on the context of what you have is using it within your APPL Ication. Generally speaking objects like location, dates, numbers or money would nearly always being Value objects, and objects like PE Ople, products, files or sales would nearly always be entities.

Why is the distinction between Value Objects and Entity Objects important?

You were probably thinking, "Why is the distinction between Value Objects and Entity Objects important?".

Well it's actually really quite important for a number of reasons.

Firstly, when you have both entities with the same attributes, these, objects is not the same because they has a Ferent identities. However, when you have both Value Objects with the same values, these-Objects do has equality and can therefore can be interchanged freely. When you can substitute one object for another, the object was a value object (in other words, the value is in the object, Rather than the identity of the object). You couldn ' t interchange entities because there would is unwanted side effects.

Secondly, overtime an entity's properties would change, but it would remain the same Entity. For example, if a user changes their email address. However when your application needs to change a Value of object property, the whole object needs to being destroyed and a new on e should replace it. For example, when do a payment, the Money object is ' t given back to your as change, is given a new money object of a lower value.

Conclusion

The difference between entities and Value objects is a important concept in Domain driven Design. In DDD, it's important to identify the difference between entities and Value Objects in order to model the real world Corr ectly in our application.

As I mentioned in this post, it's important to fully understand the context of "what" is building so then you know when An object should is an Entity and when it should is a Value object. Just because at first glance and object would seem to a identity, does not mean the It should is an Entity. Modelling a concept as an Entity with the identity, when it should is an immutable Value Object can has unwanted side Effe Cts.

Immutable Value Objects is an important part of building a application that correctly represents the intended design. Using Value Objects for things such as money for example, also ensures that mistakes aren ' t made due to an object ' s Changi Ng state through time.

Understanding the difference between entities and Value Objects isn ' t always apparent, and would require that you fully get Your head around the context of the application you is building. However, the distinction is important, and is something so you should being aware of as you model a real world system as a New application in code.

What's the difference between entities and Value Objects in DDD?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.