Question: Why await inside the repository?
See original GitHub issueWhy do you have to await
inside the repository? Couldn’t you just return the task?
Example code
Issue Analytics
- State:
- Created 5 years ago
- Comments:6
Top Results From Across the Web
Async/await in repository
The reason is, because calling an async function does not mean that the function is performed. It means that a Task is scheduled...
Read more >domain driven design - Are repositories async?
I just need some clarification: Is the "repository" used in two different context one of which would be DDD, where the retrieval is...
Read more >Clarifying question: can await be used in top-level code? #9
In the proposal, it looks like "await" is to be used inside async functions. Can "await" be used in top-level code? The reason...
Read more >COUNTRIES REPOSITORY USING ASYNC/AWAIT 1. In
Question : PART 1: COUNTRIES REPOSITORY USING ASYNC/AWAIT 1. In country.json, you will find a list of all countries. See sample country: {"country":"Iraq", ......
Read more >Asynchronous Programming with Async and Await in ASP. ...
The first thing it does is to check whether the operation is already complete. If it is, it will continue the method execution...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Based on the docs (and summed up here: https://stackoverflow.com/a/43497310/13729 ) I believe having the
await
within each repository method is appropriate, as it ensures no client of the repository will fire off multiple tasks in parallel.@valdirviana how does removing the
await
keyboard lose any async feature?@KhalilMohammad I am not sure what anti-pattern you’re talking about. I see at least two issues here.
The point of removing the
await
keyboard was to avoid an unnecessary context switch inside the method* (1st issue). But as @ardalis said, EF does not support two parallel operations so it is appropriate to utilize anawait
inside the method (2nd issue).* More explanation
What happens when you
await
? The execution of the asynchronous code happens in a new thread (let’s say Thread 2). When the asynchronous work is done, the further execution of the code will happen again in the originally executing thread (let’s say Thread 1), if it was originally executed by a UI Thread. This causes a possibly unnecessary context switch between these threads. Usually, you don’t need to proceed executing the code after theawait
in Thread 1 (unless you need to execute it on the UI-Thread). Also, it provides the possibility to avoid the state-machine created by the async-await keywords by using.ConfigureAwait(false)
.Edit
Actually, if you are using .NET Core, your Thread 2 will not necessarily resume on Thread 1. This is only true for applications using “UI-Threads”.