If that Add is calling the database, then async is warranted. HoweverHowever, I think you are overcomplicating it. WithWith Async/Await, you need async/await through all the layers until you await a SaveChangesAsyncSaveChangesAsync
or some bottom call. Ultimately
Ultimately, when this async fires, it'll free up the thread for something else to do work. Also, if you are using a framework, you should do .ConfigureAwait(false).ConfigureAwait(false)
after all of your await calls or you could end up in deadlocks for users that force a sync call. However
However, if you need the language/culture from the original thread later in the call, then you don't want to use ConfigureAwait(false)ConfigureAwait(false)
, but you could always pull off the current values, run it, then update the thread values after the await call to fix that issue. Also, all the Async methods should have the word "Async" after them, except for the top level ones on the UI to make it clear.
This is how I'd lay it out:
_Repository.AddAsync_Repository.AddAsync
, if using EF, would then await context.SaveChangesAsync()context.SaveChangesAsync()
.
If you wanted to go the extra mile, also have all methods take CancellationToken token = default(CancellationToken)CancellationToken token = default(CancellationToken)
as an optional parameter and pass that all the way down, too.