trololog85
trololog85

Reputation: 31

using await inside another await

I am developing a web api and I have some async methods from EF. All the examples I read on the internet show simple calls or snipets, but not what I am looking for...so, this is my question:

I have this method in my repository class:

public async Task GuardarLibro(Book book)
{
    var dbLibro = _libroConverter.Convert(libro);

    using (_migraPleContext)
    {
        _migraPleContext.Libro.Add(dbLibro);
        await _migraPleContext.SaveChangesAsync();
    }
}

Then I have another class that calls my repository...since, the method from my repository is async, I call it this way:

var libroDb = _libroConverter.Convert(libro);
await _libroRepository.GuardarLibro(libroDb);

My question is, if this approach is correct. I am using an await in my handler class and another await in my repository class, and I am not sure if this is a good practice or if it has a performance impact.

Upvotes: 2

Views: 2509

Answers (1)

Russ Cam
Russ Cam

Reputation: 125538

This is one correct way in which async/await can be used.

You may consider also calling .ConfigureAwait(false); on each awaitable Task if you do not require the context to captured.

Upvotes: 3

Related Questions