Dapper - mcbride-clint/DeveloperCurriculum GitHub Wiki
Dapper is a 3rd party library to aid in executing queries against a database.
It's core functionality is to map query results to .Net objects.
It is built to be used as a set of extension methods of an IDbConnection
.
This allows it to be Database agnostic excluding Database specific functionality, covering the 95% of use cases.
Dapper is built to be as thin and performant as possible.
This allows it to be only a few microseconds slower than using a straight DbConnection
and storing it in a DataTable
.
There are many useful methods included in Dapper and they all have a distinct purpose but the most common ones are:
-
Query<T>
- Return an IEnumerable of -
Execute
- Execute a Query that does not return a result -
GetExecuteScalar<T>
- Return a single scalar value of
Because Dapper works off of the IDbConnection
Interface, any Database object that implements IDbConnection
is compatible, such as SqlConnection
or OracleConnection
.
Once you have one of these connections, Dapper greatly simplifies the usage.
string connectionString =
"Data Source=(local);Initial Catalog=Northwind;"
+ "Integrated Security=true";
string queryString =
"SELECT ProductID, UnitPrice, ProductName from dbo.products "
+ "WHERE UnitPrice > @pricePoint "
+ "ORDER BY UnitPrice DESC;";
int paramValue = 5;
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
command.Parameters.AddWithValue("@pricePoint", paramValue);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine("\t{0}\t{1}\t{2}",
reader[0], reader[1], reader[2]);
}
reader.Close();
}
Once you create a class with properties that will represent your results, Dapper will map the result automatically as well as handling the opening and closing of the IDbConnection
object.
Dapper will also handle most mapping of parameters, inferring the type, and binding by name.
Mapping to the Product
class is accomplished by a case-insensitive name match or via a configurable mapping class.
public class Product
{
public string ProductID {get;set;}
public decimal UnitPrice{get;set;}
public string ProductName {get;set;}
}
...
string connectionString =
"Data Source=(local);Initial Catalog=Northwind;"
+ "Integrated Security=true";
string queryString =
"SELECT ProductID, UnitPrice, ProductName from dbo.products "
+ "WHERE UnitPrice > @pricePoint "
+ "ORDER BY UnitPrice DESC;";
int paramValue = 5;
using (SqlConnection connection = new SqlConnection(connectionString))
{
var products = connection.Query<Product>(queryString, new {pricePoint = paramValue});
foreach(var product in products)
{
Console.WriteLine("\t{0}\t{1}\t{2}",
product.ProductID, product.UnitPrice, product.ProductName);
}
}
- Dapper Github - https://github.com/DapperLib/Dapper
- Dapper Tutorial Site - https://dapper-tutorial.net/
- Pluralsight - Dapper: Getting Started - https://app.pluralsight.com/library/courses/getting-started-dapper/table-of-contents