Exploring Dapper In .NET Core

Introduction

In this article, we would talk about Dapper (a micro ORM) and how it can be used in .NET CORE to enhance the performance of existing EF Core and EF ORMs.

It will cover the following things,

  • What is Dapper?
  • Why Dapper?
  • Micro ORM vs ORM
  • Dapper vs EF Core
  • Hands-on Lab – Create a .NET Core API project and Implement Dapper into it

What is Dapper?

  • High-performance data access system built by the Stack Overflow team and released as open source.
  • A simple object mapper framework helps map the native query output to a domain class or a C# class. 
  • Falls into a family of tools known as micro-ORMs.
  • Dapper has great performance because it doesn’t translate queries that we write in .NET to SQL.
  • It extends ADO.NET’s IDBConnection and provides useful extension methods to query our database. And, we have to write queries compatible with our database provider.

Why Dapper?

  • Dapper is the fastest ORM
  • Perform CRUD operations directly using the IDBConnection object.
  • Provide querying static and dynamic data over the database.
  • Get generic results for simple or complex data types.
  • Dapper allows storing bulk data at once.

Micro ORM vs ORM

Parameter Micro ORM ORM
Map queries to objects
Procedure/Query Execution
Caching results
Change tracking
SQL generation
Identity management
Lazy loading
Database migrations

Dapper vs EF Core

Dapper EF Core
Micro ORM ORM
Preferred by people who write SQL Preferred by people who love to code but not SQL
Faster than EF Core Slower than Dapper
Performance is priority Productivity is priority
Development time is longer Faster than Dapper
Easy learning curve Difficult learning curve
Stored procedures and views friendly Stored procedures and views are not that easy

Create a .NET Core API project and Implement Dapper into it

Steps to be followed,

  • Open VS 2022 and create a project
  • Choose ASP.NET Core API as a template, create a directory and choose .NET 6.0

Exploring Dapper in .NET CORE

Create a Test DB and an employee table in the SQL table, attaching the query to do that as well.

Exploring Dapper in .NET CORE

Now, create an Employee Controller in the solution and a single endpoint named GetEmployee

Exploring Dapper in .NET CORE

Include the connection string pointing to the database Test and table Employee in the appsettings.json

Exploring Dapper in .NET CORE

Install the latest version of Dapper and Microsoft.Data.SqlClient from the Nuget explorer

Exploring Dapper in .NET CORE

Create an Employee class with all the below properties which would be mapped to the return object

  • Id
  • Name
  • Department
  • Gender

Exploring Dapper in .NET CORE

After that, come to the controller and create an API to return all the employees stored in DB, within that, create a DBConnection object and write a select query to return all the records from the DB.

Exploring Dapper in .NET CORE

Now, write a query to insert some records in Employee to get that extracted from the above API endpoint

Exploring Dapper in .NET CORE

Execute the application (Press F5) and open the swapper explorer and execute the todolist GetEmployeeDetails API to extract all the details of the employee stored in the database.

Exploring Dapper in .NET CORE

In this article, we talked about Dapper's overview and implementation.

Attaching the source code as well for reference.

Hope you see you guys again soon.

Happy learning!


Similar Articles