question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: Why await inside the repository?

See original GitHub issue

Why do you have to await inside the repository? Couldn’t you just return the task? Example code

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
ardaliscommented, Dec 12, 2018

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.

0reactions
eluchsingercommented, Sep 4, 2019

@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 an await 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 the await 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”.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found