@@ -1342,4 +1342,67 @@ ICollection<Policy> policies = CollectionHelper.ToArray<Policy>(results[1]);]]><
1342
1342
perform additional logic (getting the policies of the customers we are associated with), all in a single database round-trip.
1343
1343
</para >
1344
1344
</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 >
1345
1408
</chapter >
0 commit comments