2011-04-02 69 views
0

我已經修改了這一點,刪除了一個方法,從原來的帖子中找到了這個例子。這是通用資源庫。實現這個通用知識庫,工作單元和知識庫工廠

/// <summary> 
/// Repository base class used with DbContext Originally From http://dotnetspeak.com/index.php/2011/03/repository-pattern-with-entity-framework/ 
/// </summary> 
/// <typeparam name="TContext">Type of DdContext that this repositiory operates on</typeparam> 
public class RepositoryBase<TContext> : IDisposable, IRepositoryBase where TContext : DbContext, IObjectContextAdapter, new() 
{ 
    private DbContext _dbContext; 
    public DbContext CurrentContext { get; set; } 

    /// <summary> 
    /// Create new instance of repository 
    /// </summary> 
    /// <param name="DbContext">For embeded edmx resource please define base("name=yourAppEntities") in a class derrived from DBContext</param> 
    public RepositoryBase(DbContext _context) 
    { 
     _dbContext = new TContext(); 
     CurrentContext = _dbContext; 
    } 
    /// <summary> 
    /// Select data from database 
    /// </summary> 
    /// <typeparam name="TItem">Type of data to select</typeparam> 
    /// <returns></returns> 
    public IQueryable<TItem> Select<TItem>() 
     where TItem : class, new() 
    { 
     DbSet<TItem> _set = _dbContext.Set<TItem>(); 
     return _set; 
    } 
    /// <summary> 
    /// Insert new item into database 
    /// </summary> 
    /// <typeparam name="TItem">Type of item to insert</typeparam> 
    /// <param name="item">Item to insert</param> 
    /// <returns>Inserted item</returns> 
    public TItem Insert<TItem>(TItem item) 
     where TItem : class, new() 
    { 
     DbSet<TItem> _set = _dbContext.Set<TItem>(); 
     _set.Add(item); 
     _dbContext.SaveChanges(); 

     return item; 
    } 
    /// <summary> 
    /// Update an item 
    /// </summary> 
    /// <typeparam name="TItem">Type of item to update</typeparam> 
    /// <param name="item">Item to update</param> 
    /// <returns>Updated item</returns> 
    public TItem Update<TItem>(TItem item) 
     where TItem : class, new() 
    { 
     DbSet<TItem> _set = _dbContext.Set<TItem>(); 
     _set.Attach(item); 
     _dbContext.Entry(item).State = System.Data.EntityState.Modified; 
     _dbContext.SaveChanges(); 
     return item; 
    } 
    /// <summary> 
    /// Delete an item 
    /// </summary> 
    /// <typeparam name="TItem">Type of item to delete</typeparam> 
    /// <param name="item">Item to delete</param> 
    public void Delete<TItem>(TItem item) 
     where TItem : class, new() 
    { 
     DbSet<TItem> _set = _dbContext.Set<TItem>(); 
     var entry = _dbContext.Entry(item); 
     if (entry != null) 
     { 
      entry.State = System.Data.EntityState.Deleted; 
     } 
     else 
     { 
      _set.Attach(item); 
     } 
     _dbContext.Entry(item).State = System.Data.EntityState.Deleted; 
     _dbContext.SaveChanges(); 
    } 


    /// <summary> 
    /// Dipose repository 
    /// </summary> 
    public void Dispose() 
    { 
     if (_dbContext != null) 
     { 
      _dbContext.Dispose(); 
      _dbContext = null; 
     } 
    } 
} 

我該如何爲此實現一個DbContext工廠?正如你所看到的構造函數使用的DbContext,以及使用EDMX在編譯的文件到你有從派生的DbContext像這樣一類傳遞一個組件時:

public class ContextWrapper: DbContext 
{ 
    public string _connectionString { get; set; } 
    public ContextWrapper() 
     : base("name=" + ConfigurationManager.ConnectionStrings["MyEFStringName"].Name) 
    { 
     _connectionString = this.Database.Connection.ConnectionString; 


    } 
} 

忽略_connectionstring獲得;設置;它用於測試。

對我來說這似乎很臭,因爲你必須硬編碼的EF連接字符串名稱的名稱。

我想找出一種辦法來阻止工廠周圍這是通用的。所以我們可以根據TEntity生成一個存儲庫。

回答

3

這個怎麼樣:

public class ContextWrapper : DbContext 
{ 
    public ContextWrapper(string ConnectionStringName) 
     : base("name=" + ConnectionctionStringName) 
    { } 
} 

廠基於TEntity類型庫纔有意義,如果您有關於連接字符串信息EDMX用於映射實體類型。但是這些信息必須硬編碼到工廠,或者我不明白你的問題。

Btw。一旦您嘗試使用它,您的通用方法將完全失敗。它對CRUD操作非常有用,一個實體沒有關係,但是一旦你開始在真實實體或聚合根上使用它,你將會遇到很多問題。只需瀏覽標有的問題,就會看到。

+0

它可以很好地處理與關係的實體,它可以從UnitOfWork中受益,並且我正在接近這一點。我不明白你怎麼能說它會完全失敗,你沒有這個聲明的基礎。 – CrazyCoderz 2011-04-02 17:10:19

+0

這個問題並不是真正的Wrapper類或連接字符串更多關於如何使一個通用工廠arounf DBContext的存儲庫。 – CrazyCoderz 2011-04-02 17:11:14