[英]Implementing this generic repository, unit of work and repository factory
我从发现该示例的原始帖子中对此进行了一些修改,删除了一种方法。 这是通用存储库。
/// <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连接字符串名称的名称。
我想找出一种方法来对这也是通用的工厂施加影响。 因此,我们可以有一个基于TEntity生成的存储库。
那这个呢:
public class ContextWrapper : DbContext
{
public ContextWrapper(string ConnectionStringName)
: base("name=" + ConnectionctionStringName)
{ }
}
仅当您具有有关用于映射实体类型的到EDMX的连接字符串的信息时,基于TEntity类型的存储库的工厂才有意义。 但是这些信息必须硬编码到工厂,否则我可能不理解您的问题。
顺便说一句。 一旦尝试使用通用方法,该方法将完全失败。 它非常适合CRUD操作(一个没有关系的实体),但是一旦您开始在实际实体或聚合根上使用它,就会遇到很多问题。 只需浏览标记有实体框架和存储库模式的问题 ,您就会看到。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.