Logo

dev-resources.site

for different kinds of informations.

Data Access with Dapper: A Lightweight ORM for .NET Apps

Published at
6/27/2024
Categories
dapper
csharp
dotnet
orm
Author
tapeshm
Categories
4 categories in total
dapper
open
csharp
open
dotnet
open
orm
open
Author
7 person written this
tapeshm
open
Data Access with Dapper: A Lightweight ORM for .NET Apps

Introduction

In this blog article, we'll cover how to efficiently access data using Dapper, a lightweight ORM for .NET applications. We'll discuss its key features and compare it with Entity Framework along the way.

Data access is a crucial aspect of any application. The selection of the best tool for the job will impact performance, maintainability, and development ease. Dapper is a lightweight Object Relational Mapper (ORM) for .NET that will compete with standard ORMs like Entity Framework. This article will introduce Dapper, compare it with Entity Framework, and demonstrate real-world examples with performance benchmarks.

For those interested in learning more about .NET development, check out our .NET Development blogs. Stay updated with the latest insights and best practices!

Introduction to Dapper

Dapper is a micro-ORM developed by the team at Stack Exchange. Unlike full-fledged ORMs like Entity Framework, Dapper focuses on being simple and performant. It does this by providing a straightforward API for executing SQL queries and mapping results to strongly-typed objects.

Key Features of Dapper

  1. Lightweight and Fast: Dapper is designed to be minimalistic and efficient, with minimal overhead.

  2. Simple API: The API is intuitive and easy to use, allowing developers to execute SQL queries directly and map results to objects.

  3. Flexibility: It allows you to write raw SQL queries, giving you full control over your database interactions.

  4. Extension Methods: Dapper extends the IDbConnection interface, making it easy to integrate with existing ADO.NET code.

To get started with Dapper, you need to install the Dapper package from NuGet. You can do this using the Package Manager Console:

Install-Package Dapper
Enter fullscreen mode Exit fullscreen mode

Comparing Dapper with Entity Framework

Entity Framework (EF) is a popular ORM for .NET that provides a high level of abstraction over database interactions. It uses a model-first or code-first approach to generate database schemas and manage data access.

Key Differences:

  1. Performance: Dapper is significantly faster than Entity Framework because it generates minimal overhead. EF, on the other hand, offers rich features but at the cost of performance.

  2. Complexity: EF provides a higher level of abstraction and includes features like change tracking, lazy loading, and navigation properties. Dapper is more lightweight and requires you to write SQL queries manually.

  3. Flexibility: Dapper offers more control and flexibility as it allows direct SQL execution. EF abstracts much of the SQL away, which can be a limitation in some scenarios.

  4. Learning Curve: EF has a steeper learning curve due to its rich feature set. Dapper is easier to learn and use for developers familiar with SQL.

Real-world Examples

Let's explore some real-world examples to see how Dapper can be used for common data access tasks.

Example 1: Basic CRUD Operations

First, we need to set up a database connection. Assume we have a Product table in our database.

CREATE TABLE Product (
    Id INT PRIMARY KEY IDENTITY,
    Name NVARCHAR(100),
    Price DECIMAL(18, 2)
);
Enter fullscreen mode Exit fullscreen mode

Now, let's perform basic CRUD operations using Dapper.

Create:

using System.Data.SqlClient;
using Dapper;

string connectionString = "your_connection_string";
using (var connection = new SqlConnection(connectionString))
{
    string insertQuery = "INSERT INTO Product (Name, Price) VALUES (@Name, @Price)";
    var result = connection.Execute(insertQuery, new { Name = "Laptop", Price = 999.99m });
    Console.WriteLine($"{result} row(s) inserted.");
}
Enter fullscreen mode Exit fullscreen mode

Read:

using (var connection = new SqlConnection(connectionString))
{
    string selectQuery = "SELECT * FROM Product WHERE Id = @Id";
    var product = connection.QuerySingleOrDefault<Product>(selectQuery, new { Id = 1 });
    Console.WriteLine($"Product: {product.Name}, Price: {product.Price}");
}
Enter fullscreen mode Exit fullscreen mode

Update:

using (var connection = new SqlConnection(connectionString))
{
    string updateQuery = "UPDATE Product SET Price = @Price WHERE Id = @Id";
    var result = connection.Execute(updateQuery, new { Id = 1, Price = 1099.99m });
    Console.WriteLine($"{result} row(s) updated.");
}
Enter fullscreen mode Exit fullscreen mode

Delete:

using (var connection = new SqlConnection(connectionString))
{
    string deleteQuery = "DELETE FROM Product WHERE Id = @Id";
    var result = connection.Execute(deleteQuery, new { Id = 1 });
    Console.WriteLine($"{result} row(s) deleted.");
}
Enter fullscreen mode Exit fullscreen mode

Example 2: Using Stored Procedures

Dapper also supports executing stored procedures.

CREATE PROCEDURE GetProductById
    @Id INT
