Dapper Micro-ORM


What is Dapper?

Dapper = Lightweight, fast micro-ORM for .NET by Stack Overflow team

Key Features:

  • Blazing fast - Nearly as fast as raw ADO.NET

  • Simple API - Easy to learn and use

  • Lightweight - ~1500 lines of code

  • No configuration - No mappings, no context

  • Full control - Write your own SQL

  • Multi-database - Works with any ADO.NET provider

Dapper vs Entity Framework Core

Feature
Dapper
EF Core

Speed

⚡⚡⚡ Near ADO.NET

⚡⚡ Slower

Learning Curve

✅ Easy

⚠️ Moderate

SQL Control

✅ Full control

⚠️ Generated SQL

Migrations

❌ Manual

✅ Built-in

Change Tracking

❌ No

✅ Yes

Lazy Loading

❌ No

✅ Yes

Complex Queries

✅ Write SQL

⚠️ LINQ translation

Best For

Performance-critical, simple CRUD

Complex domains, rapid development

When to Use Dapper

✅ Use Dapper when:

  • Performance is critical

  • Simple data access patterns

  • You prefer writing SQL

  • Legacy database with complex schema

  • Microservices with focused data access

  • Reports and analytics

  • Hybrid approach (Dapper + EF Core)

❌ Use EF Core when:

  • Complex domain models

  • Need change tracking

  • Want migrations

  • Rapid prototyping

  • Learning curve is acceptable


Installation


Basic Setup

Connection String Configuration

Creating a Connection


Basic CRUD Operations

Query - Return Multiple Rows

QueryFirst / QueryFirstOrDefault

QuerySingle / QuerySingleOrDefault

Execute - INSERT, UPDATE, DELETE

ExecuteScalar - Return Single Value


Working with Parameters

Anonymous Objects

DynamicParameters

IN Clause with Lists


Advanced Queries

Multi-Mapping (JOIN queries)

Multi-Mapping (Better Approach)

Three-Way Mapping

Multiple Result Sets


Stored Procedures

Calling Stored Procedures

Example Stored Procedures


Transactions

Basic Transaction

Transaction with Using Statement

TransactionScope (Distributed Transactions)


Async Operations

Async CRUD

Async with Multiple Results


Bulk Operations

Bulk Insert

Bulk Update

Bulk Delete


Dynamic Queries

Dynamic Results

Building Dynamic SQL


Repository Pattern with Dapper

Interface

Implementation

Registration and Usage


Unit of Work Pattern

Interface

Implementation


Performance Optimization

Query Performance

Buffered vs Unbuffered Queries

Command Timeout


Best Practices

1. Always Use Parameters (Prevent SQL Injection)

2. Use Async Methods

3. Dispose Connections Properly

4. Use Const for SQL Strings

5. Handle NULLs Properly

6. Use Transactions for Multiple Operations

7. Avoid Over-Fetching Data

8. Use Stored Procedures for Complex Logic


Common Patterns

Pagination

Soft Delete

Audit Trail


Performance Comparison

Dapper vs EF Core Benchmark


Dapper Extensions

Dapper.Contrib

Dapper.SimpleCRUD


Quick Reference

Common Dapper Methods

Method
Returns
Use Case

Query<T>

IEnumerable<T>

Multiple rows

QueryFirst<T>

T

First row (throws if empty)

QueryFirstOrDefault<T>

T?

First row or null

QuerySingle<T>

T

Exactly one row

QuerySingleOrDefault<T>

T?

0 or 1 row

Execute

int

INSERT/UPDATE/DELETE

ExecuteScalar<T>

T

Single value (COUNT, MAX, etc.)

QueryMultiple

GridReader

Multiple result sets

Async Versions

All methods have async versions with Async suffix:

  • QueryAsync<T>

  • QueryFirstOrDefaultAsync<T>

  • ExecuteAsync

  • ExecuteScalarAsync<T>

  • QueryMultipleAsync


Decision Tree: Dapper vs EF Core


Guide Complete! This comprehensive Dapper guide covers setup, CRUD operations, stored procedures, transactions, async patterns, bulk operations, repository pattern, performance optimization, and when to use Dapper vs EF Core. Use Dapper when performance matters and you want full control over your SQL! ⚡

Last updated