我已经修改了一点,删除了一个方法,从我发现这个例子的原始帖子中删除了一个方法。这是通用存储库。
/// <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 get; set;这是为了测试。
对我而言,这似乎非常臭,因为你必须硬编码EF连接字符串名称的名称。
我想找到一种方法来强制一个工厂围绕这个Generic也是如此。所以我们可以有一个基于TEntity生成的存储库。
答案 0 :(得分:3)
这个怎么样:
public class ContextWrapper : DbContext
{
public ContextWrapper(string ConnectionStringName)
: base("name=" + ConnectionctionStringName)
{ }
}
只有当您有关于用于映射实体类型的EDMX的连接字符串的信息时,基于TEntity类型的存储库的工厂才有意义。但是这些信息必须硬编码到工厂或者我不明白你的问题。
顺便说一下。一旦尝试使用它,您的通用方法将完全失败。它适用于CRUD操作一个没有关系的实体,但是一旦你开始在真实实体或聚合根上使用它,你就会遇到很多问题。只需浏览使用entity-framework和repository-pattern标记的问题,您就会看到。