ABP框架系列之六:(Value-Objects-值對象)


Introduction

"An object that represents a descriptive aspect of the domain with no conceptual identity is called a VALUE OBJECT." (Eric Evans).

“表示沒有概念標識的域的描述方面的對象稱為值對象。”(Eric Evans)。

As opposite to Entities, which have their identities (Id), a Value Object has not it's identity. If identities of two Entities are different, they are considered as different objects/entities even if all other properties of those entities are same. Think two different persons have same Name, Surname and Age but they are different people if their identity numbers are different. But, for an Address (which is a classic Value Object) class, if two addresses has same Country, City, Street number... etc. they are considered as the same address.

與具有身份(id)的實體相反,值對象沒有它的標識。如果兩個實體的身份不同,即使這些實體的所有其他屬性相同,它們也被視為不同的對象/實體。認為兩個不同的人有相同的名字,姓氏和年齡,但他們是不同的人,如果他們的身份號碼是不同的。但是,對於一個地址(這是一個經典值對象)類,如果兩個地址具有相同的國家,城市,街道號…等等,它們被認為是同一個地址。

In Domain Driven Design (DDD), Value Object is another type of domain object which can include business logic and is an essential part of the domain.

在領域驅動設計(DDD)中,值對象是另一種類型的域對象,它可以包含業務邏輯,是域的重要組成部分。

Value Object Base Class

ABP has a ValueObject<T> base class which can be inherited in order to easily create Value Object types. Example Address Value Object type:

public class Address : ValueObject<Address>
{
    public Guid CityId { get; private set; } //A reference to a City entity.

    public string Street { get; private set; }

    public int Number { get; private set; }

    public Address(Guid cityId, string street, int number)
    {
        CityId = cityId;
        Street = street;
        Number = number;
    }
}

ValueObject base class overrides equality operator (and other related operator and methods) to compare two value object and assumes that they are identical if all properties are identical.

值類型繼承基礎類重載相等運算符(以及其他相關的操作和方法)比較兩個值類型,假設所有的特性都是相同的他們都是相同的。

So, all of these tests pass:

var address1 = new Address(new Guid("21C67A65-ED5A-4512-AA29-66308FAAB5AF"), "Baris Manco Street", 42);
var address2 = new Address(new Guid("21C67A65-ED5A-4512-AA29-66308FAAB5AF"), "Baris Manco Street", 42);

Assert.Equal(address1, address2);
Assert.Equal(address1.GetHashCode(), address2.GetHashCode());
Assert.True(address1 == address2);
Assert.False(address1 != address2);

Even they are different objects in memory, they are identical for our domain.

即使它們是內存中不同的對象,它們對於我們的域也是相同的。

Best Practices

Here, some best practices for Value Objects:

  • Design a value object as immutable (as like the Address above) if there is not a very good reason for designing it as mutable.
  • The properties that make up a Value Object should form a conceptual whole. For example, CityId, Street and Number shouldn't be serarate properties of a Person entity. Also, this makes Person entity simpler.
  • 如果沒有很好的理由將值對象設計為可變的,那么就要設計一個不可變的值對象(如上面的地址)。
    構成一個值對象的屬性應該構成一個概念整體。例如,cityid,街道和號碼不應該被一個人員實體分離。此外,這使實體更簡單。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM