Session: How to Perform Operations Lazily


Lazy Operations

Operations that can be executed lazily include:


Load

You can load entities lazily using the Load method.

Example

Lazy<Employee> employeeLazy = session
    .Advanced
    .Lazily
    .Load<Employee>("employees/1-A");

Employee employee = employeeLazy.Value; // load operation will be executed here

Load with Include

You can load entities witn includes lazily.

Example

internal class User
{
    public string Id { get; set; }
    public string associateId { get; set; }
    public string Name { get; internal set; }
    public string ChangeVector { get; set; }
}
Lazy<User> userLazy = session
    .Advanced
    .Lazily
    .Include<User>(x => x.associateId)
    .Load<User>("users/1-A");

User user = userLazy.Value; // load operation will be executed here

Query

Learn more here about running queries lazily.

Example

Lazy<IEnumerable<Employee>> employeesLazy = session
    .Query<Employee>()
    .Where( x => x.FirstName == "John")
    .Lazily();

IEnumerable<Employee> employees = employeesLazy.Value; // The query will be executed here

LoadStartingWith

LoadStartingWith loads entities whose ID starts with a specified prefix. Learn more about it here.
Use LoadStartingWith lazily as shown in the example below.

Example

// return entities whose Id starts with 'employees/'
Lazy<Dictionary<string, Employee>> employees = session
    .Advanced
    .Lazily
    .LoadStartingWith<Employee>("employees/");

ConditionalLoad

ConditionalLoad loads only documents whose change vector has changed since the last load.
Learn more about it here.
Use ConditionalLoad lazily as shown in the example below.

Example

var lazy = session
    .Advanced
    .Lazily
    // Load only if the change vector has changed
    .ConditionalLoad<User>("users/1-A", changeVector);

var load = lazy.Value; // The operation will be executed here

Revisions

Document Revisions and data related to them can be loaded using several methods. Each of these methods can also be used lazily.

Revision_Method Lazy_Version
Get session.Advanced.Revisions.Lazily.Get
GetFor session.Advanced.Revisions.Lazily.GetFor
GetMetadataFor session.Advanced.Revisions.Lazily.GetMetadataFor

Example

var revisionsLazy = session
.Advanced
.Revisions
.Lazily
.GetFor<User>("users/1-A");

var revisionsLazyResult = revisionsLazy.Value; // The operation will be executed here

GetCompareExchangeValue

Use GetCompareExchangeValue to retrieve Compare Exchange values.
Learn here how to use this method lazily.

Executing All Pending Lazy Operations

To execute all pending lazy operations at once, use the session.Advanced.Eagerly.ExecuteAllPendingLazyOperations method.

Example

Lazy<User> LazyLoad1 = 
    session.Advanced.Lazily.Load<User>("users/1-A");
Lazy<User> LazyLoad2 =
    session.Advanced.Lazily.Load<User>("users/2-A");
Lazy<IEnumerable<Employee>> LazyQuery = 
    session.Query<Employee>().Lazily();

// Execute all pending lazy operations
session.Advanced.Eagerly.ExecuteAllPendingLazyOperations();