我正在使用 DDD(在带有 TS 的 Node 中)编写我的第一个应用程序,并且我首先开始编写所有域——然后启动存储库/数据库,然后是应用程序,同时为每个实体编写单元测试。随着我的领域的发展,我开始对我的业务逻辑产生更多怀疑。下面我会举例说明。
我的一个业务逻辑指出“请求者”可以创建和删除标签。因此,因为我首先编写所有域,所以我在
createSectorTag
实体中创建了一个方法 Requester
。该方法定义如下:
public createSectorTag(data: ISectorTagData): number {
this.checkIfIsActive()
const sector_tag = new SectorTag(data)
return this.sector_tags.push(sector_tag)
}
如您所见,该方法创建了一个
SectorTag
实体并将其添加到我的 Requester
实体中的私有数组中。由于此方法中有一些逻辑,this.checkIfIsActive()
,我认为这段代码存在于我的域内是有意义的。但是,同时,我认为如果这对于一个简单的事情来说不是太多工作:我的意思是,每次我的应用程序调用我的域以请求请求者添加标签时,它都必须创建一个 Requester
实体,然后将其添加到 requester.sector_tags 中;之后,我必须获取新的 SectorTag
并使用存储库将其保存到数据库。
另一个例子是
deleteSectorTag
方法。此操作的逻辑应验证请求者内的任何请求是否具有该标记,如果有,则应引发异常。该方法的定义:
public deleteSectorTag(index: number): void {
/**
* Here I'll have to check if any of the requests inside the Request entity have
* the tag specified by the index in the parameter, and if so, raise an exception
*/
this.sector_tags.splice(index, 1)
}
但是,我再次相信所有这些业务逻辑都给处理增加了太多的负担。我必须从数据库获取请求者的所有请求,创建一个
Requester
实体,在实体内添加请求,然后进行验证。但这一切似乎都可能是数据库中的验证查询。
嗯,我真的很喜欢将所有逻辑放入域中的想法;这让我很高兴,因为我的代码似乎越来越接近现实。它适应了我的思维方式,但同时我担心性能。
如果有人能给我提示,我将非常感激。
这是完整的
Requester
实体:
import { BudgetRequest, SectorTag, BudgetEstimator } from '@/domain/BudgetRequest/entities'
import { RequesterIsInactiveError } from '@/domain/BudgetRequest/exceptions'
import {
IBudgetRequestData,
IRequesterData,
ISectorTagData,
} from '@/domain/BudgetRequest/interfaces'
export class Requester {
private readonly active: boolean
private readonly name: string
private readonly cnpj: number
private budget_requests: Array<BudgetRequest> = []
private sector_tags: Array<SectorTag> = []
private budget_estimators: Array<BudgetEstimator> = []
constructor(data: IRequesterData) {
this.active = data.active
this.name = data.name
this.cnpj = data.cnpj
}
get budgets_list(): Array<BudgetRequest> {
return [...this.budget_requests]
}
get sector_tags_list(): Array<SectorTag> {
return [...this.sector_tags]
}
public createBudgetRequest(data: IBudgetRequestData): number {
this.checkIfIsActive()
const budget_request: BudgetRequest = new BudgetRequest(data)
return this.budget_requests.push(budget_request)
}
public createSectorTag(data: ISectorTagData): number {
this.checkIfIsActive()
const sector_tag = new SectorTag(data)
return this.sector_tags.push(sector_tag)
}
public deleteSectorTag(index: number): void {
/**
* Here I'll have to check if any of the requests inside the Request entity have
* the tag specified by the index in the parameter, and if so, raise an exception
*/
this.sector_tags.splice(index, 1)
}
public bindSectorTagToBudgetRequest(i: number, j: number): void {
this.budget_requests[i].addSectorTag(this.sector_tags[j])
}
private checkIfIsActive(): void {
if (!this.active) {
throw new RequesterIsInactiveError()
}
}
}
我的一个业务逻辑指出“请求者”可以创建和删除标签。因此,因为我首先编写所有域,所以我在请求者实体中创建了一个 createSectorTag 方法。
实现domain模型并不意味着我们必须放弃明智的data模型。
如果您的实现需要将一堆实际上没有用的信息加载到内存中 - 这表明您的模型当前不太适合您的问题。
当两条信息之间存在关系时,你需要仔细思考三种情况
了解两条信息是否属于同一组的快速启发式方法:对其中一条信息进行更改是否需要锁定对另一条信息的所有更改?
在您的示例中:修改标签是否应该阻止对预算请求的更改?
如果答案是肯定的 - 这些信息始终保持一致对业务至关重要 - 那么将这些信息组合在一起就有意义了。
如果答案是否定的,那么您应该考虑独立锁定信息,这通常会涉及更多实体。
(提示:答案通常是“否”。)
您可以使用一个问题来区分两者:数据需要多快达成一致。如果您可以在 10 秒到一分钟之间的某个时间更改所有信息,并且领域专家说“是的,那很好”,那么这些信息当然不需要共享锁,您应该考虑以下候选设计:不,只是为了看看它是否会让事情变得更好。
此外,值得注意的是,我们的数据模型的某些部分实际上“只是”位于其他地方的信息缓存,我们真正需要做的就是使用通用工具(例如:级联删除)更新本地缓存的副本。
在这些地方,构建领域模型可能会花费更多的精力。
不要创建领域模型来解决贫乏的数据存储可以更好地解决的问题。