2009-02-17 51 views
1

当所以可以说我有一个名为Post类,它包含一个IListRepository模式和项目删除使用SQL后端

我觉得这是很容易应付增加评论到列表中时,我问我的存储库更新我的帖子,它可以看到哪些评论是新的,并发送到我的数据层所需的信息,但是当评论被删除时怎么办?你如何处理这个问题?

您是否会撤回注释列表以检查当前更改的集合中哪些不再存在?

或者连线事件以跟踪它?

还是其他什么东西?

只是为了获得更多信息,我在C#中这样做,并且不能使用O/R映射器。我必须使用存储过程来检索数据集并手动映射我的对象。我可能对存储库模式的理解错误,但我使用它来调解数据层,从请求数据,添加数据等等,并将我的数据集数据映射到对象并返回对象。因此,如果您愿意,可以随时阐述如何使用Repository模式。

回答

0

如果我明白你正在尝试什么,那么你将添加一个Comment()给附加到Post()的IList。在你的Post()中,你正在IList中寻找任何新的Comment()并保存它们。让Post()对象控制它的子对象,如Comment(),就会走向DDD的道路。

我自己对这些模式仍然陌生;但个人而言,我倾向于将任何具有元数据的实体作为自己的实体模型;因此,我为每个实体模型创建了自己的存储库。

Post() 
PostRepository : IPostRepository 

Comment() 
CommentRepository : ICommentRepository 

现在,有IList的Post.Comments我相信允许执行Post.Comments.Add()违反了迪米特的法。

我相信你的问题的解决方案会不会增加IList的,而是对早报创建()的方法来处理与该帖子实例的评论:

Post.AddComment() 
Post.FetchComments() 
Post.DeleteComments(IList<Comment> comments) 

您的文章中( )对象,你会连接你的ICommentRepository(最有可能与ServiceLocator,或者我更喜欢Castle Windsor)并且处理它们的添加和删除。

ICommentRepository.AddByPostID() 
ICommentRepository.FetchByPostID() 
ICommentRepository.Remove(int commentID) 

再说一遍,我对DDD模式仍然陌生;但是,我相信这是通过保持Post()关心的“仅处理与该Post对象有关的评论的操作”来保持关注分离的有效性并掩盖其基础逻辑。

完整的帖子()类将是这样的:

private ICommentRepository _commentRepo; 

public class Post 
{ 
    public Post(ICommentRepository commentRepo) 
    { 
    // Or you can remove this forced-injection and use a 
    // "Service Locator" to wire it up internall. 
    _commentRepo = commentRepo; 
    } 

    public int PostID { get; set; } 

    public void DeleteComments(IList<Comment> comments) 
    { 
    // put your logic here to "lookup what has been deleted" 
    // and then call ICommentRepository.Delete() in the loop. 

    _commentRepo.Remove(commentID); 
    } 
} 

请评论,如果其他人有意见或变更。

+0

是的,但我不喜欢的是,现在这个帖子有责任更新注释库,这超出了仅仅是一个帖子的范围。 – Sekhat 2009-02-21 19:03:15