Scripts2026年5月23日·1 分钟阅读

Dapper — High-Performance Micro ORM for .NET

A lightweight object mapper for .NET that extends IDbConnection with fast methods for querying and executing SQL against any database.

Agent 就绪

这个资产可以被 Agent 直接读取和安装

TokRepo 同时提供通用 CLI 命令、安装契约、metadata JSON、按适配器生成的安装计划和原始内容链接,方便 Agent 判断适配度、风险和下一步动作。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Dapper Overview
通用 CLI 安装命令
npx tokrepo install 26337977-56c4-11f1-9bc6-00163e2b0d79

Introduction

Dapper is a lightweight object mapper for .NET created by the Stack Overflow engineering team. It extends IDbConnection with convenience methods that map query results directly to strongly typed objects while delivering near-raw ADO.NET performance.

What Dapper Does

  • Maps SQL query results to C# objects with minimal overhead
  • Supports parameterized queries to prevent SQL injection
  • Handles multi-mapping for complex joins across multiple tables
  • Provides async variants of all query and execute methods
  • Works with any database that has an ADO.NET provider

Architecture Overview

Dapper operates as a set of extension methods on IDbConnection. It uses IL emission and caching to generate optimized mapping functions at runtime, avoiding the reflection overhead typical of full ORMs. There is no change tracking, no identity map, and no query generation. You write the SQL, Dapper maps the results.

Self-Hosting & Configuration

  • Install via NuGet: dotnet add package Dapper
  • No configuration files or startup registration required
  • Works with SqlConnection, NpgsqlConnection, MySqlConnection, and any IDbConnection
  • Add Dapper.Contrib for CRUD helpers or Dapper.SqlBuilder for dynamic queries
  • Compatible with .NET Framework 4.6.1+ and .NET 6 through .NET 9

Key Features

  • Near-raw ADO.NET speed with object mapping convenience
  • Multi-mapping to split a single row into multiple objects
  • Stored procedure support with output parameters
  • Buffered and unbuffered result streaming
  • Transaction support through standard IDbTransaction

Comparison with Similar Tools

  • Entity Framework Core — full ORM with change tracking and migrations; heavier overhead, more abstraction
  • ADO.NET (raw) — no mapping layer; fastest but verbose and error-prone
  • RepoDB — hybrid ORM with micro and full ORM features; smaller community
  • ServiceStack.OrmLite — convention-based mapping; commercial license required
  • PetaPoco — similar micro ORM approach; less active maintenance

FAQ

Q: When should I choose Dapper over Entity Framework Core? A: Use Dapper when you need maximum query performance, prefer writing your own SQL, or work with existing stored procedures. EF Core suits projects that benefit from migrations, change tracking, and LINQ query building.

Q: Does Dapper support database migrations? A: No. Dapper is purely a query mapper. Use a dedicated migration tool like FluentMigrator or EF Core migrations alongside it.

Q: Can Dapper map one-to-many relationships? A: Yes, using multi-mapping and QueryMultiple APIs. You split joined rows into parent and child objects, then assemble them in application code.

Q: Is Dapper safe from SQL injection? A: Yes, as long as you use parameterized queries. Dapper passes parameters to the underlying ADO.NET provider, which handles safe escaping.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产