# 领域层之值对象

### 用来描述一个事物的特征，没有唯一标识的对象

&#x20;譬如用户的QQ、手机等。其实除了唯一标志，其他任何字段都可以是值对象

#### 值对象有2个重要原则

1、实体只能通过ID（唯一标识） 来判断两者是否相同

2、而值对象。只需根据“值”就能判断两者是否相等

#### 不可变的特性

&#x20;修改值对象。必须传入新对象

### 来个示例：

首先这是实体 -- 用户实体

```
type UserModel struct {
   UserId int `gorm:"column:user_id;primary_key;auto_increment" json:"user_id"  xorm:"'user_id'"`
   UserName string `gorm:"column:user_name" json:"user_name" xorm:"'user_name'"`
   Extra *valueobjs.UserExtra   `gorm:"embedded"`//值对象
 
}
```

这是值对象-- 用户值对象，包含了QQ、手机等

```
type UserExtra struct {
   UserPhone string `gorm:"column:user_phone" json:"user_phone" xorm:"'user_phone'"`
   UserQQ string `gorm:"column:user_qq" json:"user_qq" xorm:"'user_qq'"`
   UserCity string `gorm:"column:user_city" json:"user_city" xorm:"'user_city'"`
}
```

在这里面，假设userid 是唯一标识

那么&#x20;

user1:=\&UserModel{UserId:101,UserName:"lisi"}

user2:=\&UserModel{UserId:102,UserName:"lisi"}

这两个实体，哪怕都叫lisi。但是ID不同，所以这两个实体是不一样的

但是如果这两个实体的QQ、手机和所在城市都一样，那么可以认为这两个用户实体的“Extra值”是一样的


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://65480539.gitbook.io/goft/jing-jian-ban-ling-yu-qu-dong/ling-yu-ceng-zhi-zhi-dui-xiang.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
