Typically, when you are working with outside systems, like making calls to an external database, web API, etc., you will use async operations to optimize your code while waiting for these external systems to respond, especially if they take more than a few milliseconds.
So far, we have performed all synchronous operations, but Dapper also provides async versions of all of the primary methods.
The following is the async version of the GetAllAuthors
method.
private async static Task<List<Author>> GetAllAuthorsAsync()
{
using (IDbConnection db = new SqlConnection(ConnectionString))
{
IEnumerable<Author> results = await db.QueryAsync<Author>("SELECT * FROM Authors");
return results.ToList();
}
}
Here you can see that we are using the QueryAsync
method of Dapper. Now to call this async method, we need to call it from an async Main
method.
static async Task Main(string[] args)
{
var authors = await GetAllAuthorsAsync();
foreach (var author in authors)
{
Console.WriteLine(author.FirstName + " " + author.LastName);
}
}
The following example inserts a single author asynchronously using ExecuteAsync
.
private static async void InsertSingleAuthorAsync()
{
using (IDbConnection db = new SqlConnection(ConnectionString))
{
Author author = new Author()
{
FirstName = "William",
LastName = "Shakespeare"
};
string sqlQuery = "INSERT INTO Authors (FirstName, LastName) VALUES(@FirstName, @LastName)";
int rowsAffected = await db.ExecuteAsync(sqlQuery, author);
}
}
Let's run your application, and you will see we are getting the same result. This time we did it asynchronously.