Skip to content

Commit 25777a3

Browse files
Document future results (nhibernate#1786)
1 parent 0e591a2 commit 25777a3

File tree

2 files changed

+65
-5
lines changed

2 files changed

+65
-5
lines changed

doc/reference/modules/performance.xml

+63
Original file line numberDiff line numberDiff line change
@@ -1342,4 +1342,67 @@ ICollection<Policy> policies = CollectionHelper.ToArray<Policy>(results[1]);]]><
13421342
perform additional logic (getting the policies of the customers we are associated with), all in a single database round-trip.
13431343
</para>
13441344
</sect1>
1345+
1346+
<sect1 id="performance-future">
1347+
<title>Future results</title>
1348+
1349+
<para>
1350+
Queries can be converted to future results instead of being directly executed. Future
1351+
results are not evaluated till one gets executed. At that point, all defined future
1352+
results are evaluated in one single round-trip to the database.
1353+
</para>
1354+
1355+
<para>
1356+
Future results are an alternative to using <xref linkend="performance-multi-query"/>.
1357+
They avoid the need to explicitly regroup queries, but they also hide which queries will
1358+
get executed: any pending future results of the session will be batched together, no
1359+
matter where they were defined, included out-of-scope pending future results.
1360+
</para>
1361+
1362+
<para>
1363+
Future results are obtained by calling <literal>Future</literal> or
1364+
<literal>FutureValue</literal> methods of a HQL, Criteria, QueryOver or SQL query.
1365+
For LINQ queries, the methods are named <literal>ToFuture</literal> and
1366+
<literal>ToFutureValue</literal>, see <xref linkend="querylinq-futureresults"/> for
1367+
an example.
1368+
</para>
1369+
1370+
<programlisting><![CDATA[// Define queries
1371+
IFutureEnumerable<Cat> cats =
1372+
session.CreateQuery("from Cat c where c.Color = :color")
1373+
.SetString("color", "black")
1374+
.Future();
1375+
IFutureValue<int> catCount =
1376+
session.QueryOver<Cat>()
1377+
.ToRowCountQuery()
1378+
.FutureValue<int>();
1379+
// Execute them
1380+
foreach(Cat cat in cats.GetEnumerable())
1381+
{
1382+
// Do something
1383+
}
1384+
if (catCount.Value > 10)
1385+
{
1386+
// Do something
1387+
}
1388+
]]></programlisting>
1389+
1390+
<para>
1391+
In the above example, accessing <literal>catCount.Value</literal> does not trigger a round-trip
1392+
to the database: it has been evaluated with <literal>cats.GetEnumerable()</literal> call. If
1393+
instead <literal>catCount.Value</literal> was accessed first, it would have executed both
1394+
future results and <literal>cats.GetEnumerable()</literal> would not have triggered a round-trip
1395+
to the database.
1396+
</para>
1397+
1398+
<para>
1399+
As showcased in the previous example, <literal>Future</literal> allows to get a future enumerable
1400+
result, and <literal>FutureValue</literal> is meant to obtain a single value result.
1401+
</para>
1402+
1403+
<para>
1404+
Note: in NHibernate v5.1 and previous versions, Criteria/QueryOver future results were batched
1405+
separately. Since NHibernate v5.2, they are batched with other querying API future results.
1406+
</para>
1407+
</sect1>
13451408
</chapter>

doc/reference/modules/query_linq.xml

+2-5
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ using NHibernate.Linq;]]></programlisting>
425425

426426
<para>
427427
Future results are supported by the Linq provider. They are not evaluated till one gets executed.
428-
At that point, all defined future results are evaluated in one single round-trip to database.
428+
At that point, all defined future results are evaluated in one single round-trip to the database.
429429
</para>
430430
<programlisting><![CDATA[// Define queries
431431
IFutureEnumerable<Cat> cats =
@@ -446,10 +446,7 @@ if (catCount.Value > 10)
446446
}
447447
]]></programlisting>
448448
<para>
449-
In above example, accessing <literal>catCount.Value</literal> does not trigger a round-trip to database:
450-
it has been evaluated with <literal>cats.GetEnumerable()</literal> call. If instead
451-
<literal>catCount.Value</literal> was accessed first, it would have executed both future and
452-
<literal>cats.GetEnumerable()</literal> would have not trigger a round-trip to database.
449+
See <xref linkend="performance-future" /> for more information.
453450
</para>
454451
</sect1>
455452

0 commit comments

Comments
 (0)