IQueryable功能,如Include,但是对于单个实体? [英] IQueryable functionality, like Include, but for a single Entity?

查看:235
本文介绍了IQueryable功能,如Include,但是对于单个实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Entity Framework 6,并且已经实现了存储库模式的一些外观。最初,我的存储库具有像 GetAll 这样的功能,它返回一个 IEnumerable ,以避免数据层抽象太多泄漏。但是,我的服务层是包围我的仓库并包含业务逻辑的服务层,需要更多的对查询的控制。控制如加载相关实体,仅选择某些列等。因此,为了避免将DbContext暴露给我的服务层,我调整了存储库,以便现在返回 IQueryable ,以便服务层可以在查询等上执行链 Include 等事情。



问题是我的存储库有一个返回单个实体的方法,该函数只返回该实体的POCO。这些是最需要加载的功能,调用 Include 。对于一个实体来说,有一些像 IQueryable 这样的东西。链接包含逻辑等?还是一种调整这个逻辑的方法呢?我的一个存储库的一个例子是:

 命名空间Portal.Repositories 
{
public class UploadRepository :IUploadRepository
{
private readonly IPortalContext m_context;

public UploadRepository(IPortalContext context)
{
m_context = context;
}

#region方法

public int Count(Expression< Func< Upload,bool>> predicate)
{
return m_context.Uploads.Count(谓词);
}

public Upload Get(Expression< Func< Upload,bool>> predicate)
{
return m_context.Uploads.FirstOrDefault(predicate);
}

public Upload Insert(上传实体)
{
return m_context.Uploads.Add(entity);
}

public上传删除(上传实体)
{
return m_context.Uploads.Remove(entity);
}

public IQueryable< Upload> All()
{
return m_context.Uploads;
}

public IQueryable< Upload> Where(Expression< Func< Upload,bool>>谓词)
{
return m_context.Uploads.Where(predicate);
}

#endregion
}
}

您可以看到我的 Get 方法,以及我的服务无法选择并选择何时到 Include 或者不是,因为它只是返回一个POCO。

解决方案

你可以添加一些功能到你的仓库来传递一些包含表达式。

使用System.Data.Entity; 

命名空间Portal.Repositories
{
public class UploadRepository:IUploadRepository
{
public Upload Get(
Expression< Func< Upload,bool >> predicate,
params表达式< Func< Upload,object >> [] includeExpressions)
{
var uploads = m_context.Uploads;
foreach(var i in includeExpressions)
uploads = uploads.Include(i);
return uploads.FirstOrDefault(predicate);
}
}
}


I'm using Entity Framework 6, and have implemented some semblance of the repository pattern. Initially, my repositories had functions like GetAll that returned an IEnumerable to avoid leaking the data layer abstractions out too much. But, my service layer, which is a wrapper around my repositories and contains business logic, needed more control over the queries. Control such as eager loading of related entities, selecting only certain columns, etc. So, in trying to avoid just exposing the DbContext to my service layer, I adjusted the repositories so that they now return IQueryable so that the service layer can do things like chain Includes onto the query, etc.

Problem is that my repositories have a method for returning a single entity, and the function just returns the POCO for the entity. These are the most likely functions for needing eager loading, calling Include. Can't do that off the POCO, obviously.

Is there something like IQueryable for a single entity for additional chaining of Include logic etc? Or a way to adjust this logic to allow for that? An example of a repository of mine is this:

namespace Portal.Repositories
{
    public class UploadRepository : IUploadRepository
    {
        private readonly IPortalContext m_context;

        public UploadRepository(IPortalContext context)
        {
            m_context = context;
        }

        #region Methods

        public int Count(Expression<Func<Upload, bool>> predicate)
        {
            return m_context.Uploads.Count(predicate);
        }

        public Upload Get(Expression<Func<Upload, bool>> predicate)
        {
            return m_context.Uploads.FirstOrDefault(predicate);
        }

        public Upload Insert(Upload entity)
        {
            return m_context.Uploads.Add(entity);
        }

        public Upload Delete(Upload entity)
        {
            return m_context.Uploads.Remove(entity);
        }

        public IQueryable<Upload> All()
        {
            return m_context.Uploads;
        }

        public IQueryable<Upload> Where(Expression<Func<Upload, bool>> predicate)
        {
            return m_context.Uploads.Where(predicate);
        }

        #endregion
    }
}

You can see my Get method, and how it's not possible for my services to pick and choose when to Include or not to, because it just returns a POCO.

解决方案

You can add some functionality to your repositories to pass some include expressions.

using System.Data.Entity;

namespace Portal.Repositories
{
  public class UploadRepository : IUploadRepository
  {
    public Upload Get(
      Expression<Func<Upload, bool>> predicate, 
      params Expression<Func<Upload, object>>[] includeExpressions)
    {
      var uploads = m_context.Uploads;
      foreach (var i in includeExpressions)
        uploads = uploads.Include(i);
      return uploads.FirstOrDefault(predicate);
    }
  }
}

这篇关于IQueryable功能,如Include,但是对于单个实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