Results Transformers

Results Transformers have been introduced in RavenDB 2.5 to give the user the ability to do a server side projections (with possibility to load data from other documents).

Note

Result Transformers are substituting the index TransformResults feature, marking it as obsolete.

Main features of the Result Transformers are:

I. Stand-alone, separated from Index.

public class Order
{
	public DateTime OrderedAt { get; set; }

	public Status Status { get; set; }

	public string CustomerId { get; set; }

	public IList<OrderLine> Lines { get; set; }
}

public class OrderStatisticsTransformer : AbstractTransformerCreationTask<Order>
{
	public OrderStatisticsTransformer()
	{
		TransformResults = orders => from order in orders
									 select new
											{
												order.OrderedAt,
												order.Status,
												order.CustomerId,
												CustomerName = LoadDocument<Customer>(order.CustomerId).Name,
												LinesCount = order.Lines.Count
											};
	}
}

II. User can use them on index results on demand.

What does it mean? It means if you want load whole Order you can do so by not using a transformer in the query:

IList<Order> orders = session.Query<Order>()
	.Where(x => x.CustomerId == "customers/1")
	.ToList();

or if you want to get your transformed results then you can execute query as follows:

public class OrderStatistics
{
	public DateTime OrderedAt { get; set; }

	public Status Status { get; set; }

	public string CustomerId { get; set; }

	public string CustomerName { get; set; }

	public int LinesCount { get; set; }
}

IList<OrderStatistics> statistics = session.Query<Order>()
	.TransformWith<OrderStatisticsTransformer, OrderStatistics>()
	.Where(x => x.CustomerId == "customers/1")
	.ToList();

III. Can be used with automatic indexes.

OrderStatistics statistic = session.Load<OrderStatisticsTransformer, OrderStatistics>("orders/1");