Session: How to Perform Operations Lazily
Operation execution for operations such as Load
, Load with Includes
, LoadStartingWith
and queries can be deferred till needed using lazy session operations. Those operations ca be accessed using Lazily
property found in Advanced
session operations.
Operations
Available operations are:
Load
- described here.Load with Includes
- described here.LoadStartingWith
- described here.GetCompareExchangeValue
- described here.
Querying
Dedicated article about lazy queries can be found here.
Example
Lazy<Employee> employeeLazy = session
.Advanced
.Lazily
.Load<Employee>("employees/1-A");
Employee employee = employeeLazy.Value; // load operation will be executed here
Executing All Pending Lazy Operations
To execute all pending lazy operations use ExecuteAllPendingLazyOperations
method from eager session operations found under Eagerly
property in Advanced
session operations.
Lazy<IEnumerable<Employee>> employeesLazy = session
.Query<Employee>()
.Lazily();
session.Advanced.Eagerly.ExecuteAllPendingLazyOperations(); // query will be executed here
IEnumerable<Employee> employees = employeesLazy.Value;