Dapper

What’s Dapper?

Dapper is a simple object mapper for .NET and owns the title of King of Micro ORM in terms of speed and is virtually as fast as using a raw ADO.NET data reader. An ORM is an Object Relational Mapper, which is responsible for mapping between database and programming language.

Dapper extends the IDbConnection by providing useful extension methods to query your database.

How Dapper Works?

It is a three-step process.

  • Create an IDbConnection object.
  • Write a query to perform CRUD operations.
  • Pass query as a parameter in the Execute method.

Installation

Dapper is installed through NuGet: https://www.nuget.org/packages/Dapper

  1. PM> Install-Package Dapper

Requirement

Dapper works with any database provider since there is no DB specific implementation.

Methods

Dapper will extend your IDbConnection interface with multiple methods:

  1. string sqlOrderDetails = "SELECT TOP 5 * FROM OrderDetails;";
  2. string sqlOrderDetail = "SELECT * FROM OrderDetails WHERE OrderDetailID = @OrderDetailID;";
  3. string sqlCustomerInsert = "INSERT INTO Customers (CustomerName) Values (@CustomerName);";
  4. using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
  5. {
  6. var orderDetails = connection.Query<OrderDetail>(sqlOrderDetails).ToList();
  7. var orderDetail = connection.QueryFirstOrDefault<OrderDetail>(sqlOrderDetail, new {OrderDetailID = 1});
  8. var affectedRows = connection.Execute(sqlCustomerInsert, new {CustomerName = "Mark"});
  9. Console.WriteLine(orderDetails.Count);
  10. Console.WriteLine(affectedRows);
  11. FiddleHelper.WriteTable(orderDetails);
  12. FiddleHelper.WriteTable(new List<OrderDetail>() { orderDetail });
  13. }

Try it: .NET Core | .NET Framework

Parameter

Execute and queries method can use parameters from multiple different ways:

  1. // Anonymous
  2. var affectedRows = connection.Execute(sql,
  3. new {Kind = InvoiceKind.WebInvoice, Code = "Single_Insert_1"},
  4. commandType: CommandType.StoredProcedure);
  5. // Dynamic
  6. DynamicParameters parameter = new DynamicParameters();
  7. parameter.Add("@Kind", InvoiceKind.WebInvoice, DbType.Int32, ParameterDirection.Input);
  8. parameter.Add("@Code", "Many_Insert_0", DbType.String, ParameterDirection.Input);
  9. parameter.Add("@RowCount", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue);
  10. connection.Execute(sql,
  11. parameter,
  12. commandType: CommandType.StoredProcedure);
  13. int rowCount = parameter.Get<int>("@RowCount");
  14. // List
  15. connection.Query<Invoice>(sql, new {Kind = new[] {InvoiceKind.StoreInvoice, InvoiceKind.WebInvoice}}).ToList();
  16. // String
  17. connection.Query<Invoice>(sql, new {Code = new DbString {Value = "Invoice_1", IsFixedLength = false, Length = 9, IsAnsi = true}}).ToList();

Result

The result returned by queries method can be mapped to multiple types:

  1. string sqlOrderDetails = "SELECT TOP 10 * FROM OrderDetails;";
  2. using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
  3. {
  4. var anonymousList = connection.Query(sqlOrderDetails).ToList();
  5. var orderDetails = connection.Query<OrderDetail>(sqlOrderDetails).ToList();
  6. Console.WriteLine(anonymousList.Count);
  7. Console.WriteLine(orderDetails.Count);
  8. FiddleHelper.WriteTable(orderDetails);
  9. FiddleHelper.WriteTable(connection.Query(sqlOrderDetails).FirstOrDefault());
  10. }

Try it: .NET Core | .NET Framework

Utilities

  1. // Async
  2. connection.QueryAsync<Invoice>(sql)
  3. // Buffered
  4. connection.Query<Invoice>(sql, buffered: false)
  5. // Transaction
  6. using (var transaction = connection.BeginTransaction())
  7. {
  8. var affectedRows = connection.Execute(sql,
  9. new {Kind = InvoiceKind.WebInvoice, Code = "Single_Insert_1"},
  10. commandType: CommandType.StoredProcedure,
  11. transaction: transaction);
  12. transaction.Commit();
  13. }
  14. // Stored Procedure
  15. var affectedRows = connection.Execute(sql,
  16. new {Kind = InvoiceKind.WebInvoice, Code = "Single_Insert_1"},
  17. commandType: CommandType.StoredProcedure);