dongliao1949 2018-01-15 04:27
浏览 131
已采纳

DDD从值对象列表中删除文本值对象

I have entity with a list of value object like this:
(I'm using Go, but I hope it generally makes sense)

// 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
}

I have Crop behaviour for AddNewNote(content string). But the business process needs to have remove note behaviour too. I'm thinking something like RemoveNote(content string) behaviour. So I will iterate my Crop.Notes, find the row with the same content, then remove that row from the Crop.Notes list. But I think that finding the value by its note's content is error-prone. And its weird from the API point of view too because I need to send the content to the params.

My question is, how can I implement my Remove Note behaviour above?

EDIT: Sorry I think I'm not clearly explain myself.
I know how to remove a value from a slice.
My issue is about the DDD. About how to remove Value Object that only have fields above from the Crop.Notes list. Because we know that Value Object cannot have Identifier.
And if I really can only use Content and CreatedDate fields from my Value Object, then I should send that Content or CreatedDate value to the endpoint when I do REST API request which is weird.

  • 写回答

2条回答 默认 最新

  • dougua9165 2018-01-22 01:37
    关注

    Just use the instance of CropNote itself :

    /* 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 */
    }
    

    Now its up to your Application layer to identify and call the removal of the note.

    Additional thing to think about:

    Why is crop notes part of the Crop aggregate root? is the behavior of a Crop influenced by the notes or does Crop behavior influence the notes? Don't try and rebuild your data model inside your domain, it wont make sense. If your system requires the independent adding/removing/updating of crop notes, they may work better as their own aggregate roots that are indirectly dependent on an existing crop entity eg:

    /*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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证