Q: How do I get started with asynchronous operations in general and their XPO use in particular?
A: Check out the Asynchronous Programming Patterns and Task-based Asynchronous Pattern articles in Microsoft .NET Guide. To get started with XPO asynchronous APIs specifically, consider the following resources:
- see the XPO - Async/Await Method Support (v18.2) blog post;
- download our GitHub demo;
- review our XpoTutorials demo files ("c:\Users\Public\Documents\DevExpress Demos 18.2\Components\WinForms\CS\XpoTutorials\AsyncLoadingTaskBased\AsyncLoadingTaskBased.cs" );
- consult with the Session, XPQueryExtensions, and other key API references.
Q: Are there any plans to extend XAF's IObjectSpace to support async/await?
A: While we cannot provide any ETA, this XPO feature is definitely the first step in this direction. This support will bring XAF users the following benefits:
- improve XAF WinForms UX and perceived performance with the help of asynchronous Actions (open DetailView from ListView, save DetailView, execute long custom Actions);
- improve the new ASP.NET Core Data Service throughput under a high load.
For more information on this feature, please refer to the following support tickets: T112329, AS6060, CQ59274, Q352118.
Right away, you can use the IObjectSpace > GetObjectsQuery<T> method with ToListAsync, ToArrayAsync, CountAsync and other suitable IQueryable methods. Create, update and delete operations require us to extend IObjectSpace and then support these XXXAsync methods at the UI level. However, nothing prevents you from wrapping these IObjectSpace method calls into the Task.Run method manually, if required.
Q: Do these new Task-based APIs provide notifications (for instance, cancellation, progress)?
A: All XPO methods accept System.Threading.CancellationToken. In v18.2, we do not provide any means for reporting progress, though. Even if we had provided XPO method overloads accepting the IProgress<T> parameters, it would not be much usable: the progress would stay at 0% most of the method execution time and then immediately go to 100%.
That is mainly because asynchronous execution is implemented at the XPO layer in v18.2. XPO connection providers call synchronous versions of IDbConnection, IDbCommand, IDataReader, and other ADO.NET APIs. Also, not all database engines supported by XPO ship with ADO.NET drivers providing async methods. While this is a complex task, we plan to enhance XPO asynchronous operation support in future releases.
Q: Can I use async/await with direct SQL queries (Session methods like ExecuteScalar, ExecuteQuery, GetObjectsFromQuery, etc.)?
A: No (see the previous point on asynchronous support at the ADO.NET level). However, nothing prevents you from wrapping these method calls into the Task.Run method manually, if required.
Q: Can I use async/await with XPCollection and XPView?
A: No, XPCollection.LoadAsync and XPView.LoadAsync will not return Task and thus will not support async/await. We recommend that you use XPQuery<T> instead - it is a more efficient way to query data.
Q: Should we use the ThreadSafeDataLayer to use async/await XPO methods?
A: In general, the requirement to use the ThreadSafeDataLayer remains the same as if synchronous operations were used in the same context. If the existing code works with the SimpleDataLayer and you just change it to use asynchronous methods, you can continue using the SimpleDataLayer.
The only thing that should be avoided is the parallel execution of two asynchronous operations in the context of one Session instance. Use the await operator if you need to call an asynchronous method several times.[C#]
await session.DoSomethingAsync(); await session.DoSomethingAsync();