Including documents

You may have read about includes earlier, if not, note that 'includes' option gives you the ability to side-load additional documents in a single server call. Transformers also contain this powerful feature and you can take advantage of it by using Include method.

public class Products_NameCategoryAndSupplier : AbstractTransformerCreationTask<Product>
{
	public class Result
	{
		public string Name { get; set; }

		public string Category { get; set; }

		public string Supplier { get; set; }
	}

	public Products_NameCategoryAndSupplier()
	{
		TransformResults =
			products =>
			from product in products
			let supplier = Include<Supplier>(product.Supplier)
			let category = Include<Category>(product.Category)
			select new
			{
				Name = product.Name,
				Category = product.Category,
				Supplier = product.Supplier
			};
	}
}

Products_NameCategoryAndSupplier.Result result = session
	.Query<Product>()
	.Where(x => x.Name == "Chocolade")
	.TransformWith<Products_NameCategoryAndSupplier, Products_NameCategoryAndSupplier.Result>()
	.First();

int numberOfRequests = session.Advanced.NumberOfRequests;

Category category = session.Load<Category>(result.Category); // no server call
Supplier supplier = session.Load<Supplier>(result.Supplier); // no server call

Assert.Equal(numberOfRequests, session.Advanced.NumberOfRequests);