model-view-controller - 存储库模式 + 工作单元

标签 model-view-controller design-patterns repository unit-of-work

将近一年后,我开始了一个新的 mvc 项目,这次是版本 4。我想知道以下存储库模式的实现是否弊大于利。

public interface IRepository<T> where T : class
{
    IEnumerable<T> GetAll();

    IQueryable<T> Query(Expression<Func<T, bool>> filter);

    void Add(T entity);

    void Remove(T entity);
}

public interface IUnitOfWork
{
    void Commit();
}

public interface IDbContext : IDisposable
{
    IDbSet<T> Set<T>() where T : class;

    int SaveChanges();
}

public class DbContextAdapter : IDbContext
{
    private readonly DbContext _myRealContext;

    public DbContextAdapter()
    {
        this._myRealContext = new EstafaContext();
    }

    public void Dispose()
    {
        _myRealContext.Dispose();
    }

    public IDbSet<T> Set<T>() where T : class
    {
        return _myRealContext.Set<T>();
    }

    public int SaveChanges()
    {
        return _myRealContext.SaveChanges();
    }
}

public class SqlRepository<T> : IRepository<T> where T : class
{
    private IDbSet<T> _dbSet;

    public SqlRepository(IDbContext dbContext)
    {
        this._dbSet = dbContext.Set<T>();
    }

    public IEnumerable<T> GetAll()
    {
        return this._dbSet.ToList();
    }

    public IQueryable<T> Query(Expression<Func<T, bool>> filter)
    {
        return this._dbSet.Where(filter);
    }

    public void Add(T entity)
    {
        this._dbSet.Add(entity);
    }

    public void Remove(T entity)
    {
        this._dbSet.Remove(entity);
    }
}

public class SqlUnitOfWork : IDisposable, IUnitOfWork
{
    private IDbContext _dbContext;

    private SqlRepository<Cliente> _clientes ;

    public SqlUnitOfWork()
    {
        this._dbContext = new DbContextAdapter();
    }

    public void Dispose()
    {
        if (this._dbContext != null)
        {
            this._dbContext.Dispose();
        }
        GC.SuppressFinalize(this);
    }

    public IRepository<Cliente> Clientes
    {
        get { return _clientes ?? (_clientes = new SqlRepository<Cliente>(_dbContext)); }
    }       

    public void Commit()
    {
        this._dbContext.SaveChanges();
    }
}

这样,我可以通过 SqlUnitOfWork 从一个点管理所有存储库。
我在之前的项目中使用了这种设计,效果很好,但我觉得它效率不高,可能是多余的。
添加这样一个抽象层值得吗?

提前致谢!

最佳答案

尽管我的实现并不完全像您所拥有的那样,但对我来说似乎很可靠。

我通常做的唯一其他更改是为每个实体类型存储库定义特定的接口(interface),如下所示:

public interface IClienteRepository : IRepository<Cliente>
{
  IEnumerable<Cliente> GetByName(string firstName);
  // etc
}

这样我仍然可以将所有存储库通用地用于我的 CRUD 操作,但我也可以使用相同的存储库来抽象出一些查询逻辑。您的存储库的用户应该只需要知道他们想要什么,而不是如何获得它。

当然,这有点乏味,需要您对存储库进行具体实现以添加额外功能,但它只是封装了查询逻辑,无论如何您都可以通过应用程序在其他地方获得。

关于model-view-controller - 存储库模式 + 工作单元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13695407/

相关文章:

c# - 构造异常消息的最佳做法是什么?

asp.net-mvc - 未找到或错误构建的路线

java - 将表示 XML 的 Java 对象序列化为其 XML 形式是否合适?

c# - 我可以显示标签并使用户无法与 DropDownList 交互吗?

java - Java中访客模式的通用实现

repository - 带有 StructureMap (IoC) 的 IQueryable 存储库 - 我如何实现 IDisposable?

git - 有没有办法在远程存储库树上执行 git-grep 命令?

android - 为 Android 创建开源项目

iOS Swift - 呈现 View Controller 并在它们之间传递信息

java - 如何在 Spring 中嵌套 View