Last active
December 15, 2020 13:57
-
-
Save erdtsieck/06ee43b1539191de68ba to your computer and use it in GitHub Desktop.
This gist shows how to use the NHibernate async API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#region ICriteria async API | |
// Usage ICriteria.ListAsync<T>() | |
var customers = await session.CreateCriteria<Customer>().ListAsync<Customer>(); | |
// Usage ICriteria.UniqueResultAsync<T>() | |
var customer = await session | |
.CreateCriteria<Customer>() | |
.Add(Restrictions.Eq("Name", "Erdtsieck")) | |
.UniqueResultAsync<Customer>(); | |
// Usage ICriteria.Future<T>() (returns IAwaitableEnumerable, that has a method AsTask()) | |
var customers = session | |
.CreateCriteria<Customer>() | |
.Future<Customer>(); | |
var products = session | |
.CreateCriteria<Product>() | |
.Future<Product>(); | |
foreach (var customer in await customers.AsTask()) { } // async | |
foreach (var product in products) { } // sync (normal) | |
// Usage ICriteria.FutureValue<T>() (returns IFutureValue, that has a method ValueAsync()) | |
var customerCountFuture = session | |
.CreateCriteria<Customer>() | |
.SetProjection(Projections.Count(Projections.Id())) | |
.FutureValue<int>(); | |
var productCountFuture = session | |
.CreateCriteria<Product>() | |
.SetProjection(Projections.Count(Projections.Id())) | |
.FutureValue<int>(); | |
var customerCount = await customerCountFuture.ValueAsync(); // async | |
var productCount = productCountFuture.Value; // sync (normal) | |
#endregion | |
#region IQueryOver async API | |
// Usage IQueryOver.ListAsync() | |
var person = await session.QueryOver<Customer>().ListAsync(); | |
// Usage IQueryOver.ListAsync<T>() | |
var person = await session.QueryOver<Customer>().Select(p => p.Name).ListAsync<string>(); | |
// Usage IQueryOver.SingleOrDefaultAsync<T>() | |
var customer = await session.QueryOver<Customer>().SingleOrDefaultAsync(); | |
// Usage IQueryOver.Future<T>() (returns IAwaitableEnumerable, that has a method AsTask()) | |
var customers = session.QueryOver<Product>().Future(); | |
var products = session.QueryOver<Product>().Future(); | |
foreach (var customer in await customers.AsTask()) { } // async | |
foreach (var product in products) { } // sync (normal) | |
// Usage IQueryOver.FutureValue<T>() (returns IFutureValue, that has a method ValueAsync()) | |
var customerFuture = session.QueryOver<Customer>().FutureValue(); | |
var productFuture = session.QueryOver<Product>().FutureValue(); | |
var customer = await customerFuture.ValueAsync(); // async | |
var product = productFuture.Value; // sync (normal) | |
#endregion |
It is Criteria, Queryable and HQL for now. Didn't show any HQL examples here, because they're less commonly used.
Thanks for posting such comprehensive list of examples
But @annemartijn0 there is no method called .AsTask()
nor .ValueAsync()
to be used with .Future()
and .FutureValue()
. Where did you get those?
hello everyone, my code is below used NHibernate version 5.13 but i cant use AsTask() method. Any help? Thanks
public async Task<List<Folder>> ListFolders()
{
var folders = new List<Folder>();
var hashtableFolders = await ContextPerRequest.Session.CreateCriteria(typeof(Folder).Name)
.Add(Restrictions.Eq("State", (int)State.Active))
.ListAsync<Hashtable>();
foreach (var hashtableFolder in await hashtableFolders.AsTask())
{
var folder = _hashtableAdapter.HashtableToObject<Folder>(hashtableFolder);
folders.Add(folder);
}
return folders;
}
I'm sorry guys. This code was part of a proof of concept for integrating async I/O in NHibernate. It did not make the product in this form, so you should search the official docs.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome! Cannot wait to see this in the core... Is there anything plain LINQ-related in the forthcoming update?