RavenDB IEnumerable与列表 [英] RavenDB IEnumerable vs List

查看:81
本文介绍了RavenDB IEnumerable与列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些我不了解的行为.我正在使用RavenDB,并且每个工作单元都使用一个会话:当逻辑类调用RavenDB数据访问层(DAL)时,将创建一个新的会话.在DAL中,可以调用其他DAL类和方法,但只使用一个会话.

I have some behavior that I don't understand. I'm using RavenDB, and I'm using a session for each unit of work: When a logic class calls the RavenDB data access layer (DAL), a new session is created. Within the DAL, other DAL classes and methods can be invoked, but just one session will be used.

我不了解的部分是下面的GetMostRecentByStartTime()方法中使用IEnumerable和List之间的区别.在这种方法中,就像显示的那样使用List,这是我的输出:

The part I don't understand is the difference between using IEnumerable and List within the GetMostRecentByStartTime() method below. In that method, using List just like what's shown, this is my output:

使用列表:
即将关闭会话之前的请求数:2
即将关闭会话之前的请求数:4
即将关闭会话之前的请求数:6
即将关闭会话之前的请求数:7

Using List:
Number of requests just before closing session: 2
Number of requests just before closing session: 4
Number of requests just before closing session: 6
Number of requests just before closing session: 7

注意:这些时间实际上并没有关闭.只有在最后一次之后.仅在完成最初调用的DAL方法后,我们才会关闭会话.

Note: The session doesn't actually get closed each of these times; only after the last time. We only close the session when the originally-called DAL method is done.

现在,如果我用IEnumerable替换List的每个实例(这是我所做的 only 更改),我将得到以下输出:

