简体   繁体   English

如何使用Async和Await Keyword进行递归异步?

[英]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? 那么, 我如何使用async和await关键字进行异步?
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); 不幸的是, async操作过滤器尚不可用(尚未); see this question . 看到这个问题 You can vote here for the MVC team to add this capability. 您可以在此投票给MVC团队以添加此功能。

Once MVC supports async action filters, you could use, eg, GetAllAsync . 一旦MVC支持async操作过滤器,您就可以使用例如GetAllAsync The recursive part does not require any async functionality because it's not I/O-bound. 递归部分不需要任何async功能,因为它不受I / O限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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