@@ -1405,4 +1405,67 @@ var items = list.GetEnumerable();]]></programlisting>
14051405 </para >
14061406
14071407 </sect1 >
1408+
1409+ <sect1 id =" performance-future" >
1410+ <title >Future results</title >
1411+
1412+ <para >
1413+ Queries can be converted to future results instead of being directly executed. Future
1414+ results are not evaluated till one gets executed. At that point, all defined future
1415+ results are evaluated in one single round-trip to the database.
1416+ </para >
1417+
1418+ <para >
1419+ Future results are an alternative to using <xref linkend =" performance-multi-query" />.
1420+ They avoid the need to explicitly regroup queries, but they also hide which queries will
1421+ get executed: any pending future results of the session will be batched together, no
1422+ matter where they were defined, included out-of-scope pending future results.
1423+ </para >
1424+
1425+ <para >
1426+ Future results are obtained by calling <literal >Future</literal > or
1427+ <literal >FutureValue</literal > methods of a HQL, Criteria, QueryOver or SQL query.
1428+ For LINQ queries, the methods are named <literal >ToFuture</literal > and
1429+ <literal >ToFutureValue</literal >, see <xref linkend =" querylinq-futureresults" /> for
1430+ an example.
1431+ </para >
1432+
1433+ <programlisting ><![CDATA[ // Define queries
1434+ IFutureEnumerable<Cat> cats =
1435+ session.CreateQuery("from Cat c where c.Color = :color")
1436+ .SetString("color", "black")
1437+ .Future();
1438+ IFutureValue<int> catCount =
1439+ session.QueryOver<Cat>()
1440+ .ToRowCountQuery()
1441+ .FutureValue<int>();
1442+ // Execute them
1443+ foreach(Cat cat in cats.GetEnumerable())
1444+ {
1445+ // Do something
1446+ }
1447+ if (catCount.Value > 10)
1448+ {
1449+ // Do something
1450+ }
1451+ ]]> </programlisting >
1452+
1453+ <para >
1454+ In the above example, accessing <literal >catCount.Value</literal > does not trigger a round-trip
1455+ to the database: it has been evaluated with <literal >cats.GetEnumerable()</literal > call. If
1456+ instead <literal >catCount.Value</literal > was accessed first, it would have executed both
1457+ future results and <literal >cats.GetEnumerable()</literal > would not have triggered a round-trip
1458+ to the database.
1459+ </para >
1460+
1461+ <para >
1462+ As showcased in the previous example, <literal >Future</literal > allows to get a future enumerable
1463+ result, and <literal >FutureValue</literal > is meant to obtain a single value result.
1464+ </para >
1465+
1466+ <para >
1467+ Note: in NHibernate v5.1 and previous versions, Criteria/QueryOver future results were batched
1468+ separately. Since NHibernate v5.2, they are batched with other querying API future results.
1469+ </para >
1470+ </sect1 >
14081471</chapter >
0 commit comments