开发者

EF 4.1 + Repository + UnitOfWork + Remove Dependency with EF

开发者 https://www.devze.com 2023-03-25 01:18 出处:网络
I have implemented Generic repository which only depends on IU开发者_如何学PythonnitOfWork (in Infrastructure.Repositories Library) and also haven\'t used any references to Entity Framework 4.1 dll. E

I have implemented Generic repository which only depends on IU开发者_如何学PythonnitOfWork (in Infrastructure.Repositories Library) and also haven't used any references to Entity Framework 4.1 dll. Entity Framework's DbContext wrapped with EFUnitOfWork which is in different class library called Infrastructure.EntityFramework. However, I came across some difficulty with Linq to Entity query that may force me to include direct dependancy with repository and EF 4.1 library.

In one of my class repository I need to use following query with a join. How can I overcome DBContext usage in my repository ?

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;


I made my repositories to expose IQuerables over DbContext. DbContext wrap with UnitOfWork as follows

My Repository base goes like this

    /// <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>();
    }

my unit of work implementation goes here

    /// <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>();
    }

Changes to my query as follows

        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;
0

精彩评论

暂无评论...
验证码 换一张
取 消