So lets say I have a class called Post that contains an IList
I find that it’s quite easy to cope with adding of Comments to the list when I ask my repository to update my post it can see which comments are new and send down to my data layer the required information, but what about when a comment is deleted? How do you handle this?
Would you pull back a list of comments to check which ones no longer exist in the current changed collection?
Or wire up events to keep track of it?
or something else entirely?
Just for further information, I’m doing this in C# and can’t use a O/R Mapper. I have to use stored procedures to retrieve datasets and map my objects manually. I may have my understanding of the repository pattern wrong, but I am using it to mediate to the data layer, from requesting data, adding data and such, and it maps my dataset data to objects and returns the object(s). So feel free to elaborate on how to use the Repository pattern as well if you wish.
If I understand what you are attempting, you are adding a Comment() to the IList in question which is attached to a Post(). And within your Post(), you are looking for any new Comment()s in the IList and saving them. Having the Post() object control its child objects, such as Comment()s, does go down the road of DDD.
I am still new to these patterns myself; but personally, I lean towards the concept that any entity that has metadata, I treat as its own entity model; therefore, I create my own repostiory for each entity model.
Now, having a IList Post.Comments I believe violates the Law of Demeter by allowing you to execute Post.Comments.Add().
I believe a solution to your problem would be not add to the IList, but instead create methods on the Post() to handle the comments related to that Post instance:
Then within your Post() object, you would wire up your ICommentRepository (most likely with a ServiceLocator, or I prefer Castle Windsor) and handle adding and deletion of them.
Again, I am still new to the DDD patterns; but, I believe this is keeps the Seperation of Concerns valid and masks the underlying logic by keeping it within the Post()’s concern to ‘only handle actions on comments related to this Post object’.
The complete Post() class would be something like this:
Please comment if others have opinions or changes.