AS
BEGIN
    SELECT * FROM Product WHERE Id = @Id
END
Enter fullscreen mode Exit fullscreen mode
using (var connection = new SqlConnection(connectionString))
{
    var product = connection.QuerySingleOrDefault<Product>(
        "GetProductById",
        new { Id = 1 },
        commandType: CommandType.StoredProcedure);
    Console.WriteLine($"Product: {product.Name}, Price: {product.Price}");
}
Enter fullscreen mode Exit fullscreen mode

Example 3: Mapping Complex Types

Dapper can map complex types and relationships.

CREATE TABLE Category (
    Id INT PRIMARY KEY IDENTITY,
    Name NVARCHAR(100)
);

ALTER TABLE Product ADD CategoryId INT;
ALTER TABLE Product ADD CONSTRAINT FK_Product_Category FOREIGN KEY (CategoryId) REFERENCES Category(Id);
Enter fullscreen mode Exit fullscreen mode
public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public Category Category { get; set; }
}

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }
}

using (var connection = new SqlConnection(connectionString))
{
    string sql = @"
        SELECT p.*, c.* 
        FROM Product p 
        INNER JOIN Category c ON p.CategoryId = c.Id
        WHERE p.Id = @Id";

    var product = connection.Query<Product, Category, Product>(
        sql,
        (product, category) => {
            product.Category = category;
            return product;
        },
        new { Id = 1 },
        splitOn: "Id").FirstOrDefault();

    Console.WriteLine($"Product: {product.Name}, Price: {product.Price}, Category: {product.Category.Name}");
}
Enter fullscreen mode Exit fullscreen mode

Performance Benchmarks

To illustrate the performance differences, let's compare Dapper and Entity Framework in terms of query execution time. Below are some benchmark results (in milliseconds) for retrieving 1000 records from a Product table.

The benchmark results show that Dapper performs significantly better than Entity Framework for this specific use case. While EF offers more features, Dapper's performance advantage can be crucial for high-load applications.

ORM Query Execution Time (ms)
Dapper 15
Entity Framework 45

The benchmark results show that Dapper performs significantly better than Entity Framework for this specific use case. While EF offers more features, Dapper's performance advantage can be crucial for high-load applications.

Conclusion

Dapper is an excellent choice for developers who need a lightweight, fast and flexible ORM for .NET applications. Its simplicity and performance make it suitable for many data access situations, whenever you want complete control over SQL queries. Though Entity Framework offers additional features, Dapper is more efficient and simpler to use compared to a lot of programs.

Adding Dapper to your .NET projects will help you optimize data access, improve performance, and maintain flexibility in your database interactions. Whether you are developing a small application or a large enterprise system, Dapper enables you to develop a maintainable data access code.

For those interested in learning more about .NET development, check out our .NET Development blogs. Stay updated with the latest insights and best practices!

orm Article's
30 articles in total
Favicon
Let's take a quick look at Drizzle ORM
Favicon
Prisma 101 baby.
Favicon
The Step-by-Step Process of Online Reputation Repair for Personal and Business Profiles
Favicon
The Best Strategies for Removing Negative News Articles from Search Engines
Favicon
Trysil - Multi-tenat API REST
Favicon
Typing Prisma Json Fields? Yes, You Can!
Favicon
What is ORM
Favicon
Top 5 ORM Services to Safeguard Your Online Reputation in 2024
Favicon
Gorm Pagination With Ease
Favicon
Introdução ao ORM do Django: Exercícios Práticos
Favicon
Explorando ORM: Facilitando o Desenvolvimento com Bancos de Dados
Favicon
Techniques for Synchronous DB Access in TypeScript
Favicon
Why We Adopted a Synchronous API for the New TypeScript ORM
Favicon
Data Access with Dapper: A Lightweight ORM for .NET Apps
Favicon
Writing code like this improves efficiency by 100 times compared to directly using MyBatis
Favicon
The Power of Less: Streamlining Dependencies with Remult
Favicon
Seeking a Type-Safe Ruby on Rails in TypeScript, I Started Developing an ORM
Favicon
TypeORM: O ORM que Você Precisa Conhecer para Trabalhar com Node.js e TypeScript
Favicon
SQL generation: Golang's builder pattern vs Clojure's persistent map
Favicon
How to Setting Default UUID for User Model in Peewee
Favicon
Introduction to "Accel Record": A TypeScript ORM Using the Active Record Pattern
Favicon
Difference Between ORM and ODM
Favicon
How to add update, and delete data in peewee Database Python
Favicon
Most popular Postgres ORMs
Favicon
Introducing Stalactite ORM
Favicon
Advanced Django ORM Features (Q-Objects, F-Expressions, Aggregations and Annotations)
Favicon
Less Code (-75%) & More Power (💪) with Remult
Favicon
A Software Engineer's Tips and Tricks #1: Drizzle
Favicon
Asgard saviour: Neurelo
Favicon
Prisma vs. Drizzle: A Comprehensive Guide for Your NextJS Project

Featured ones: