Dapper .NET Data Access APIs
Dapper is a popular, high-performance Object Relational Mapper (ORM) for .NET. It allows you to map query results directly to your Plain Old CLR Objects (POCOs) with minimal overhead. Dapper is known for its speed and simplicity, often outperforming other ORMs in benchmarks.
What is Dapper?
Dapper, often referred to as "the fastest micro-ORM," focuses on providing an efficient way to execute SQL queries and map the results to .NET objects. It achieves this by leveraging dynamic methods and generics, minimizing reflection and intermediate object creation.
Key Features
- High Performance: Dapper is designed for speed, making it an excellent choice for performance-critical applications.
- Simplicity: Its API is straightforward and easy to learn, requiring minimal boilerplate code.
- Flexibility: You write your SQL, giving you full control over your queries. Dapper handles the mapping.
- Extensibility: Supports custom type handlers for complex mappings.
- Type Safety: Works seamlessly with strongly-typed .NET objects.
Getting Started with Dapper
To use Dapper, you first need to install it via NuGet. You'll also need a connection to your database. The core Dapper extension methods are defined on the IDbConnection
interface.
Prerequisites
- A .NET project (e.g., .NET Core, .NET Framework).
- A database connection string.
- The Dapper NuGet package.
Core Concepts
Dapper's main purpose is to simplify the interaction between your .NET code and your database. The primary operations involve:
- Executing SQL queries and returning multiple results (lists of objects).
- Executing SQL queries and returning a single result (a single object or scalar value).
- Executing SQL commands that do not return results (INSERT, UPDATE, DELETE).
Example Usage
Here's a simple example of querying data using Dapper:
using System.Data;
using System.Data.SqlClient; // Or your specific database provider
using Dapper;
using System.Collections.Generic;
// Assume 'connectionString' is your database connection string
string connectionString = "Server=myServer;Database=myDatabase;Integrated Security=True;";
public class Product
{
public int ProductId { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
public class ProductRepository
{
public IEnumerable<Product> GetAllProducts()
{
using (IDbConnection db = new SqlConnection(connectionString))
{
string sql = "SELECT ProductId, Name, Price FROM Products";
return db.Query<Product>(sql);
}
}
}
Key Dapper Extension Methods
Query<T>(string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null)
Executes a query and returns an enumeration of typed results. This is the most common method for retrieving lists of data.
QueryFirstOrDefault<T>(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
Executes a query and returns the first element of the result set, or a default value if the result set is empty.
QuerySingle<T>(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
Executes a query and returns the first element of the result set. Throws an exception if the result set is empty or contains more than one element.
Execute(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
Executes a command against the database and returns the number of rows affected.
Learn More
Explore the sections below to dive deeper into Dapper's capabilities, including installation, querying, executing commands, transactions, and advanced features.