NHibernate 中的 Future() 是什么意思?

What does Future() means in NHibernate?

我是 NHibernate 新手 IEnumerable Future() 的描述;说以下

// Summary:
//     Get a enumerable that when enumerated will execute a batch of queries in
//     a single database roundtrip

只是想知道这是什么意思,描述与单词无关'future'

Future 允许在单次往返中执行两个或多个 sql,只要数据库支持。

它也几乎是透明的,因此您会希望尽可能使用 Futures。如果 NHibernate 无法在单次往返中执行查询,它将按预期执行两次或多次查询。

来自http://ayende.com/blog/3979/nhibernate-futures

Let us take a look at the following piece of code:

using (var s = sf.OpenSession())
using (var tx = s.BeginTransaction())
{
    var blogs = s.CreateCriteria<Blog>()
        .SetMaxResults(30)
        .List<Blog>();
    var countOfBlogs = s.CreateCriteria<Blog>()
        .SetProjection(Projections.Count(Projections.Id()))
        .UniqueResult<int>();

    Console.WriteLine("Number of blogs: {0}", countOfBlogs);
    foreach (var blog in blogs)
    {
        Console.WriteLine(blog.Title);
    }

    tx.Commit();
}

This code would generate two queries to the database Two queries to the database is a expensive, we can see that it took us 114ms to get the data from the database. We can do better than that, let us tell NHibernate that it is free to do the optimization in any way that it likes

using (var s = sf.OpenSession())
using (var tx = s.BeginTransaction())
{
    var blogs = s.CreateCriteria<Blog>()
        .SetMaxResults(30)
        .Future<Blog>();
    var countOfBlogs = s.CreateCriteria<Blog>()
        .SetProjection(Projections.Count(Projections.Id()))
        .FutureValue<int>();

    Console.WriteLine("Number of blogs: {0}", countOfBlogs.Value);
    foreach (var blog in blogs)
    {
        Console.WriteLine(blog.Title);
    }

    tx.Commit();
}

Instead of going to the database twice, we only go once, with both queries at once. The speed difference is quite dramatic, 80 ms instead of 114 ms, so we saved about 30% of the total data access time and a total of 34 ms.