快速理解聚合根、实体、值对象的区别和联系

转自:https://www.cnblogs.com/netfocus/p/5145345.html

快速理解聚合根、实体、值对象的区别和联系_第1张图片
image.png

---------------------------分割线------------------------------------------
程序操作上的理解:
值对象不需要在DbContext里面写DbSet,比如Address是值对象,不需要在DbContext里面写 public DbSet

Addresss { get; set; }

//值对象(不需要有主键)
public class Address
{
    #region Properties
    // 国家
    public string Country { get; set; }

    //省份
    public string State { get; set; }

    // 市
    public string City { get; set; }

    public string Street { get; set; }

    public string Zip { get; set; }

    #endregion
}

使用值对象的领域:

public class User:AggregateRoot
{
    public string UserName { get; set; }
    public string Password { get; set; }

    public string Email { get; set; }

    public string PhoneNumber { get; set; }

    public bool IsDisabled { get; set; }

    public DateTime RegisteredDate { get; set; }

    public virtual Address UserAddress { get; set; }//值对象
}

生成的数据表会是:

快速理解聚合根、实体、值对象的区别和联系_第2张图片
image.png

你可能感兴趣的:(快速理解聚合根、实体、值对象的区别和联系)