缠着我的头等待异步和NHibernate [英] Wrapping My Head Around await async and NHibernate
问题描述
给出以下代码:
private ISession _session;
public async Task<T> GetAsync<T>(int id) where T : ISomeEntity
{
//how to call and return _session.Get<T>(id) asynchronous
}
是否可以异步调用NHibernate ISession.Get<T>()
?建议吗?不值得吗?
Is it possible to call NHibernate ISession.Get<T>()
asynchronously? Advisable? Not worth it?
推荐答案
在默认情况下,NHibernate在实体框架的意义上不支持异步等待.但是,建议这样做(如果可以的话),因为数据库调用是IO调用,这是使其异步的很好的选择.结果,在等待来自数据库的响应时,您的线程将返回到池中而不是被挂起,这将使您的应用程序更具可伸缩性. 现在要异步支持.我已经分叉NHibernate实现了这一目标. 在我称为NHibernateX的fork中,有一些异步方法,例如GetAsync,ListAsync,ToListAsync等. 这是源代码和nuget软件包:
NHibernate does not support Async await in the sense of entity framework does by default. However it would be recommended to do so (if you could) since a database call is an IO call which is a very good candidate to make it async. As a result while waiting response from DB your thread will return to the pool instead of being pend and it will make your app more scalable. Now coming to async support. I have forked NHibernate to achive this. In my fork which I call NHibernateX, there are Async Methods like GetAsync, ListAsync, ToListAsync etc Here's the source and nuget package:
https://github.com/ReverseBlade/nhibernate-core
https://www.nuget.org/packages/NHibernateX/
这篇关于缠着我的头等待异步和NHibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!