DDD 从值对象列表中删除文本值对象
DDD Remove a Text Value Object from A List of Value Object
我有这样一个值对象列表的实体:
(我正在使用 Go,但我希望它通常有意义)
// this is my Crop entity
type Crop struct {
UID uuid.UUID
Name string
Type string
Notes []CropNote // This is a list of value object.
}
// This is my CropNote value object
type CropNote struct {
Content string
CreatedDate time.Time
}
我有 AddNewNote(content string)
的裁剪行为。但是业务流程也需要有删除注释行为。我在想类似 RemoveNote(content string)
的行为。所以我将迭代我的 Crop.Notes
,找到具有相同 content
的行,然后从 Crop.Notes
列表中删除该行。但我认为通过其注释的内容来查找值是容易出错的。从 API 的角度来看也很奇怪,因为我需要将内容发送到参数。
我的问题是,如何实现上面的删除注释行为?
编辑:
对不起,我想我没有清楚地解释自己。
我知道如何从切片中删除一个值。
我的问题是关于 DDD。关于如何从 Crop.Notes
列表中删除只有上述字段的值对象。因为我们知道Value Object不能有Identifier。
如果我真的只能使用我的值对象中的 Content
和 CreatedDate
字段,那么当我执行 REST API 奇怪的请求。
另外@WeiHuang 的回答:
如果你的笔记是一个无序列表,你可以使用 swap 而不是 append
或 copy
.
func (c Crop) removeNote(content string) {
j:=len(c.Notes)
for i:=0;i<j;i++ {
if c.Notes[i]==content {
j--
c.Notes[j],c.Notes[i]=c.Notes[j],c.Notes[i]
}
}
c.Notes=c.Notes[:j]
}
只需使用 CropNote 本身的实例即可:
/* sorry for sudo code, not up with GO */
func (c Crop) removeNote(noteToRemove CropNote) {
c.Notes= c.Notes.RemoveItem(noteToRemove); /* RemoveItem() is your own array manipulation code */
}
现在由您的应用层来识别并调用注释的删除。
需要考虑的其他事项:
为什么作物笔记是作物聚合根的一部分? Crop 的行为是受音符影响还是 Crop 行为影响音符?不要尝试在您的域内重建您的数据模型,这没有意义。如果您的系统需要独立的 adding/removing/updating 作物笔记,它们可能会更好地作为自己的聚合根,间接依赖于现有作物实体,例如:
/*again, not proficiant with GO - treat as sudo code */
private type CropNote struct {
UID uuid.UUID
CropUID uuid.UUID
Content string
CreatedDate time.Time
}
function NewCropNote(crop Crop, content string) *CropNote{
cn := new(CropNote)
cn.UUID = uuid.new()
cn.CropUID = crop.UUID
cn.CreatedDate = now()
cn.Content = content
return cn
}
我有这样一个值对象列表的实体:
(我正在使用 Go,但我希望它通常有意义)
// this is my Crop entity
type Crop struct {
UID uuid.UUID
Name string
Type string
Notes []CropNote // This is a list of value object.
}
// This is my CropNote value object
type CropNote struct {
Content string
CreatedDate time.Time
}
我有 AddNewNote(content string)
的裁剪行为。但是业务流程也需要有删除注释行为。我在想类似 RemoveNote(content string)
的行为。所以我将迭代我的 Crop.Notes
,找到具有相同 content
的行,然后从 Crop.Notes
列表中删除该行。但我认为通过其注释的内容来查找值是容易出错的。从 API 的角度来看也很奇怪,因为我需要将内容发送到参数。
我的问题是,如何实现上面的删除注释行为?
编辑:
对不起,我想我没有清楚地解释自己。
我知道如何从切片中删除一个值。
我的问题是关于 DDD。关于如何从 Crop.Notes
列表中删除只有上述字段的值对象。因为我们知道Value Object不能有Identifier。
如果我真的只能使用我的值对象中的 Content
和 CreatedDate
字段,那么当我执行 REST API 奇怪的请求。
另外@WeiHuang 的回答:
如果你的笔记是一个无序列表,你可以使用 swap 而不是 append
或 copy
.
func (c Crop) removeNote(content string) {
j:=len(c.Notes)
for i:=0;i<j;i++ {
if c.Notes[i]==content {
j--
c.Notes[j],c.Notes[i]=c.Notes[j],c.Notes[i]
}
}
c.Notes=c.Notes[:j]
}
只需使用 CropNote 本身的实例即可:
/* sorry for sudo code, not up with GO */
func (c Crop) removeNote(noteToRemove CropNote) {
c.Notes= c.Notes.RemoveItem(noteToRemove); /* RemoveItem() is your own array manipulation code */
}
现在由您的应用层来识别并调用注释的删除。
需要考虑的其他事项:
为什么作物笔记是作物聚合根的一部分? Crop 的行为是受音符影响还是 Crop 行为影响音符?不要尝试在您的域内重建您的数据模型,这没有意义。如果您的系统需要独立的 adding/removing/updating 作物笔记,它们可能会更好地作为自己的聚合根,间接依赖于现有作物实体,例如:
/*again, not proficiant with GO - treat as sudo code */
private type CropNote struct {
UID uuid.UUID
CropUID uuid.UUID
Content string
CreatedDate time.Time
}
function NewCropNote(crop Crop, content string) *CropNote{
cn := new(CropNote)
cn.UUID = uuid.new()
cn.CropUID = crop.UUID
cn.CreatedDate = now()
cn.Content = content
return cn
}