Wrap my head around waiting for asynchronous and NHibernate

Given this code:

private ISession _session; public async Task<T> GetAsync<T>(int id) where T : ISomeEntity { //how to call and return _session.Get<T>(id) asynchronous } 

Is it possible to call NHibernate ISession.Get<T>() asynchronously? Goodies? Not worth it?

+6
source share
3 answers

NHibernate does not support Async, expecting a default entity framework in terms of infrastructure. However, it would be advisable to do this (if possible), since a database call is an I / O call, which is a very good candidate to make it asynchronous. As a result, waiting for a response from the database, your thread will return to the pool instead of being deferred, and this will make your application more scalable. Now let's start asynchronous support. I developed NHibernate to achieve this. In my fork, which I call NHibernateX, there are Async methods like GetAsync, ListAsync, ToListAsync, etc. Here is the source and nuget package:

https://github.com/ReverseBlade/nhibernate-core

https://www.nuget.org/packages/NHibernateX/

+13
source

If the underlying operation is not already asynchronous (i.e. async-await , BeginXXX / EndXXX , etc.), there is no value when adding an async wrapper around it.

async is useful in 2 cases: scalability and responsiveness. Running async on synchronization doesn't help with scalability in general and for responsiveness, you can simply use Task.Run for unloading, which work with another ThreadPool thread when you need, instead of adding a dedicated method.

More in Should I expose asynchronous wrappers for synchronous methods?

+8
source

NHibernate 5 now supports template

 private ISession _session; public async Task<T> GetAsync<T>(int id) where T : ISomeEntity { return _session.GetAsync<T>(id); } 
+1
source

Source: https://habr.com/ru/post/986839/


All Articles