I was going through an online video and the author had the below code:
public async Task CreateBookAsync(BookDto newBook)
{
var book = new Book(newBook.Id, newBook.Title, newBook.Author, newBook.Price);
await _bookRepository.AddAsync(book);
await _bookRepository.SaveChangesAsync();
}
BookRepository:
public Task AddAsync(Book book)
{
_dbContext.Add(book);
return Task.CompletedTask;
}
public async Task SaveChangesAsync()
{
await _dbContext.SaveChangesAsync();
}
I was not sure why AddAsync returns a Task and not just void, why have return Task.CompletedTask. Is it because the CreateBookAsync is using async Task, its beneficial to keep all methods within using Task? Would it make a difference if it just returned void?