Now, if I replace every instance of List with IEnumerable (and that's the only change I make), I get this output:

使用IEnumerable:
即将关闭会话之前的请求数:2
即将关闭会话之前的请求数:3
即将关闭会话之前的请求数:4
即将关闭会话之前的请求数:27

Using IEnumerable:
Number of requests just before closing session: 2
Number of requests just before closing session: 3
Number of requests just before closing session: 4
Number of requests just before closing session: 27

为什么有区别?

另一个问题是,当我在应用程序中添加新的InstallationSummary对象时,使用IEnumerable方法增加了请求数量.我不明白为什么.当我使用List方法时,即使添加了更多InstallationSummary对象,请求计数仍保持不变.任何人都可以解释吗?

Another issue is that the request count increases, using the IEnumerable approach, when I add new InstallationSummary objects within my app. I don't understand why. When I use the List approach, the request count stays the same, even after adding more InstallationSummary objects. Can anyone explain that, too?

public IEnumerable<InstallationSummary> GetMostRecentByStartTime(int numberToRetrieve)
{
    Stopwatch stopwatch = new Stopwatch();
    stopwatch.Start();

    try
    {
        return ExecuteQuery<IEnumerable<InstallationSummary>>(() =>
        {
            List<InstallationSummary> installationSummaries =
                QueryAndCacheEtags(session => session.Advanced.LuceneQuery<InstallationSummary>()
                .Include(x => x.ApplicationServerId)
                .Include(x => x.ApplicationWithOverrideVariableGroup.ApplicationId)
                .Include(x => x.ApplicationWithOverrideVariableGroup.CustomVariableGroupId)
                .OrderByDescending(summary => summary.InstallationStart)
                .Take(numberToRetrieve)).Cast<InstallationSummary>().ToList();

            List<string> appServerIds = (from item in installationSummaries select item.ApplicationServerId).ToList();
            List<string> appIds = (from item in installationSummaries select item.ApplicationWithOverrideVariableGroup.ApplicationId).ToList();
            List<string> groupIds = (from item in installationSummaries select item.ApplicationWithOverrideVariableGroup.CustomVariableGroupId).ToList();

            List<ApplicationServer> appServers = new ApplicationServerData().GetByIds(appServerIds).ToList();
            List<Application> apps = new ApplicationData().GetByIds(appIds).ToList();
            List<CustomVariableGroup> groups = new CustomVariableGroupData().GetByIds(groupIds).ToList();

            foreach (InstallationSummary summary in installationSummaries)
            {
                summary.ApplicationServer = appServers.Where(server => server.Id == summary.ApplicationServerId).FirstOrDefault();

                summary.ApplicationWithOverrideVariableGroup.Application =
                    apps.Where(app => app.Id == summary.ApplicationWithOverrideVariableGroup.ApplicationId).FirstOrDefault();

                if (summary.ApplicationWithOverrideVariableGroup.CustomVariableGroupId == null) { continue; }

                summary.ApplicationWithOverrideVariableGroup.CustomVariableGroup =
                    groups.Where(group => group.Id == summary.ApplicationWithOverrideVariableGroup.CustomVariableGroupId).FirstOrDefault();
            }

            return installationSummaries;
        });
    }
    finally
    {
        stopwatch.Stop();
        Debug.WriteLine("InstallationSummaryData.GetMostRecentByStartTime(): " + stopwatch.ElapsedMilliseconds);
    }
}

在这里调用上述方法:

protected T ExecuteQuery<T>(Func<T> func)
{
    if (func == null) { throw new ArgumentNullException("func"); }

    try
    {
        return func.Invoke();
    }
    finally
    {
        Debug.WriteLine("Number of requests just before closing session: " + _session.Advanced.NumberOfRequests);
        CloseSession();
    }
}

推荐答案

首先,如果您换掉所有ILists并替换为IEnumerables,这并不是一个小的更改.主要区别在于,您可以 使用IEnumerables推迟执行,而使用IList时总是渴望执行.如果您不了解这种差异,可能会导致很多问题.

First, it is not a minor change if you swap all your ILists out and replace with IEnumerables. The major difference is that you can get deferred execution with your IEnumerables, while you always have eager execution when using IList. This can cause a lot of issues, if you're not aware of this difference.

在您的情况下,差异的原因是延迟执行和RavenDB .Include<T>()功能使用错误的结合.后者旨在通过在客户端DocumentSession中缓存包含的文档来减少对数据库的远程调用次数.如果使用DocumentSession.Load<T>(),则效果很好,但如果使用DocumentSession.Query<T>().Where(x => x.Id == id),则获取文档没有任何区别.如果您熟悉NHibernate,则这是您的第一级缓存.

In your case, the reason for the difference is a combination of deferred execution and wrong usage of the .Include<T>() feature of RavenDB. The latter is intended to reduce the number of remote calls to the database by caching the included documents inside the clients DocumentSession. This works great if you use DocumentSession.Load<T>() but it doesn't make any difference if you get your documents using DocumentSession.Query<T>().Where(x => x.Id == id). If you are familiar with with NHibernate, this is your first level cache.

为了使其正常运行,请更改代码并改用它:

In order to get it working, change your code and use this instead:

List<InstallationSummary> installationSummaries =
    QueryAndCacheEtags(session => session.Advanced.LuceneQuery<InstallationSummary>()
    .Include(x => x.ApplicationServerId)
    .Include(x => x.ApplicationWithOverrideVariableGroup.ApplicationId)
    .Include(x => x.ApplicationWithOverrideVariableGroup.CustomVariableGroupId)
    .OrderByDescending(summary => summary.InstallationStart)
    .Take(numberToRetrieve)).Cast<InstallationSummary>().ToList();

foreach (InstallationSummary summary in installationSummaries)
{
    summary.ApplicationServer = session.Load<ApplicationServer>(summary.ApplicationServerId);

    summary.ApplicationWithOverrideVariableGroup.Application =
        session.Load<Application>(summary.ApplicationWithOverrideVariableGroup.ApplicationId);

    if (summary.ApplicationWithOverrideVariableGroup.CustomVariableGroupId != null)
        summary.ApplicationWithOverrideVariableGroup.CustomVariableGroup =
            session.Load<CustomVariableGroup>(summary.ApplicationWithOverrideVariableGroup.CustomVariableGroupId);
}

这篇关于RavenDB IEnumerable与列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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