[Translation] entity and the value of the object in the end is not the same thing?

[Translation] entities and objects of the same value it? (Is Entity the same as Value Object?)

原文: Is Entity the same as Value Object?

In this post, we’ll discuss an interesting question about whether the concepts of Entity and Value Object are the same.

In this article, we will discuss an interesting question, and the concept of entity value of an object is the same.

I wrote a lot about entities and value objects. Here’s the go-to post if you need to learn more about what they are and the differences between them: Entity vs Value Object: the ultimate list of differences. And to this date, I find interesting angles to look at these concepts from.

I wrote a bunch of articles about the entity and the value of the object if you need to know what they are and the differences between them, refer to the following article: Entity vs Value objects: the ultimate difference in the list so far, I've found. Some interesting angle to explore these concepts.

This one comes from Panos Kousidis who asked a insightful question in the comments to one of my posts about value objects:

This question comes from Panos Kousidis, he was my one about the value of the object article presents a very deep question in comments:

Can we consider an "Entity" as a "ValueObject" that compares only its Id for equality? Can this result in defining the base entity class as

Can we consider 实体regarded as 值对象only compare their Idequality whether an entity is defined as a base class? :

public abstract class Entity : ValueObject
{
   public int Id { get; protected set; }

   protected override IEnumerable<object> GetEqualityComponents()
   {
       yield return Id;
   }
}

This is a deep question which doesn’t have a quick answer, so let’s break it down into two parts. Here’s what differentiates entities from value objects (again, taken from this article):

This is a difficult question to answer, so we will be divided into two part article here about the difference between physical objects and values (or here, taken from. Article ):

  • Identity comparison
  • Identifier comparator.
  • Immutability
  • Immutability.
  • History preservation*
  • * Maintain history.

