Session: How to perform operations lazily?

Operation execution for operations such as Load, Load with Includes, LoadStartingWith, MoreLikeThis 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.
  • MoreLikeThis - described here.

Querying

Dedicated article about lazy queries can be found here.

Example

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

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;