简体   繁体   English

在 .Net 中分层异步/等待

[英]Layering async/await in .Net

Given an application where I want to do some asynchronous execution of code does it matter that I have multiple layers of async/await methods?给定一个我想要异步执行代码的应用程序,我有多层 async/await 方法有关系吗?

Let's take for example a web application (using asp .net core).让我们以一个 Web 应用程序(使用 asp .net core)为例。

We have a controller that does something simple like:我们有一个控制器可以做一些简单的事情,比如:

public class MyController : Controller
{
    private readonly MyDbContext _context;

    public MyController(MyDbContext context)
    {
       _context = context
    }

    public async Task<ActionResult> GetResource()
    {
       var resources = await _dbContext.Resources.ToList();
       return Ok(resources);
    }
}

If I added a seperate layer of abstraction (let's say for example I'm using a repository pattern):如果我添加了一个单独的抽象层(例如,我正在使用存储库模式):

public class MyController : Controller
{
    private readonly MyRepository _repo;

    public MyController(MyRepository repo)
    {
       _repo = repo
    }

    public async Task<ActionResult> GetResource()
    {
       var resources = await _repo.GetResources();
       return Ok(resources);
    }
}

public class MyRepository 
{

    private readonly MyDbContext _context;

    public MyRepository(MyDbContext context)
    {
        _context = context;
    }

    public async Task<Resource> GetResources()
    {
        return await _context.Resources.ToList();
    } 
}

Note I'm very much aware that I could do the code underneath, but am trying to illustrate a point.请注意,我非常清楚我可以执行下面的代码,但我试图说明一点。


    public Task<Resource> GetResources()
    {
        return _context.Resources.ToList();
    }

All though this may be a contrived example because awaits will be ripped out when it gets to the bytecode.尽管这可能是一个人为的例子,因为awaits将在到达字节码时被删除。

From my understanding the run time will create a thread when it reaches an await so will it in the first example create one thread, and in the second create two threads.根据我的理解,运行时将在到达await时创建一个线程,因此在第一个示例中它会创建一个线程,并在第二个示例中创建两个线程。

With this in mind is it better to keep any asynchronous tasks as close to the entry point as possible?考虑到这一点,让任何异步任务尽可能靠近入口点是否更好?

does it matter that I have multiple layers of async/await methods?我有多层 async/await 方法有关系吗?

No. It is normal to go async all the way .不。一直走async是正常

The proper way to think about it is that your I/O operations are naturally asynchronous.正确的思考方式是您的 I/O 操作自然是异步的。 Eg, database queries.例如,数据库查询。 So anything that calls those are also naturally asynchronous.所以任何调用它们的东西自然也是异步的。 Eg, domain or logic layer.例如,域或逻辑层。 So anything that calls those are also naturally asynchronous.所以任何调用它们的东西自然也是异步的。 Eg, controller.例如,控制器。 You don't start at the controller and "make it asynchronous";您不会从控制器开始并“使其异步”; you start at the I/O and make it asynchronous.您从 I/O 开始并使其异步。 Controller methods that don't do I/O shouldn't be asynchronous.不执行 I/O 的控制器方法不应该是异步的。

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

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