EF 4.1 + Repository + UnitOfWork + Remove Dependency with EF



我已经实现了只依赖于IUnitOfWork的通用存储库(在基础设施中)。仓库库),也没有使用任何引用实体框架4.1 dll。实体框架的DbContext用EFUnitOfWork包装,EFUnitOfWork在不同的类库中称为Infrastructure.EntityFramework。然而,我在使用Linq to Entity查询时遇到了一些困难,这可能迫使我包括与存储库和EF 4.1库的直接依赖关系。

在我的一个类存储库中,我需要使用下列查询连接。如何克服存储库中DBContext的使用?

var result = from cc in ProjectXEFDbContext.CurrentContext().PurchaseOrderLineItemCollection
                     join bb in GetQuery() on cc.PurchaseOrderId equals bb.Id
                     where bb.Id == purchaseOrder.Id && cc.Total > 50
                     select cc;

我让我的存储库通过DbContext公开IQuerables。DbContext用UnitOfWork包装如下

My Repository base是这样的

    /// <summary>
    /// Gets the query.
    /// </summary>
    /// <returns></returns>
    public IQueryable<TEntity> GetQuery()
    {
        return this.UnitOfWork.GetQuery<TEntity>();
    }
    /// <summary>
    /// Loads the type.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public IQueryable<T> LoadType<T>() where T : class
    {
        return this.UnitOfWork.GetQuery<T>();
    }

我的工作单元实现在这里

    /// <summary>
    /// Gets the query.
    /// </summary>
    /// <typeparam name="TEntity">The type of the entity.</typeparam>
    /// <returns></returns>
    public IQueryable<TEntity> GetQuery<TEntity>() where TEntity:class 
    {
        return this.DbContext.Set<TEntity>();
    }

我的查询修改如下

        var result = from cc in GetQuery()
                     join bb in LoadType<PurchaseOrderLineItem>() on cc.Id equals                      bb.PurchaseOrderId
                     where cc.Id == purchaseOrder.Id && bb.Total > 50
                     select bb;

最新更新