Dapper – The Alternative ORM

Dapper is the best alternative for Entity Framework, and in many situations, it is better. In this article, I will explain why Dapper it’s my favorite ORM.

If you think that Dapper is not suitable for you, consider that Stack Overflow uses.

Dapper vs Entity Framework

The most significant difference between these two is that you write SQL queries instead of C# LINQ Code when using Dapper.

The Entity Framework is suitable when you don’t rely too much on the database. If you perform complex LINQ queries, the SQL that is generated is not the best in performance.

On the other part, Dapper is just a helper library that executes your SQL scripts and maps the result to an object.

The most significant advantage of Entity Framework is that you don’t write SQL scripts, but let’s be honest: SQL is more straightforward and better than LINQ.

When you have to join multiple tables and have some filters, it is much easier to write the SQL query instead of C# code.

Performance

In matters of performance, Dapper does a great job. It is almost as fast as handed code.

I have a benchmark for some selects and inserts, and this is the result:

Method Duration
Dapper 439.9
Entity Framework 792.8

 

Dapper Features

  1. Works with most types of databases. Dapper supports SQL Server, Azure Databases, MySQL, Oracle, PostgreSQL, SQLite, and SQL Compact.
  2. Dapper already has a lot of helper methods inspired by Entity Framework that can bring your data in a particular form: using IDbConnection conn = Connection; conn.Open(); var payment= await conn.QueryFirstOrDefaultAsync("SELECT TOP 1 FROM Payments;");
  3. It is almost as fast as hand-coded data retrieval using the SqlCommand class. Entity Framework instead takes two more times to perform the same query.
  4. You can quickly execute store procedures and map the result to a C# entity. var payments = await conn.QueryAsync<Payment>("dbo.GetPayments", new { Month = "November" }, commandType: CommandType.StoredProcedure);
  5. Dapper supports transactions using IDbConnection conn = Connection; conn.Open(); using var transaction = conn.BeginTransaction(); company.id = await conn.QuerySingleAsync<int>("dbo.AddCompany", new { Name = company.Name, company.Position, company.HasBankInfo }, commandType: CommandType.StoredProcedure); transaction.Commit();

When to use Dapper

I recommend using Dapper instead of Entity Core whenever you interact with the database multiple times in the same operation.

For example, you have to select a row from a table, insert a new record in another table and update the information in another. In my opinion, this is the perfect scenario when you can use Dapper.

If you only insert data as it is, without any modification, and after that, you select it, then Entity Framework will do an excellent job for you.

Another reason for using Dapper is that you already have the database used in production. Entity Framework database first will give you headaches with errors that you don’t know what it means.

Also, if the database is already in production, it probably uses SQL procedures. So, you can use Dapper to call them and map the result to C# classes. Using Entity Core, you will have to write the C# code to query the database.

3 thoughts on “Dapper – The Alternative ORM”

Leave a Comment