使用IncludeMultiple的方法中的方法、IncludeMultiple

由网友(自娱自乐自己过〃)分享简介:我使用的拉吉斯拉夫·Mrnka的扩展方法:I am using Ladislav Mrnka's extension method:public static IQueryable IncludeMultiple(this IQueryable query,params Expression我使用的拉吉斯拉夫·Mrnka的扩展方法:

I am using Ladislav Mrnka's extension method:

    public static IQueryable<T> IncludeMultiple<T>(this IQueryable<T> query,
        params Expression<Func<T, object>>[] includes)
        where T : class
    {
        if (includes != null)
        {
            query = includes.Aggregate(query,
                      (current, include) => current.Include(include));
        }

        return query;
    }

我从这里(变化不大后):

public virtual IEnumerable<T> Get(
            Expression<Func<T, bool>>[] filters = null,
            Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
            string includeProperties = "")
        {
            IQueryable<T> query = GetQuery();

            if (filters != null)
            {
                foreach (var filter in filters)
                {
                    query = query.Where(filter);
                }
            }

            foreach (var includeProperty in includeProperties.Split
                (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
            {
                query = query.Include(includeProperty);
            }

            if (orderBy != null)
            {
                query = orderBy(query);
            }

            return query;
        }

我想用的,而不是在includeProperties变量字符串的IncludeMultiple。 所以,我改变了功能:

I want to use the IncludeMultiple instead of strings in includeProperties variable. So, I changed the function:

public virtual IEnumerable<T> Get(
            Expression<Func<T, bool>>[] filters = null,
            Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
            params Expression<Func<T, object>>[] includes)
        {
            IQueryable<T> query = GetQuery();

            if (filters != null)
            {
                foreach (var filter in filters)
                {
                    query = query.Where(filter);
                }
            }

            if (includes.Length > 0)
            {
                query = query.IncludeMultiple(includes);
            }

            if (orderBy != null)
            {
                query = orderBy(query);
            }

            return query;
        }

现在,我有点迷惑。在一个类中定义此方法,其中GetQuery()的定义(仓库实现)。但是,如果我想执行这个方法,我会在开始时使用GetQuery().. 我说得对不对? 是更好地使用它作为一个扩展的IQueryable?

Now, I am a little bit confuse. This method defined in a class where GetQuery() is defined (repository implementation). But in case I want to execute this method, I would have initially use GetQuery().. Am I right? Is it better to use this as an extension to IQueryable?

推荐答案

没有你不需要调用后,使用此方法 GetQuery ,因为调用 GetQuery 是在该方法的第一个命令。您可以使用 GetQuery 获取根据您的需要。该获取方法其实非常好,因为它隐藏其中。使用它作为一个存储库的方法要好得多然后将其用作扩展方法的情况下,单元测试和嘲弄。

No you don't need to use this method after you call GetQuery because calling GetQuery is the first command in this method. You can use either GetQuery or Get depending on your needs. The Get method is actually very good because it hides including. Using it as a repository method is much better then using it as extension method in case of unit testing and mocking.

阅读全文

相关推荐

最新文章