Difference between a value object and an entity

Source: Internet
Author: User

Reference: http://culttt.com/2014/04/30/difference-entities-value-objects/

In common programs, objects (object) represent related properties and methods, for example, a person can become an object, he has a name, e-mail address and password, and other attributes. In the database this person is represented by an ID, which means that we can change his name, email address and password, but he is still the same person, when an object can change his attributes, but is still the same object, then we call it an ' entity ', an entity is Variable (mutable)because it can change his attributes without changing the identity of the object. The entity object will maintain identity because it has an ID in the database.

Imagine that our application allows us to track people's current location. When people can successfully connect to the Internet, our application creates a new location object that contains attributes with longitude and dimension. The object at that location is a ' value object 'because we don't care about the specific instance of the object, we only care about its location. At the same time, the properties of the object representing this position cannot be changed, and if its longitude or dimension changes a little, it cannot represent the position, and if the user's position is changed, then we can simply recreate the location object. When an object's properties cannot be changed, he is called ' immutable ' (immutable).

Another important difference is that the equality of value objects is not based on identity. For example, when you create a positional object where the latitude and longitude of the two-position object is the same, we consider the two positional objects to be equal. But the entities are different, because people of various IDs, although they have the same name, but they will not be the same person.

When do you use entities and when do they use value objects? Then the real decision is the context of the application.

Imagine, in the example above, if our application is not a common application, but rather a statistical application of the number of visitors to different locations. At this point, each location should have a unique identifier, to be stored as an entity in the database, but the person is not the entity we care about, we only care about someone passing through this place, then anyone for the system is the same.

So, whether an object is an entity or a value object really depends on how you use it in the context of your application. such as location, date, number, or currency, are generally value objects, while people, products, and files are almost all entities.

Difference between a value object and an entity

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.