- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
以下代码从 3 个不同的资源中获取属于客户的投资列表。该流程以 Controller 的调用开始,并遵循下面描述的流程,其中所有方法都声明为异步并使用 await 运算符调用。
我想知道将所有方法设为异步是否有问题。是否有任何性能损失?是代码异味还是反模式?
我知道有些事情必须等待,比如访问 url、从 cahce 获取数据等。但我认为有些事情比如填充列表或汇总一些值不需要异步。
下面遵循代码(为清楚起见省略了某些部分):
Controller
{HttpGet]
public async Task<IActionResult> Get()
{
Client client = await _mediator.Send(new RecuperarInvestimentosQuery());
return Ok(cliente);
}
public async Task<Client> Handle(RecoverInvestimentsQuery request, CancellationToken cancellationToken)
{
Client client;
List<Investiment> list = await _investimentBuilder.GetInvestiments();
client = new Cliente(request.Id, list);
return client;
}
public async Task<List<Investiment>> GetInvestiments()
{
ListInvestiments builder = new ListInvestiments();
await builder.BuildLists(_builder);
// here I get the List<Investiment> list already fulfilled to return to the controller
return list;
}
public async Task BuildLists(IBuilder builder)
{
Task[] tasks = new Task[] {
builder.GetFundsAsync(), //****
builder.ObterTesouro(),
builder.ObterRendaFixa()
};
await Task.WhenAll(tasks);
}
public async Task GetFundsAsync()
{
var listOfFunds = await _FundsService.RecoverFundsAsync();
// listOfFunds will get all items from all types of investments
}
public async Task<List<Funds>> RecoverFundsAsync()
{
var returnCache = await _clientCache.GetValueAsync("fundsService");
// if not in cache, so go get from url
if (returnCache == null)
{
string url = _configuration.GetValue<string>("Urls:Funds");
var response = await _clienteHttp.ObterDadosAsync(url);
if (response != null)
{
string funds = JObject.Parse(response).SelectToken("funds").ToString();
await _clienteCache.SetValueAsync("fundService", funds);
return JsonConvert.DeserializeObject<List<Funds>>(fundos);
}
else
return null;
}
return JsonConvert.DeserializeObject<List<Funds>>(returnCache);
}
public async Task<string> GetDataAsync(string Url)
{
using (HttpClient client = _clientFactory.CreateClient())
{
var response = await client.GetAsync(Url);
if (response.IsSuccessStatusCode)
return await response.Content.ReadAsStringAsync();
else
return null;
}
}
public async Task<string> GetValueAsync(string key)
{
IDatabase cache = Connection.GetDatabase();
RedisValue value = await cache.StringGetAsync(key);
if (value.HasValue)
return value.ToString();
else
return null;
}
最佳答案
你的代码对我来说看起来不错。您正在使用 async
和 await
只为 I/O
和网络访问操作,它非常适合 async
and await
purposes:
async
和
await
,那么您的所有代码也趋于异步。这个事实在 MSDN 文章 -
Async/Await - Best Practices in Asynchronous Programming 中有详细描述。 :
Asynchronous code reminds me of the story of a fellow who mentioned that the world was suspended in space and was immediately challenged by an elderly lady claiming that the world rested on the back of a giant turtle. When the man enquired what the turtle was standing on, the lady replied, “You’re very clever, young man, but it’s turtles all the way down!” As you convert synchronous code to asynchronous code, you’ll find that it works best if asynchronous code calls and is called by other asynchronous code—all the way down (or “up,” if you prefer). Others have also noticed the spreading behavior of asynchronous programming and have called it “contagious” or compared it to a zombie virus. Whether turtles or zombies, it’s definitely true that asynchronous code tends to drive surrounding code to also be asynchronous. This behavior is inherent in all types of asynchronous programming, not just the new async/await keywords.
关于c# - 嵌套的异步方法太多。那是问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62104283/
我在尝试执行以下代码时遇到了一些麻烦: #include #include int main(int argc, char** argv) { std::wstring buffer; /
这是一个有点出乎意料的行为,可能会咬到初学者。首先,这是故意的吗?其次,Perl 6 还使用哪些其他东西来猜测要创建哪个对象?它是开始认为是 Block 还是 Hash 并稍后更改,还是最终决定? 您
subArraySum 中的 for 循环调用 sumArray ,它也包含一个 for 循环。这会被认为是 O(n^2) 时间吗?放轻松,因为这是我的第一个问题,正如您从我的代码中看到的那样,我是初
我是一名优秀的程序员,十分优秀!