how should i add an object into a collection maintained by aggregate root

后端 未结 2 1203
小鲜肉
小鲜肉 2020-12-15 23:12

lets say i have a BlogPost aggregate root. it holds a List.
how should the BlogPost AddComment signature look? is it OK to use:

         


        
相关标签:
2条回答
  • 2020-12-15 23:53

    If you believe in DDD, it's perfectly fine to know about some entity beneath the aggregate root, as long as you do not store an ID or a reference to it somewhere outside of the aggregate.

    I would go for the blogPost.AddComment(new Comment(...))-version.

    0 讨论(0)
  • 2020-12-16 00:05

    If you consider Comment to be an aggregate of BlogPost and to not make sense out of that scope then you should be using the second example.

    The aggregate root should control how the aggregates are instantiated so their constructors should not be visible outside of the aggregate root.

    Plus, Comment should be a child class of BlogPost if you want a true relation of AggregateRoot-Aggregate.

    0 讨论(0)
提交回复
热议问题