Data Access with Dapper
Dapper keeps the SQL you write but removes most of the ADO.NET code around it. You will query, insert, update and delete with Dapper and map results straight into your models.
Updated
Dapper
- Why Dapper (Journeyman): Dapper is a small library that keeps your SQL but writes the ADO.NET code around it for you - opening the connection, adding parameters, and turning rows into objects.
- Query and QuerySingle (Journeyman): Query<T> reads many rows into objects; QuerySingle, QuerySingleOrDefault, QueryFirst and QueryFirstOrDefault read one; ExecuteScalar<T> reads one value. Pick by how many rows you expect.
- Execute: Insert, Update and Delete (Journeyman): Execute runs INSERT, UPDATE and DELETE and returns how many rows changed. To get a new row's id back, query it with QuerySingle<int>.
- Parameters and Mapping to Models (Journeyman): Pass parameters as an anonymous object or a model, with property names that match the @ names; lists become IN lists. Columns map to properties by name, so alias any column that doesn't match.
Exercise: Rewrite a DAO with Dapper
- Exercise: Rewrite a DAO with Dapper (Journeyman): Use Dapper to rewrite the BooksPlus DAOs from the Connecting an App exercise, add a rename and a lookup by a list of ids, and pass the same tests with a fraction of the code.