实体框架和工作单元

时间:2012-08-28 20:20:47

标签: entity-framework unit-of-work

我正在使用EF / Repository / Unit of Work,但我很难理解一些细节。在UnitOfWork内部,我创建了一个新的EF DbContext(EmmaContext),但是在存储库中查看,我将其转换为我知道错误,如何正确获取repo中的上下文?也许我完全走错了路?

这是我的UnitOfWork:

//Interface
public interface IUnitOfWork : IDisposable
{
    void Commit();
}

//Implementation
public class UnitOfWork : IUnitOfWork
{
    #region Fields/Properties
    private bool isDisposed = false;
    public EmmaContext Context { get; set; }
    #endregion

    #region Constructor(s)
    public UnitOfWork()
    {
        this.Context = new EmmaContext();
    }
    #endregion

    #region Methods
    public void Commit()
    {
        this.Context.SaveChanges();
    }

    public void Dispose()
    {
        if (!isDisposed)
            Dispose(true);
        GC.SuppressFinalize(this);
    }

    private void Dispose(bool disposing)
    {
        isDisposed = true;
        if (disposing)
        {
            if (this.Context != null)
                this.Context.Dispose();
        }
    }
    #endregion
}

这是存储库:

//Interface
public interface IRepository<TEntity> where TEntity : class
{
    IQueryable<TEntity> Query();
    void Add(TEntity entity);
    void Attach(TEntity entity);
    void Delete(TEntity entity);
    void Save(TEntity entity);
}

//Implementation
public abstract class RepositoryBase<TEntity> : IRepository<TEntity> where TEntity : class
{
    #region Fields/Properties
    protected EmmaContext context;
    protected DbSet<TEntity> dbSet;
    #endregion

    #region Constructor(s)
    public RepositoryBase(IUnitOfWork unitOfWork)
    {
        this.context = ((UnitOfWork)unitOfWork).Context;
        this.dbSet = context.Set<TEntity>();
    }
    #endregion

    #region Methods
    public void Add(TEntity entity)
    {
        dbSet.Add(entity);
    }

    public void Attach(TEntity entity)
    {
        dbSet.Attach(entity);
    }

    public void Delete(TEntity entity)
    {
        dbSet.Remove(entity);
    }

    public IQueryable<TEntity> Query()
    {
        return dbSet.AsQueryable();
    }

    public void Save(TEntity entity)
    {
        Attach(entity);
        context.MarkModified(entity);
    }
    #endregion
}

4 个答案:

答案 0 :(得分:4)

Sam:我通常对使用具体的UnitOfWork的具体存储库感到满意:

   public RepositoryBase(UnitOfWork unitOfWork)
   {
        this.context = unitOfWork.Context;
        this.dbSet = context.Set<TEntity>();
   }

存储库和UoW通常协同工作,需要彼此了解一点。

当然,使用这些类的代码只知道接口定义而不是具体类型。

答案 1 :(得分:4)

这是best article I've read

在他们的示例中,他们像这样管理存储库:

    private SchoolContext context = new SchoolContext();
    private GenericRepository<Department> departmentRepository;
    private GenericRepository<Course> courseRepository;

    public GenericRepository<Department> DepartmentRepository
    {
        get
        {

            if (this.departmentRepository == null)
            {
                this.departmentRepository = new GenericRepository<Department>(context);
            }
            return departmentRepository;
        }
    }

您的工作单元包含上下文,如果需要引用存储库,则会在尚未创建存储库时创建它,并传入它所持有的上下文。

本文还介绍了他们如何将常规MVC控制器实现转换为使用工作单元模式。

答案 2 :(得分:1)

在此post中说您必须在存储库库中实现IUnitOfWork接口。

我希望这会有所帮助。 此致

答案 3 :(得分:0)

Unit of Work
Repository

  

UnitOfWork用于管理原子操作。

     

存储库封装了持久存储在数据存储中的对象集以及对它们执行的操作。

如果你传递了上下文或UnitOfWork,那么你没有实现UnitOfWork + Repository模式,导致你从UnitOfWork中撤出它的责任。阿卡,你不需要它。

如果您只传递DbSet,则为正确的实现。实际上你不需要更多。