2011-09-15 66 views
1

當使用多組實體時,使用存儲庫模式(使用實體框架)的正確方式是什麼?什麼是使用多個存儲庫的正確方法?

我應該爲每個實體創建一個存儲庫嗎?
例如:
擁有以下實體:文章,類別和評論。
我應該爲每一個存儲庫嗎?

我使用存儲庫這樣的:

public class BaseArticleRepository : BaseRepository 
{ 

    private ContentModel _contentctx; 
    public ContentModel Contentctx 
    { 
     get 
     { 
      if ((_contentctx == null)) 
      {     
       _contentctx = new ContentModel(); 
      } 

      return _contentctx; 
     } 
     set { _contentctx = value; } 
    } 
    // IDisposable Support code comes here....       
    } 

而且樣本庫的文章:

public class ArticlesRepository : BaseArticleRepository 
{ 
    public Article GetArticleById(int id) 
    { 

     var article = Contentctx.Articles.Where(o => o.ArticleID == id).FirstOrDefault();   
     return article; 
    } 

    public List<Article> GetArticles() 
    { 

     var articles = Contentctx.Articles.ToList();   
     return articles; 
    } 

    public List<ArticleHeader> GetArticlesHeaders() 
    { 
     var articles = (from article in Contentctx.Articles 
         select new ArticleHeader 
         { 
          ArticleID = article.ArticleID, 
          Title = article.Title, 
          CategoryTitle = article.Articles_Categories.Title,        
          AddedBy = article.AddedBy, 
          AddedDate = article.AddedDate, 
          ViewCount = article.ViewCount 

        }).ToList(); 


     return articles; 
    } 
    public List<ArticleHeader> GetArticlesHeaders(int PageIndex, int PageSize) 
    { 


     var articles = (from article in Contentctx.Articles 
         select new ArticleHeader 
         { 
           ArticleID = article.ArticleID, 
           Title = article.Title, 
           CategoryTitle = article.Articles_Categories.Title, 
           AddedBy = article.AddedBy, 
           AddedDate = article.AddedDate, 
           ViewCount = article.ViewCount 

         }).OrderBy(p => p.AddedDate).Skip(PageSize * PageIndex).Take(PageSize).ToList(); 

      return articles; 
} 



    public int GetArticleCount(string txtFilter) 
    { 

     int ret = Contentctx.Articles.Where(o => o.Title.Contains(txtFilter)).Count();  
     return ret; 

    } 



    public int AddArticle(Article article, int categoryId) 
    { 

      Contentctx.AddToArticles(article);    
    } 
} 

基本上每個庫實現了所有CRUD數據(包括獲取與過濾和排序數據),但我在某些博客中讀到這是錯誤的存儲庫模式實現,因爲存儲庫必須實現基本的通用功能來檢索和插入(刪除修改)數據。
所有排序,過濾必須在內存中完成。

但我preform什麼我可以在服務器端(sqlserver)。
如果我只需要標題和摘要,爲什麼我應該從數據庫加載所有文章(包含所有字段)?

+1

「所有的排序,過濾必須在內存本地完成。」 - 你是從哪裏讀到的?不知道你使用的後端是什麼,但是當然,sql查詢可以比內存中的數據更有效地對數據進行排序/過濾(因爲你花費了相同的工作量編程)。如果你有一百萬行的數據要過濾通過? –

回答

2

我會建議爲您正在處理的每個聚合根目錄創建一個存儲庫。一個aggregate root是您實際想要作爲您操作的對象(即Customer)擁有的數據結構,它可能具有Address,Orders,Invoices等子結構(您檢索的實際客戶與這些相關子結構合併爲各個表的集合)。

+0

是的聚合會適合我。但是儲存庫的實現呢,有沒有像我的文章那樣的具體查詢是可以的?或者實現必須像這樣是通用的:http://www.codeproject.com/KB/database/ImplRepositoryPatternEF.aspx – leaver

+0

嚴格地說,存儲庫模式應該是相當愚蠢的,邏輯坐落在上面的層中,但我總是會包含諸如存儲庫中的「GetByID」之類的方法。您只是試圖抽象存儲庫中的數據訪問層,僅此而已。想想你想使用這些信息的位置。存儲庫將數據呈現給業務邏輯層,BLL將數據呈現給表示層......這通常會回答關於應該在哪裏實現特定方法的問題。 – Lazarus

+0

這是一個很好的方法http://blog.lowendahl.net/?p=249,但它會要求我實施「ICriteria」爲我需要的每個查詢(和我很多) – leaver

0

要說「這是正確的方式」始終是一種冒險的肯定,但我認爲你應該創建聚合(由各種表格組成的實體),並且一旦完成,就嘗試瞭解「工作單元」 (UoW)模式。 UoW是我用來處理多個存儲庫的方式。

順便說一句,我同意zespri。在內存中排序和過濾不是一個好主意。

相關問題