Quantcast
Channel: Developer Express Inc.
Viewing all articles
Browse latest Browse all 2370

XPO - Async/Await Method Support (v18.2)

$
0
0

With v18.2, XPO's Session and other key classes ship with a set of async APIs, including FindObjectAsync, GetObjectByKeyAsync, CommitChangesAsync (which return System.Threading.Tasks.Task object).

These Task-based APIs are important for both server and client programming (useful for long and DB-intensive operations like commits, queries, delete operations, etc.) as they improve server scalability and deliver a more resilient user experience, especially on mobile devices.

To see how asynchronous CRUD operations work with XPO, refer to the following code from an ASP.NET Core MVC application:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using DevExpress.Xpo.AspNetCoreMvcDemo.Models;
using DevExpress.Xpo.Demo.Entities;

namespace DevExpress.Xpo.AspNetCoreMvcDemo.Controllers {
    public class DataController : Controller {
        readonly UnitOfWork uow;
        public DataController(UnitOfWork uow) {
            this.uow = uow;
        }
        public async Task<IActionResult> Index() {
            var users = await uow.Query<User>()
                .OrderBy(u => u.LastName)
                .ThenBy(u => u.FirstName)
                .Select(u => new UserModel {
                    Oid = u.Oid,
                    FirstName = u.FirstName,
                    LastName = u.LastName,
                    Email = u.Email
                }).ToListAsync();
            int totalCount = await uow.Query<User>().CountAsync();
            var viewModel = new DataViewModel() {
                Users = users,
                TotalCount = totalCount
            };
            return View(viewModel);
        }
        [HttpPost]
        public async Task<IActionResult> Delete(Guid id) {
            var user = await uow.GetObjectByKeyAsync<User>(id);
            if(user != null) {
                uow.Delete(user);
                await uow.CommitChangesAsync();
            }
            return RedirectToAction("Index");
        }
        [HttpPost]
        public async Task<IActionResult> Create(UserModel model) {
            if(model != null) {
                var newUser = new User(uow) {
                    FirstName = model.FirstName,
                    LastName = model.LastName,
                    Email = model.Email
                };
                await uow.CommitChangesAsync();
            }
            return RedirectToAction("Index");
        }
    }
}

To get started, simply download our GitHub demo. See also the Asynchronous Programming Patterns and Task-based Asynchronous Pattern articles in Microsoft .NET Guide.

Please note that 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.

Your feedback counts!

Everyone can use our https://www.nuget.org/packages/DevExpress.Xpo/ Nuget package - it's free.  Check the Include prerelease option when finding and installing the package:

Once you’ve had the opportunity to try this new feature in your upgraded v18.2 project, feel free to report issues and suggestions via the DevExpress Support Center.  By offering early build access, we hope to discover whether our new features and solutions effectively address your requirements. Your assistance and feedback will help us deliver the best possible solution.

Before we let you go, please take a moment to answer the following survey question:

As always, thank you very much for your feedback. 


Viewing all articles
Browse latest Browse all 2370

Trending Articles