[英]Using multiple DbContexts with a generic repository and unit of work
我的应用程序越来越大,到目前为止,我只有一个MyDbContext
,其中包含我的应用程序中需要的所有表。 我希望(出于概述的目的)将它们分成多个DbContext
,例如MainDbContext
, EstateModuleDbContext
, AnotherModuleDbContext
和UserDbContext
。
我不确定如何完成此操作,因为我现在正在使用依赖注入(ninject)将DbContext放在UnitOfWork类上,例如:
kernel.Bind(typeof(IUnitOfWork)).To(typeof(UnitOfWork<MyDbContext>));
我应该放弃依赖注入这种方法,并显式设置要在服务中使用的DbContext
例如:
private readonly EstateService _estateService;
public HomeController()
{
IUnitOfWork uow = new UnitOfWork<MyDbContext>();
_estateService = new EstateService(uow);
}
代替:
private readonly EstateService _estateService;
public HomeController(IUnitOfWork uow)
{
_estateService = new EstateService(uow);
}
还是这还有另一种更好的方法? 还有一个附带的问题,我不喜欢将uow
通知我-服务是否有另一种(更好的)方法?
码
我有这个IDbContext和MyDbContext:
public interface IDbContext
{
DbSet<T> Set<T>() where T : class;
DbEntityEntry<T> Entry<T>(T entity) where T : class;
int SaveChanges();
void Dispose();
}
public class MyDbContext : DbContext, IDbContext
{
public DbSet<Table1> Table1 { get; set; }
public DbSet<Table2> Table1 { get; set; }
public DbSet<Table3> Table1 { get; set; }
public DbSet<Table4> Table1 { get; set; }
public DbSet<Table5> Table1 { get; set; }
/* and so on */
static MyDbContext()
{
Database.SetInitializer<MyDbContext>(new CreateDatabaseIfNotExists<MyDbContext>());
}
public MyDbContext()
: base("MyDbContext")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
}
}
然后我有这个IRepository和实现:
public interface IRepository<T> where T : class
{
IQueryable<T> GetAll();
void Add(T entity);
void Delete(T entity);
void DeleteAll(IEnumerable<T> entity);
void Update(T entity);
bool Any();
}
public class Repository<T> : IRepository<T> where T : class
{
private readonly IDbContext _context;
private readonly IDbSet<T> _dbset;
public Repository(IDbContext context)
{
_context = context;
_dbset = context.Set<T>();
}
public virtual IQueryable<T> GetAll()
{
return _dbset;
}
public virtual void Add(T entity)
{
_dbset.Add(entity);
}
public virtual void Delete(T entity)
{
var entry = _context.Entry(entity);
entry.State = EntityState.Deleted;
_dbset.Remove(entity);
}
public virtual void DeleteAll(IEnumerable<T> entity)
{
foreach (var ent in entity)
{
var entry = _context.Entry(ent);
entry.State = EntityState.Deleted;
_dbset.Remove(ent);
}
}
public virtual void Update(T entity)
{
var entry = _context.Entry(entity);
_dbset.Attach(entity);
entry.State = EntityState.Modified;
}
public virtual bool Any()
{
return _dbset.Any();
}
}
还有IUnitOfWork和实现,它处理DbContext完成的工作
public interface IUnitOfWork : IDisposable
{
IRepository<TEntity> GetRepository<TEntity>() where TEntity : class;
void Save();
}
public class UnitOfWork<TContext> : IUnitOfWork where TContext : IDbContext, new()
{
private readonly IDbContext _ctx;
private readonly Dictionary<Type, object> _repositories;
private bool _disposed;
public UnitOfWork()
{
_ctx = new TContext();
_repositories = new Dictionary<Type, object>();
_disposed = false;
}
public IRepository<TEntity> GetRepository<TEntity>() where TEntity : class
{
// Checks if the Dictionary Key contains the Model class
if (_repositories.Keys.Contains(typeof(TEntity)))
{
// Return the repository for that Model class
return _repositories[typeof(TEntity)] as IRepository<TEntity>;
}
// If the repository for that Model class doesn't exist, create it
var repository = new Repository<TEntity>(_ctx);
// Add it to the dictionary
_repositories.Add(typeof(TEntity), repository);
return repository;
}
public void Save()
{
_ctx.SaveChanges();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (this._disposed) return;
if (disposing)
{
_ctx.Dispose();
}
this._disposed = true;
}
}
除非有逻辑上的接缝,否则请勿将模块化数据片段拆分为多个DbContext
。 从实体DbContextA
不能与实体自动导航或集合属性DbContextB
。 如果拆分上下文,则您的代码必须负责手动执行约束并在上下文之间加载相关数据。
对于“概述”(也就是保持理智),您仍然可以按模块组织CLR代码和数据库表。 对于POCO,请将其保存在不同名称空间下的不同文件夹中。 对于表,可以按架构分组。 (但是,在通过SQL模式进行组织时,您可能还应该考虑安全性。例如,如果有任何数据库用户应限制对某些表的访问,请根据这些规则设计模式。)然后,您可以执行此操作建立模型时:
ToTable("TableName", "SchemaName"); // put table under SchemaName, not dbo
只有一个单独去DbContext
当它的实体有在你第一次的任何实体没有关系DbContext
。
我也同意Wiktor,因为我不喜欢您的界面和实现设计。 我特别不喜欢public interface IRepository<T>
。 另外,为什么还要声明多个public DbSet<TableN> TableN { get; set; }
public DbSet<TableN> TableN { get; set; }
public DbSet<TableN> TableN { get; set; }
在您的MyDbContext
? 请帮我一个忙,阅读这篇文章 ,然后阅读这篇 文章 。
您可以使用如下所示的EF接口设计极大地简化代码:
interface IUnitOfWork
{
int SaveChanges();
}
interface IQueryEntities
{
IQueryable<T> Query<T>(); // implementation returns Set<T>().AsNoTracking()
IQueryable<T> EagerLoad<T>(IQueryable<T> queryable, Expression<Func<T, object>> expression); // implementation returns queryable.Include(expression)
}
interface ICommandEntities : IQueryEntities, IUnitOfWork
{
T Find<T>(params object[] keyValues);
IQueryable<T> FindMany<T>(); // implementation returns Set<T>() without .AsNoTracking()
void Create<T>(T entity); // implementation changes Entry(entity).State
void Update<T>(T entity); // implementation changes Entry(entity).State
void Delete<T>(T entity); // implementation changes Entry(entity).State
void Reload<T>(T entity); // implementation invokes Entry(entity).Reload
}
如果声明MyDbContext : ICommandEntities
,则只需设置一些方法来实现该接口(通常是单行)。 然后,您可以IQueryEntities
3个接口中的任何一个注入到服务实现中:通常, ICommandEntities
用于具有副作用的操作,而IQueryEntities
用于没有副作用的操作。 任何仅负责保存状态的服务(或服务装饰器)都可以依赖IUnitOfWork
。 我不同意Controller
尽管应该依赖IUnitOfWork
。 使用以上设计,您的服务应在返回Controller
之前保存更改。
如果您的应用中有多个单独的DbContext
类有意义,则可以按照Wiktor的建议进行操作 ,并使上述接口通用。 然后,您可以像这样将依赖项注入服务中:
public SomeServiceClass(IQueryEntities<UserEntities> users,
ICommandEntities<EstateModuleEntities> estateModule) { ... }
public SomeControllerClass(SomeServiceClass service) { ... }
// Ninject will automatically constructor inject service instance into controller
// you don't need to pass arguments to the service constructor from controller
创建宽的每个聚合(甚至每个实体更糟)的存储库接口可以与EF对抗,增加无聊的管道代码,并过度注入构造函数。 相反,给您的服务更大的灵活性。 诸如.Any()
类的方法不属于该接口,您可以在服务方法中调用Query<T>
或FindMany<T>
返回的IQueryable<T>
上的扩展。
您的工作单元界面不是通用的,但是实现是通用的。 解决此问题的最简单方法是决定并遵循相同的约定。
例如,也使您的接口通用。 这样,您可以将三个不同的接口(具有三个不同的通用参数的同一接口)注册到三个不同的实现:
container.Bind( typeof<IUnitOfWork<ContextOne>> ).To( typeof<UnitOfWork<ContextOne>> );
...
是的,将您的工作单元注入控制器/服务中是一个好主意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.