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
Create a Test DB and an employee table in the SQL table, attaching the query to do that as well.
Now, create an Employee Controller in the solution and a single endpoint named GetEmployee
Include the connection string pointing to the database Test and table Employee in the appsettings.json
Install the latest version of Dapper and Microsoft.Data.SqlClient from the Nuget explorer
Create an Employee class with all the below properties which would be mapped to the return object
- Id
- Name
- Department
- Gender
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.
Now, write a query to insert some records in Employee to get that extracted from the above API endpoint
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.
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!