我应该在退货时使用等待吗?

时间:2022-07-25 20:17:15

I have an MVC web application, the controller action calls a code in a business layer, which in turn calls an API:

我有一个MVC Web应用程序,控制器操作调用业务层中的代码,而后者又调用API:

public async Task<ActionResult> TestAction(){
   Info something = await businessLayer.GetInfoAsync();
   return View(something);
}

in the business layer I have GetInfoAsync defined as follows:

在业务层我有GetInfoAsync定义如下:

public async Task<Info> GetInfoAsync(){
   return await myLib.GetAsync<Info>();
}

in my library I have GetAsync defined as follows:

在我的库中,我将GetAsync定义如下:

public async Task<T> GetAsync(){
   //do something
   var data = await aService.Get<T>();
   return data;
}

As you can see above GetInfoAsync method just makes a call to myLib.GetAsync and returns whatever value myLib.GetAsync returns. Could I just get rid of async/await for GetInfoAsync, and define it as follows :

正如您在上面看到的,GetInfoAsync方法只是调用myLib.GetAsync并返回myLib.GetAsync返回的任何值。我可以摆脱GetInfoAsync的async / await,并按如下方式定义它:

public Task<Info> GetInfoAsync(){
   return myLib.GetAsync<Info>();
}

Thanks !

谢谢 !

1 个解决方案

#1


1  

Yes, you can (and should) get rid of async/await in the middle layer.

是的,您可以(并且应该)摆脱中间层的异步/等待。

If your async method is just awaiting a single task (and that's all it does), then the async just adds overhead.

如果您的异步方法只是在等待单个任务(并且就是这样),那么异步只会增加开销。

#1


1  

Yes, you can (and should) get rid of async/await in the middle layer.

是的,您可以(并且应该)摆脱中间层的异步/等待。

If your async method is just awaiting a single task (and that's all it does), then the async just adds overhead.

如果您的异步方法只是在等待单个任务(并且就是这样),那么异步只会增加开销。