Updating / Manipulating An Entity 更新/使用实体
Once an entity is created, it is updated/manipulated by the use cases until it is deleted from the system. There can be different types of the use cases directly or indirectly changes an entity.
一个实体一旦被创建,它就会被用例更新/操纵,直到它从系统中被删除。可能有不同类型的用例直接或间接地改变一个实体。
In this section, we will discuss a typical update operation that changes multiple properties of an Issue
.
在这一节中,我们将讨论一个典型的更新操作,即修改一个Issue
的多个属性。
This time, beginning from the Update DTO: 这次,从更新DTO开始:
By comparing to IssueCreationDto
, you see no RepositoryId
. Because, our system doesn't allow to move issues across repositories (think as GitHub repositories). Only Title
is required and the other properties are optional.
对比IssueCreationDto
,你看到没有RepositoryId
。因为,我们的系统不允许跨库移动问题(就像GitHub库)。只有Title
是必须的,其他的属性是可选的。
Let's see the Update implementation in the IssueAppService
: 让我们看看IssueAppService
中的更新实现:
UpdateAsync
method getsid
as a separate parameter. It is not included in theUpdateIssueDto
. This is a design decision that helps ABP to properly define HTTP routes when you auto expose this service as an HTTP API endpoint. So, that's not related to DDD.UpdateAsync
方法获得Id
作为一个单独的参数。它不包括在UpdateIssueDto
中。这是一个设计决定,当你将该服务自动暴露为HTTP API端点时,有助于ABP正确定义HTTP路由。所以,这与DDD没有关系。- It starts by getting the
Issue
entity from the database. - 它首先从数据库中获取
Issue
实体。 - Uses
IssueManager
'sChangeTitleAsync
instead of directly callingIssue.SetTitle(...)
. Because we need to implement the duplicateTitle
check as just done in the Entity Creation. This requires some changes in theIssue
andIssueManager
classes (will be explained below). - 使用
IssueManager
的ChangeTitleAsync
,而不是直接调用Issue.SetTitle(..)
。因为我们需要实现重复Title
的检查,就像刚才在实体创建中做的那样。这需要对Issue
和IssueManager
类进行一些修改(将在下面解释)。 - Uses
IssueManager
'sAssignToAsync
method if the assigned user is being changed with this request. - 如果指定的用户被改变,则使用
IssueManager
的AssignToAsync
方法。 - Directly sets the
Issue.Text
since there is no business rule for that. If we need later, we can always refactor. - 直接设置
Issue.Text
,因为没有跟它相关的业务规则。如果我们以后需要,我们可以随时重构。 - Saves changes to the database. Again, saving changed entities is a responsibility of the Application Service method that coordinates the business objects and the transaction. If the
IssueManager
had saved internally inChangeTitleAsync
andAssignToAsync
method, there would be double database operation (see the Discussion:Why is the Issue not saved to the database in IssueManager? above). - 将变化保存到数据库中。同样,保存改变的实体是负责协调业务对象和事务的应用服务方法的职责。如果
IssueManager
在ChangeTitleAsync
和AssignToAsync
方法中进行了内部保存,就会有两次数据库操作(见讨论:为什么IssueManager
中没有保存到数据库中的操作?) - Finally uses the
IObjectMapper
to return anIssueDto
that is automatically created by mapping from the updatedIssue
entity. - 最后使用
IObjectMapper
返回一个IssueDto
,这个IssueDto
是由更新后的Issue
实体通过映射自动创建的。
As said, we need some changes in the Issue
and IssueManager
classes.
如前所述,我们需要对Issue
和IssueManager
类进行一些修改。
First, made SetTitle
internal in the Issue
class: 首先,在Issue
类中将SetTitle
设置为内部的:
Then added a new method to the IssueManager
to change the Title
:
然后给IssueManager
添加了一个新的方法来改变Title
。