1
votes

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

1 Answers

5
votes

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