Skip to content

Instantly share code, notes, and snippets.

@erdtsieck
Last active December 15, 2020 13:57
Show Gist options
  • Save erdtsieck/06ee43b1539191de68ba to your computer and use it in GitHub Desktop.
Save erdtsieck/06ee43b1539191de68ba to your computer and use it in GitHub Desktop.
This gist shows how to use the NHibernate async API
#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
@Korayem
Copy link

Korayem commented Jun 24, 2018

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?

@ydang204
Copy link

ydang204 commented Sep 13, 2018

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;
        }

@erdtsieck
Copy link
Author

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