(Translator's Note: History preservation, historic maintain this translation yes. Tea aunt offered me the drive design: the core software complexity response to it (Rev.) did not find the corresponding translation, can also be translated as "historic preservation" , probably meaning the state change of the recording entity will be preserved.)

Let’s review the treatment of entities as value objects with regards to each of these two items.

Let us look again, a solid body as an object of value approach, which involves both (author refers 实体and 值对象) in each.

Identifier comparator (Identity comparison)

Identity comparison defines how two instances of a class compare to each other.

Identifier comparator how to define two instances of the class are compared with each other.

Entities are compared by their identifiers. Two objects are deemed equal if they have the same Id:

Entities are compared between each other by their identifiers. For two objects, if they have the same Id, it is considered equal.

Identifier equality

Identifier equality (equal identifier)

Value objects are compared by their content. Two value objects are deemed the same if their contents match:

To compare the value of the object based on their content. If the value of the contents of two objects are identical, they are considered equal.

Identifier equality

Structural equality (equal structure)

Note that although you usually compare value objects by all of their contents, it doesn’t have to always be the case. Some fields might not matter for identity comparison.

Note that, although usually by comparing the value of the object to compare their content, but not necessarily always the case. Some fields may not be important when comparing the identifier.

An example is the Error class I brought up in a recent article:

An example of this is my recent article mentioned Errorcategories:

public sealed class Error : ValueObject
{
    public string Code { get; }
    public string Message { get; }

    internal Error(string code, string message)
    {
        Code = code;
        Message = message;
    }

    protected override IEnumerable<object> GetEqualityComponents()
    {
        yield return Code; // the only field used for comparison (唯一用于比较的字段).
    }
}

This class contains two fields:

This class contains two fields (where disputed, the code should be in the above 属性, but the author often mix in this article field(字段)and property(属性)):

  • Code — this is what defines an error,
  • Message — for additional debug information just for developers.
  • Code - This defines the error code,
  • Message - only for developers additional debugging information.

If you pass errors to external systems, those systems shouldn’t ever bind to error messages, only to their codes. This is why the Error class uses only the Code field for identity comparison: changes in debug messages don’t matter; two errors with the same code are treated as the same error even if their messages differ.

If you pass the error to the external system, these systems should not be binding with an error message, but should only be bound to give them the error code that is why. ErrorClass only Codefield as an identifier comparison: change the debug information does not matter. having the same two error error code error is considered to be the same, even if their message.

This is where Panos Kousidis' question comes from too. If you can exclude some fields from a value object’s identity comparison, can you also exclude all of them (except for the Id) and end up with the code like the following?

This is the root of the problem Panos Kousidis If you compare the identifier values ​​of the objects excluded from some fields, does it also exclude all fields except Id, until they are similar to the following code?

public abstract class Entity : ValueObject
{
    public int Id { get; protected set; }

    protected override IEnumerable<object> GetEqualityComponents()
    {
        yield return Id;
    }
}

public class Customer : Entity
{
    public string Name { get; protected set; }
    public string Email { get; protected set; }
}

You definitely can, I don’t see any reason why not. So, from the identity comparison perspective, the answer to the question "Can you treat Entity and Value Object as the same concept?" is yes.

You can do it, I do not see any reason why not. Therefore, from the perspective of comparison of the identifier, " Can entities and value objects as the same concept? " The answer is " YES ".

Immutability (Immutability)

In terms of immutability, the difference between entities and value object is that value objects are immutable, whereas entities are almost always mutable. You don’t modify a value object; instead, you create a new one and replace the old instance with it.

On a non-denaturing, the difference value of the target entity and in that the value of the object is immutable, whereas almost always entity does not need to modify the value of the variable objects; Rather create a new object instance of a value to replace the old instance.

One could argue that immutability isn’t a defining property of a value object either. You could even apply the same line of reasoning as with identity comparison and say that what matters is immutability of the fields that form the value object’s identity, and that all other fields can be left mutable. In the example with the Error class, that would mean being able to change the Message field, but not Code.

Some might say, it is not immutable attribute defines the value of the object. You can even apply the same reasoning as to compare the identifiers , and said it was important immutability of the constitution identifier field value of the object, and all other fields can remain variable. in Errorthe example class, means may be modified Messagefield, but can not modify Codefield.

And it’s true that fields that form the object’s identity must not change. This requirement works similarly for entities and value objects:

Indeed, the object identifier field configuration can not be changed, this also applies to solid objects and values:

  • The modification of an entity’s Id field would turn that entity into a different one. Thus, such a modification is prohibited.
  • Modifying the physical entity Id field will become another entity. Accordingly, this modification is prohibited.
  • Similarly, the modification of fields that form a value object’s identity would, too, turn that value object into a different one.
  • Similarly, modifications to the configuration identifier field value of the object will change the value of an object into another object is a different value.

But what about the remaining fields? If we can change an entity’s properties (except for the Id one), can’t we also change the fields of a value object, as long as they aren’t part of its identity?

? If we can change one entity in addition to other fields it but the remaining Idproperty outside, so if we can modify the value of the object field, as long as those fields are not part of the value of the object identifier?

On the surface, it looks like we can, but this line of reasoning falls apart when you take into account the 3rd component that differentiates entities from value objects: history preservation.

On the surface, it seems we can do like this, but when considering the difference between a third entity and the value of the object: history maintain when this reasoning can not stand the scrutiny.

Maintain history (History preservation)

History preservation is whether or not an object has a history in your domain model.

Maintain history refers to whether an object has a history in the domain model.

Entities have such a history (even though you might not store it explicitly). In other words, entities live in a continuum: they are created, modified, and deleted — all at different points in time. Value objects don’t have a history; they are a mere snapshot of some state.

Entity would have such a history (even though you may not have to store it explicitly) In other words, the entity is continuously present: They are created at different points in time, modify, and delete objects no historical value, they are only one. a snapshot of some states.

The modification of a value object implicitly extends its lifetime beyond just being a snapshot. Such a modification assumes the value object also has a history, which goes against the requirement of not preserving history in value objects.

Modify the value of the object implicitly extend its lifetime, not just as a snapshot. Such modifications assumption that the value of the object also has a history, but in violation of the value of the object does not keep a history requirement.

History preservation is what answers the question of "Can you treat Entity and Value Object as the same concept?". That answer is no.

History is to maintain " 可以将实体对象和值对象视为同一个概念?the" answer to this question, the answer is no.

Summary (Summary)

The answer to the question of "Can we consider an entity as a value object that compares only its Id for equality?" boils down to three parts:

" Can we consider 实体regarded as 值对象only compare their Idequality? " The answer to this question can be summed up in three parts:

  • In terms of identity comparison, the answer is yes.
  • Is more identifiers, the answer is possible .
  • In terms of immutability, the answer is yes.
  • On the immutability, the answer is possible .
  • In terms of history preservation, the answer is no.
  • On Historic Preservation, the answer is not .

Thus, the overall answer is also no.

Therefore, the total answer is unacceptable .

Guess you like

Origin www.cnblogs.com/xixixiao/p/is-entity-same-as-value-object.html