简体   繁体   中英

How can i make Recursion Asynchronous using Async and Await Keyword?

i am getting multi-level dynamic menus from database. Currently i have called it successfully but i want to make it async.
here is my code;

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
    var menus = Uow.Menus.GetAll()
        .Select(m => new ParentMenuViewModel()
        {
            MenuId = m.MenuId,
            Name = m.Name,
            ParentId = m.ParentId
        })
        .ToList<ParentMenuViewModel>();
    ViewBag.Menus = CreateVM(0, menus);
    base.OnActionExecuting(filterContext);
}

public IEnumerable<ParentMenuViewModel> CreateVM(int parentid, List<ParentMenuViewModel> list)
{
    return list.Where(m=> m.ParentId==parentid)
        .Select(m=> new ParentMenuViewModel()
        {
            MenuId = m.MenuId,
            Name = m.Name,
            ParentId = m.ParentId,
            ChildMenus = CreateVM(m.MenuId, list)
        });
}

So, how can i make this asynchronous using async and await keywords?
Notice that, it loops until the children are loaded completely;
Is it even a good practice to make it async (the above code)?

Unfortunately, async action filters are not available (yet); see this question . You can vote here for the MVC team to add this capability.

Once MVC supports async action filters, you could use, eg, GetAllAsync . The recursive part does not require any async functionality because it's not I/O-bound.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM