How can I run NHibenate queries asynchronously?

后端 未结 5 763
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-30 23:15

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 doe

5条回答
  •  囚心锁ツ
    2020-12-31 00:00

    multiple async calls can be rewritten with Futures

    var footask = QueryFooAsync();
    var bartask = QueryBarAsync();
    var baztask = QueryBazAsync();
    
    var foos = await footask;
    var bars = await bartask;
    var baz = await baztask;
    
    // do something with foos, bars, baz
    

    can be replaced with

    var foos = session.Query().....ToFuture();
    var bars = session.Query().....ToFuture();
    var baz = session.Query().....ToFutureValue();
    
    await Task.Factory.StartNew(() => var ignored = baz.Value)  // await the results
    
    // do something with foos, bars, baz
    

    this even has the benefit over async code that the roundtrip time is only paid once instead of 3 times.

提交回复
热议问题