【问题标题】:UnitOfWork and Repository pattern - find in-memory objectsUnitOfWork 和存储库模式 - 查找内存中的对象
【发布时间】:2013-06-20 08:00:15
【问题描述】:

我正在使用 UnitOfWork 和存储库模式。

// generic repository
public class Repository<T> : where T : class
{
    private readonly DbSet<T> _dbSet;

    public Repository(DbSet<T> dbSet)
    {
        this._dbSet = dbSet;
    }

    public IQueryable<T> Queryable()
    {
        return this._dbSet.AsQueryable();
    }

    public IEnumerable<T> All()
    {
        return this._dbSet.AsEnumerable();
    }

    public IEnumerable<T> Find(Expression<Func<T, bool>> where)
    {
        return this._dbSet.Where(where);
    }

    public T First(Expression<Func<T, bool>> where)
    {
        return this._dbSet.First(where);
    }

    public T FirstOrDefault(Expression<Func<T, bool>> where)
    {
        return this._dbSet.FirstOrDefault(where);
    }

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

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

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

// product repository
public class ProductRepository : Repository<Product>
{
    public ProductRepository(DbSet<Product> dbSet) : base(dbSet)
    {
    }
}

// unit of work
public interface IUnitOfWork
{
    ProductRepository ProductsRepository { get; }
    void Commit();
}

// DbContext as unit of work
public class ApplicationUnitOfWork : DbContext, IUnitOfWork
{
    private readonly ProductRepository _productsRepository;

    public DbSet<Product> Products { get; set; }

    public ApplicationUnitOfWork()
    {
        _productsRepository = new ProductRepository(Products);
    }

    #region IUnitOfWork Implementation

    public ProductRepository ProductsRepository
    {
        get { return _productsRepository; }
    }

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

    #endregion
}

当我想在数据库中插入产品时,我会这样做:

_unitOfWork.ProductsRepository.Add(new Product());
_unitOfWork.Commit();

这行得通。

我的问题是,如果我在存储库中插入一个产品,然后在调用.Commit() 之前尝试检索它,存储库会返回null

Product product = new Product { Id = 5 };
_unitOfWork.ProductsRepository.Add(product);

Product theProduct = _unitOfWork.ProductsRepository.FirstOrDefault(p => p.Id == 5);
// theProduct is null

如何更改存储库模式实现(或 UnitOfWork),使其也返回“内存中”对象?

【问题讨论】:

  • 您是否尝试过使用 Find 而不是 FirstOrDefault ?否则你可以查询 Set.Local.
  • DbSet&lt;T&gt;.Find(params Object[])+1
  • Ekkkk 代码到处都是气味。
  • @Phill:什么意思?
  • BaseRepository 是一种反模式,您对如何获取数据做出假设,并非所有数据都通过 id 获取,复合类型永远不会使用 id 键,有些东西永远不会有列表等. 但是你正在强制实施。

标签: asp.net entity-framework repository-pattern unit-of-work


【解决方案1】:

选项#1

使用Find 方法而不是FirstOrDefault。看看this article 看看它是如何工作的:

DbSet 上的 Find 方法使用主键值来尝试查找 由上下文跟踪的实体。如果实体不在 上下文然后查询将被发送到数据库以查找实体 那里。如果在上下文中找不到实体,则返回 Null 或 在数据库中。查找不同于使用查询在两个 重要方式:只有在以下情况下才会对数据库进行往返 在上下文中找不到具有给定键的实体。寻找遗嘱 返回处于已添加状态的实体。

选项#2

查询本地数据。看看this article

【讨论】:

    猜你喜欢
    • 2019-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    相关资源
    最近更新 更多