RavenDB IEnumerable 与 List

发布于 2024-12-25 18:17:17 字数 3740 浏览 3 评论 0原文

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

我不明白的部分是在下面的 GetMostRecentByStartTime() 方法中使用 IEnumerable 和 List 之间的区别。在该方法中,就像所示的那样使用 List,这是我的输出:

Using List:
会话结束前的请求数:2
会话结束前的请求数:4
会话结束前的请求数:6
关闭会话之前的请求数:7

注意:会话实际上并不是每次都关闭;而是每次都会关闭。仅在上次之后。我们仅在最初调用的 DAL 方法完成后才关闭会话。

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

Using IEnumerable:
会话结束前的请求数:2
会话结束前的请求数:3
会话结束前的请求数:4
关闭会话前的请求数:27

为什么有差异?

另一个问题是,当我在应用程序中添加新的 InstallationSummary 对象时,使用 IEnumerable 方法,请求计数会增加。我不明白为什么。当我使用 List 方法时,即使添加更多 InstallationSummary 对象,请求计数也保持不变。谁也能解释一下吗?

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

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.

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:

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

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.

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

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

Why the difference?

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

Here is where the above method gets invoked:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩物 2025-01-01 18:17:17

首先,如果您将所有 IList 换出并替换为 IEnumerable,这并不是一个小变化。主要区别在于,您可以使用 IEnumerables 获得延迟执行,而使用 IList 时始终可以立即执行。如果您没有意识到这种差异,这可能会导致很多问题。

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

为了使其正常工作,请更改您的代码并使用以下代码:

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

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.

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文