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条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c