2013-03-15 49 views
2

我的倉庫看起來是這樣的:實體框架 - 知識庫和工作單元 - 我正在做這個工作嗎?

public class SqlRepository<T> : IRepository<T> where T : class 
{ 
    private ExchangeSiteContext _dbContext; 
    private DbSet<T> _dbSet; 

    #region [Constructor] 
    public SqlRepository(ExchangeSiteContext context) 
    { 
     _dbContext = context; 
     _dbSet = context.Set<T>(); 
    } 
    #endregion 

    /// <summary> 
    /// Gets the DbContext of the repository 
    /// </summary> 
    public ExchangeSiteContext DbContext 
    { 
     get 
     { 
      return this._dbContext; 
     } 
    } 

    /// <summary> 
    /// Get a list of entities 
    /// </summary> 
    /// <returns>List of type T entities</returns> 
    public IQueryable<T> GetList() 
    { 
     return this._dbSet.AsQueryable(); 
    } 

    /// <summary> 
    /// Get a list of entities by a predicate 
    /// </summary> 
    /// <param name="predicate">The predicate</param> 
    /// <returns>IQueryable of T</returns> 
    public IQueryable<T> GetList(Expression<Func<T, bool>> predicate) 
    { 
     return this._dbSet.Where(predicate).AsQueryable(); 
    } 

    ... 
    ... 
} 

我的工作單位是這樣的:

public class SqlUnitOfWork : IUnitOfWork, IDisposable 
{ 
    #region [Private Variables] 
    private ExchangeSiteContext _dbContext; 
    private BicycleSellerListingRepository _bicycleSellerListingRepository; 
    private UserProfileRepository _userProfileRepository; 
    #endregion 

    #region [Constructor] 
    public SqlUnitOfWork() 
    { 
     this._dbContext = new ExchangeSiteContext(); 
    } 
    #endregion 

    #region [Custom Repositories] 
    public BicycleSellerListingRepository BicycleSellerListingRepository 
    { 
     get 
     { 
      if (this._bicycleSellerListingRepository == null) 
       this._bicycleSellerListingRepository = new BicycleSellerListingRepository(this._dbContext); 

      return this._bicycleSellerListingRepository; 
     } 
    } 

    public UserProfileRepository UserProfileRepository 
    { 
     get 
     { 
      if (this._userProfileRepository == null) 
       this._userProfileRepository = new UserProfileRepository(this._dbContext); 

      return this._userProfileRepository; 
     } 
    } 
    #endregion 

    /// 
    /// Generic repository 
    /// 
    public SqlRepository<T> GenericRepository<T>() where T : class 
    { 
     return new SqlRepository<T>(this._dbContext); 
    } 

    public void Commit() 
    { 
     this._dbContext.SaveChanges(); 
    } 

    public void Dispose() 
    { 
     this._dbContext.Dispose(); 
     this._dbContext = null; 
    } 
} 

我的倉庫都是通用的。我的工作單元有一些自定義存儲庫,主要用於無法執行通用操作的情況。

我的問題是,這看起來不對?我以前從未創建過倉庫或工作單元。這似乎工作得很好,但我不確定我是否忽略了某些事情。

+0

看起來不錯,但爲什麼你的SqlRepository公開暴露了DbContext?通常,存儲庫只包含CRUD方法,而公共的dbcontext屬性允許太多。 – 2013-03-15 21:46:06

+0

好的。沒有理由,也不應該。 – Hosea146 2013-03-15 21:53:45

回答

3

存儲庫和UoW沒有一個單一的正確實現(就我而言,我更喜歡其中UoW是通過DbContext傳遞給存儲庫的簡單包裝)。但這裏有一些問題我在你的實現中看到:

  • GetList方法很混亂。他們正在返回IQueryable而不是列表。我認爲GetAll是比較合適的名稱。
  • 您無需致電_dbSet.AsQueryable(),因爲DbSet<T>實施了IQueryable<T>。只需返回_dbSet
  • 通常,在通用存儲庫中創建一些用於包含相關實體以進行急切加載的方法。
  • 如果您的存儲庫是通用的,那麼您爲什麼使用特定的上下文?改爲使用DbContext
  • 你爲什麼要從存儲庫中公開DbContext
  • 您正在UoW中創建上下文。這使得依賴注入成爲不可能。
  • _dbContext設置爲null在您處置後不需要。
+1

很棒的評論 - 非常感謝。 – Hosea146 2013-03-16 11:21:05

1

工作和庫模式的實施單位仍然在開發世界一個巨大的辯論的主題,看了那麼多關於這後不過我可以給你一些指引我已經收集:

  • 不要過度使用泛型,如果你真的需要一個通用的倉庫,那麼只能在那裏真正地共享所有的方法,如果你可以避免這一切,我會更好。
  • 永遠不要放一種基於表達式樹的過濾方法。
  • 我可以給你其他的想法,但我想指導你寫一篇關於這篇文章的文章[不是因爲它是我的,我真的很努力],如果你想要的話你可以查看here
相關問題