c# - 将多个 DbContext 与通用存储库和工作单元一起使用

标签 c# entity-framework repository-pattern dbcontext unit-of-work

我的应用程序越来越大,到目前为止,我只有一个 MyDbContext,它包含我的应用程序中需要的所有表。我希望(为了概述)将它们分成多个 DbContext,例如 MainDbContextEstateModuleDbContextAnotherModuleDbContextUserDbContext

我不确定这是如何完成的,因为我现在正在使用依赖注入(inject) (ninject) 将我的 DbContext 放在我的 UnitOfWork 类上,例如:

kernel.Bind(typeof(IUnitOfWork)).To(typeof(UnitOfWork<MyDbContext>));

我是否应该通过依赖注入(inject)放弃这种方法并显式设置 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; }在你的MyDbContext ?帮我一个忙,阅读this article ,然后阅读 this one .

您可以使用像这样的 EF 接口(interface)设计大大简化您的代码:

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 ,你只需要设置一些方法来实现接口(interface)(通常是一行)。然后,您可以将 3 个接口(interface)中的任何一个注入(inject)到您的服务实现中:通常是 ICommandEntities对于有副作用的操作,以及 IQueryEntities对于没有的操作。任何只负责保存状态的服务(或服务装饰器)都可以依赖于 IUnitOfWork .我不同意 Controller s 应该依赖于 IUnitOfWork尽管。使用上述设计,您的服务应在返回到 Controller 之前保存更改。 .

如果有多个单独的 DbContext您的应用程序中的类是有意义的,您可以 do as Wiktor suggests并使上述接口(interface)通用。然后,您可以像这样将依赖注入(inject)到服务中:

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

为每个聚合(甚至更糟糕的每个实体)创建存储库接口(interface)可能会与 EF 冲突,增加无聊的管道代码,并过度注入(inject)您的构造函数。相反,为您的服务提供更大的灵 active 。类似 .Any() 的方法不属于界面,您可以在 IQueryable<T> 上调用分机由 Query<T> 返回或 FindMany<T>从您的服务方法中。

关于c# - 将多个 DbContext 与通用存储库和工作单元一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20551236/

相关文章:

entity-framework - 使用多个 Db。如何注入(inject)连接到不同数据库的DbContext?

c# - 我们应该扩展 Comparer<T> 还是实现 IComparer<T>

c# - 这个类最好的设计是什么?

mysql - asp.net Identity 2.0的Extent用户表

entity-framework - EF Code First - 如果模型更改,则重新创建数据库

linq-to-sql - 我怎样才能跟上新技术?

c# - 初始化类中的子类对象 - StackOverflowException

c# - 使用正则表达式替换而不是字符串替换

c# - 使用 EF 从 Azure 移动服务检索父/子记录(包含列表的对象)

php - 方法重构?