Take advantage of the async methods in Dapper to improve the scalability and performance of your ASP.NET Core applications. Credit: Thinkstock Dapper is an easy to use, lightweight, flexible, fast, and open source “micro ORM” (object-relational mapper) that was developed by Sam Saffron of Stack Overflow. You can take advantage of Dapper to simplify data access and ensure high performance in your applications. Dapper supports asynchronous operations as well. Dapper was built with performance and ease of use in mind. You can learn more about Dapper from my previous article here. In this article, we’ll examine how we can perform asynchronous operations using Dapper in ASP.NET Core. To work with the code examples illustrated in this article, you should have Visual Studio 2019 installed in your system. If you don’t already have a copy, you can download Visual Studio 2019 here. Create an ASP.NET Core API project in Visual Studio First off, let’s create an ASP.NET Core project in Visual Studio. Assuming Visual Studio 2019 is installed in your system, follow the steps outlined below to create a new ASP.NET Core project in Visual Studio. Launch the Visual Studio IDE. Click on “Create new project.” In the “Create new project” window, select “ASP.NET Core Web Application” from the list of templates displayed. Click Next. In the “Configure your new project” window, specify the name and location for the new project. Click Create. In the “Create New ASP.NET Core Web Application” window shown next, select .NET Core as the runtime and ASP.NET Core 2.2 (or later) from the drop-down list at the top. I’ll be using ASP.NET Core 3.0 here. Select “API” as the project template to create a new ASP.NET Core API application. Ensure that the check boxes “Enable Docker Support” and “Configure for HTTPS” are unchecked as we won’t be using those features here. Ensure that Authentication is set as “No Authentication” as we won’t be using authentication either. Click Create. You should now have a new ASP.NET Core API project in Visual Studio. Select the Controllers solution folder in the Solution Explorer window and click “Add -> Controller…” to create a new controller named DefaultController. We’ll use this project in the subsequent sections of this article. Install the Dapper NuGet package Now that you have created an ASP.NET Core application in Visual Studio, the next thing you should do is install the necessary NuGet package, named Dapper. You can install this package from the NuGet package manager inside the Visual Studio 2019 IDE. Alternatively, you can write the following command to install this package via the .NET CLI. dotnet add package Install-Package Dapper Once the Dapper package has been installed successfully into your project, you can start using Dapper. Asynchronous methods in Dapper Dapper contains several asynchronous methods that you can use to perform asynchronous CRUD operations. Here is the list of asynchronous methods in Dapper: ExecuteAsync QueryAsync QueryFirstAsync QueryFirstOrDefaultAsync QuerySingleAsync QuerySingleOrDefaultAsync QueryMultipleAsync We’ll use the first two methods from the above list in the example below. Dapper QueryAsync and ExecuteAsync examples Create a new class named Author. This is our entity class. public class Author { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Address { get; set; } } Next, create an interface called IAuthorRepository and write the following code there. public interface IAuthorRepository { Task<Author> GetByID(int id); public Task<bool> Insert(Author model); } The AuthorRepository class extends the IAuthorRepository interface. public class AuthorRepository { private readonly IConfiguration _config; public AuthorRepository(IConfiguration config) { _config = config; } public async Task<Author> GetByID(int id) { using (IDbConnection conn = new SqlConnection(_config.GetConnectionString ("IDGDbConnectionString"))) { string query = "SELECT Id, FirstName, LastName, Address FROM Author WHERE Id = @id"; conn.Open(); var result = await conn.QueryAsync<Author>( query, new { Id = id }); return result.FirstOrDefault(); } } public async Task<bool> Insert(Author model) { return false; } } Note how the QueryAsync method has been used here to query an Author record asynchronously. Now replace the Insert method with the following code. public async Task<bool> Insert(Author model) { int x = -1; using (var connection = new SqlConnection (_config.GetConnectionString ("IDGDbConnectionString"))) { await connection.OpenAsync(); var sqlStatement = @" INSERT INTO Author (FirstName, LastName, Address)"; x = await connection.ExecuteAsync(sqlStatement, model); } return x > 0; } Note how the ExecuteAsync method has been used here. The ExecuteAsync method accepts two parameters. These include the SQL statement and an object containing the data to be inserted or updated. Use dependency injection for database connectivity in ASP.NET Core You can specify the connection string to be used to connect to the database in the appSettings.json file. Then you can use the GetConnectionString() method of the IConfiguration instance to retrieve the connection string. Next, you should take advantage of dependency injection to inject the IConfiguration instance to the AuthorRepository class as shown in the code snippet given below. public class AuthorRepository : IAuthorRepository { private readonly IConfiguration _config; public EmployeeRepository(IConfiguration config) { _config = config; } //Other methods } The following code snippet shows how you can create an instance of the SqlConnection class. public IDbConnection DbConnection { get { return new SqlConnection(_config.GetConnectionString ("IDGDbConnectionString")); } } The DbConnection property can now be used in lieu of creating an instance of the SqlConnection class as shown earlier. You can modify the Insert method of the AuthorRepository as shown below. public async Task<bool> Insert(Author model) { int x = -1; using (var connection = DbConnection) { await connection.OpenAsync(); var sqlStatement = @" INSERT INTO Author (FirstName, LastName, Address)"; x = await connection.ExecuteAsync(sqlStatement, model); } return x > 0; } Add the repository to the services layer in ASP.NET Core You can write the following code to add the repository to the services layer using dependency injection so that you can access it from the controllers. public void ConfigureServices(IServiceCollection services) { services.AddTransient<IAuthorRepository , AuthorRepository>(); //Other code } Use the repository instance in the controller in ASP.NET Core We will now take advantage of dependency injection to use the AuthorRepository instance in the controller. The following code snippet illustrates how this can be done. [Route("api/[controller]")] [ApiController] public class AuthorController : ControllerBase { private readonly IAuthorRepository _authorRepository; public AuthorController(IAuthorRepository authorRepository) { _authorRepository = authorRepository; } //Action methods } Finally, the following code snippet illustrates how you can retrieve an Author instance using an instance of AuthorRepository. [HttpGet] [Route("{id}")] public async Task<ActionResult<Author>> GetByID(int id) { return await _authorRepository.GetByID(id); } Dapper is a lightweight ORM built by Sam Saffron of Stack Overflow. Object-relational mappers are used to eliminate the “impedance mismatch” that exists between the object models of programming languages and the data models of databases. If you would like to execute advanced queries using Dapper, you should look at the DapperExtensions library. You can read my article on advanced operations in Dapper here. Related content feature 14 great preprocessors for developers who love to code Sometimes it seems like the rules of programming are designed to make coding a chore. Here are 14 ways preprocessors can help make software development fun again. By Peter Wayner Nov 18, 2024 10 mins Development Tools Software Development feature Designing the APIs that accidentally power businesses Well-designed APIs, even those often-neglected internal APIs, make developers more productive and businesses more agile. By Jean Yang Nov 18, 2024 6 mins APIs Software Development news Spin 3.0 supports polyglot development using Wasm components Fermyon’s open source framework for building server-side WebAssembly apps allows developers to compose apps from components created with different languages. By Paul Krill Nov 18, 2024 2 mins Microservices Serverless Computing Development Libraries and Frameworks news Go language evolving for future hardware, AI workloads The Go team is working to adapt Go to large multicore systems, the latest hardware instructions, and the needs of developers of large-scale AI systems. By Paul Krill Nov 15, 2024 3 mins Google Go Generative AI Programming Languages Resources Videos