如何异步运行 NHibenate 查询? [英] How can I run NHibenate queries asynchronously?

查看:21
本文介绍了如何异步运行 NHibenate 查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提高服务器应用程序可扩展性的一种方法是异步运行 IO 绑定操作(读取文件、套接字、Web 请求、数据库请求等).这并不意味着在 ThreadPool 中运行它们,它只会在执行操作时阻塞线程.正确的方法是使用异步 API(BeginRead、BeginGetResponse、BeginExecuteReader 等).该问题在 CLR vi C# 一书中得到了很好的描述.

One way to increase scalability of the server application is to run IO-bound operation (reading files, sockets, web requests, database requests etc) asynchronously. This does not mean run them in the ThreadPool which will just block threads while operation is being executed. The correct way is to use asynchronous API (BeginRead, BeginGetResponse, BeginExecuteReader etc). The problem is well described in CLR vi C# book.

这里有一些关于 异步查询的文章SQL.

有什么方法可以异步执行 Nhibernate 查询?Linq 到 NHibernate 怎么样?

Are any ways to execute Nhibernate query asynchonously? What about Linq to NHibernate?

谢谢,安德烈

推荐答案

从 NHibernate v5 开始,现在完全支持异步!

As of NHibernate v5, async is now fully supported!

这里有一些漂亮的例子:

Here are some nifty examples:

Customer customer = await session.GetAsync<Customer>(1);

List<Customer> customers = await session.Query<Customer>().ToListAsync();

Customer customer = await session.Query<Customer>()
.Where(x => x.Name.Contains("Customer 1"))
.SingleOrDefaultAsync();

更新实体

using (ISession session = sessionFactory.OpenSession())
using (ITransaction transaction = session.BeginTransaction())
{
    Customer customer = await session.GetAsync<Customer>(1);
    customer.Name = "Customer 3";
    await session.SaveOrUpdateAsync(customer);
    await transaction.CommitAsync();
}

来源文章

这篇关于如何异步运行 NHibenate 查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