2009-02-17 48 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