Dapper

Dapper

Dapper is an open-source object-relational mapping (ORM) for .NET which is lightweight, fast and super easy to learn. Dapper falls into the category of micro-ORM since it provides only a subset of functionalities of Entity Framework Core ORM. But believe me you can do absolutely anything with Dapper like:

  • a. Execute Raw SQL Queries including parameterized queries.
  • b. Execute Stored Procedures.
  • c. Map the results to class objects.
  • d. Seed Database.
  • e. Create Transactions.
  • f. Use any database with Dapper like SQL Server, Oracle, MySQL, MongoDB, etc.

How do you use a Dapper ORM?

Dapper can be installed to .NET apps from NuGet. Just search for “Dapper” in Tools ➤ NuGet Package Manager ➤ Manage NuGet Packages for Solution window and install it.

Dapper Package NuGet

Alternately, you can run the following command in the Tools ➤ NuGet Package Manager ➤ Package Manager Console window to install Dapper.

PM> Install-Package Dapper

Once Dapper package is installed, we are ready to perform database operations in our .NET app. In the below code we are inserting a record to a database table with Dapper.


string myCS = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=DapperStart;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";

var sql = @"Insert into Inventory(Name, Price, Quantity) values (@Name, @Price, @Quantity)";
using (var connection = new SqlConnection(myCS))
{
    var customers = await connection.ExecuteAsync(sql, new { Name = "Shirt", Price = 100, Quantity = 1 });
}

The above code is very simple to understand, here we have added a database connection string to a variable called myCS. Next we have assigned a variable sql a database insert query. A “System.Data.SqlClient.SqlConnection” object is created and then we are executing dapper’s ExecuteAsync method by passing the sql query variable “sql” and the values of 3 columns – Name, Price & Quantity in Anonymous manner.

This inserts a new record to the inventory table. Here we have written all the codes in just 4 lines showing the power of Dapper.

Dapper vs Entity Framework Core

Lets understand the differences between Dapper and Entity Framework Core.

Area Dapper Entity Framework Core
Speed Dapper is faster because it executes raw sql queries. Entity Framework Core is slower in speed since it uses multiple steps to interact with the database. Two main components of EF core are – Entity classes & Database Context object. Data is transported to Entity classes from the database using Language Integrated Query (LINQ) while Database context object is used to maintain a session with the database.
Complexity Dapper is very simple to use, you just have to execute the query to the database. EF Core is a little complex since there is a higher-level, object-oriented approach to database operations. You have to be good at LINQ.
Learning Curve Dapper has a low learning curve. All it needs is that you should be good at writing queries. EF Core has a steep learning curve since there are lots of things like DB context, Migrations, Fluent API, Conventions and configurations, change tracking, sql generation, caching, lazy loading, etc.
For Developers Developers should be good at writing SQL Queries and Stored Procedures since it is the backbone in Dapper ORM. Developers should be good at LINQ in order to work with EF Core. This is because data will be communicated in LINQ queries.

We have written a full series on Entity Framework Core which will be very useful for learning this ORM.It covers all the topics in great details. Do check it.

Is Dapper faster than EF Core?

Yes, Dapper is faster than Entity Framework Core in both execution speed as well as development time. In dapper execution of raw SQL queries are performed unlike EF Core where there is Database Context, Entities and LINQ which does high-level, object-oriented approach to database operations. Executing raw SQL Queries is always fast. Development of projects are done much faster when using Dapper since it is very easy to learn and does not have extra features like EF Core which requires some time to understand.

In my understanding, you can learn Dapper in just 1 day only unlike EF core which will take you no less than 10 days.

Now it’s time to check our series of tutorials to make you learn Dapper and use it confidently in your .NET apps.