Hello! I’m here with a new blog post. Recently, I’ve been spending quite some time in the C# and .NET world, especially focusing on speed and performance in REST API development. At this point, when I want to make database operations faster and cleaner, Dapper came to mind. I’ve used Dapper before, but sometimes it feels like I’m just starting fresh, so I decided to prepare a beginner’s guide for both my own review and for you.
Now you might ask, ‘Why Dapper? When Entity Framework exists?’ You’re right, Entity Framework is powerful and has many features. But sometimes the operations get simpler, and you want more control, and of course, the performance difference is noticeable. Dapper is one of the most well-known micro ORM libraries and truly simplifies processes incredibly. Think of it like riding a bicycle instead of a car; you know exactly where you’re going, and you feel more in control. 🙂
So, what exactly is Dapper? Simply put, it’s a library that maps SQL query results directly to C# objects. You write your SQL queries, and Dapper fills your model classes with the raw data. Isn’t that nice? This way, you benefit from SQL’s power while also using C#’s type safety.
Now, let’s look at the setup and project structure. It’s not complicated at all, don’t worry. Experienced developers, like those in Bursa, know that a quick start is always good. For example, recently a friend asked me, ‘Brother, how do we set up Dapper, and what should the project structure look like?’ I said, ‘Let me show you, and we’ll reinforce it with an example code.’
First, you need to add Dapper to your project. NuGet Package Manager is perfect for this, right? Open your project in Visual Studio, right-click and choose ‘Manage NuGet Packages…’. Then, in the ‘Browse’ tab, search for ‘Dapper’. You will see many options, but select the official one at the top and click install. That’s it. The setup takes about 2 minutes, which I think is very important.
With installation complete, let’s see how to use it in your project. I usually do as follows: create a separate layer or folder for managing database connections, for example, called ‘DataAccess’. Inside this layer, there are classes that handle database operations, like ‘UserRepository’, ‘ProductRepository’. These repository classes execute SQL queries directly using Dapper.
Sometimes, while coding, you might wonder, ‘Am I doing this correctly?’ To dispel these doubts, here is an example. Suppose we want to fetch a list of users. First, let’s see a wrong approach, then the correct, clean method with Dapper.
In the past, when I first started using Dapper, I made a mistake—embedding connection string directly into the code. So, I kept the connection info in the code, which is not secure at all. Of course, I won’t show that old code, but to illustrate the concept, let’s imagine:
// WRONG APPROACH (Example – Actual Mistake in Code)
using System.Data.SqlClient; // Or Npgsql, MySql etc. using System.Collections.Generic; using System.Threading.Tasks;
public class WrongUserRepository { public async Task> GetAllUsersAsync() { var users = new List
See? The connection string is hardcoded, and each column is read manually, which is both time-consuming and error-prone. It’s like trying to draw a picture with a handmade brush and mixed paint—more effort, more room for mistakes. Isn’t that too cumbersome?
Now, let’s see how to do this more professionally and cleanly with Dapper. That’s where Dapper’s real power lies. You write your SQL query, and Dapper maps the results directly to your model class. Isn’t that great?
// CORRECT APPROACH (With Dapper)
using Dapper; // Indicates the use of Dapper using Microsoft.Extensions.Configuration; // For configuration management using System.Collections.Generic; using System.Data; // For IDbConnection using System.Threading.Tasks;
// Assuming the connection string is retrieved from Configuration public class UserRepository { private readonly IConfiguration _configuration; public UserRepository(IConfiguration configuration) { _configuration = configuration; } private IDbConnection GetConnection() { return new System.Data.SqlClient.SqlConnection(_configuration.GetConnectionString(“DefaultConnection”)); // You can replace SqlConnection with NpgsqlConnection for PostgreSQL, etc. } public async Task
// Our simple model class public class User { public int Id { get; set; } public string Name { get; set; } public string Email { get; set; } }
See how different it is? The connection string is now retrieved from Configuration, and manual mapping is eliminated. With `connection.QueryAsync
Note that Dapper does not only fetch data; it also supports Insert, Update, Delete operations via methods like `ExecuteAsync`. Basically, you can comfortably manage all CRUD operations with Dapper. If you don’t want to create separate Entity Framework models for each small table in a project, you can directly run your custom queries with Dapper, making your project lighter. In summary, Dapper is a fantastic choice for projects requiring speed and control. I highly recommend trying it; you won’t regret it. 🙂 Maybe in my next post, I will cover using stored procedures or transaction management with Dapper, what do you think?
If you want to learn more about Dapper, you can search for Google Dapper ORM for many resources. Also, the official documentation is available on GitHub at Dapper GitHub. From my experience, this library works great, especially where performance is critical. Truly, with just this simple setup, you get access to a very powerful tool.
Anyway, that’s all for today. I hope I’ve explained how useful Dapper is. I strongly recommend trying it in your projects. Good luck to everyone!