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 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)