Skip to content

Dapper Extensions is a small library that complements Dapper by adding basic CRUD operations (Get, Insert, Update, Delete) for your POCOs. For more advanced querying scenarios, Dapper Extensions provides a predicate system. The goal of this library is to keep your POCOs pure by not requiring any attributes or base class inheritance.

Notifications You must be signed in to change notification settings

tmsmith/Dapper-Extensions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Repository files navigation

NuGet VersionNuGet Download CountGitHub issuesGitHub pull requests

Introduction

Dapper Extensions is a small library that complements Dapper by adding basic CRUD operations (Get, Insert, Update, Delete) for your POCOs. For more advanced querying scenarios, Dapper Extensions provides a predicate system. The goal of this library is to keep your POCOs pure by not requiring any attributes or base class inheritance.

Customized mappings are achieved through ClassMapper.

Important: This library is a separate effort from Dapper.Contrib (a sub-system of the Dapper project). Important: Check our projects list to see the plans for the next releases.

Features

  • Zero configuration out of the box.
  • Automatic mapping of POCOs for Get, Insert, Update, and Delete operations.
  • GetList, Count methods for more advanced scenarios.
  • GetPage for returning paged result sets.
  • Automatic support for Guid and Integer primary keys (Includes manual support for other key types).
  • Pure POCOs through use of ClassMapper (Attribute Free!).
  • Customized entity-table mapping through the use of ClassMapper.
  • Composite Primary Key support.
  • Singular and Pluralized table name support (Singular by default).
  • Easy-to-use Predicate System for more advanced scenarios.
  • Properly escapes table/column names in generated SQL (Ex: SELECT [FirstName] FROM [Users] WHERE [Users].[UserId] = @UserId_0)
  • Unit test coverage (150+ Unit Tests)

Naming Conventions

  • POCO names should match the table name in the database. Pluralized table names are supported through the PlurizedAutoClassMapper.
  • POCO property names should match each column name in the table.
  • By convention, the primary key should be named Id. Using another name is supported through custom mappings.

Installation

For more information, please view our Getting Started guide.

http://nuget.org/List/Packages/DapperExtensions

PM> Install-Package DapperExtensions 

Examples

The following examples will use a Person POCO defined as:

publicclassPerson{publicintId{get;set;}publicstringFirstName{get;set;}publicstringLastName{get;set;}publicboolActive{get;set;}publicDateTimeDateCreated{get;set;}}

Get Operation

using(SqlConnectioncn=newSqlConnection(_connectionString)){cn.Open();intpersonId=1;Personperson=cn.Get<Person>(personId);cn.Close();}

Simple Insert Operation

using(SqlConnectioncn=newSqlConnection(_connectionString)){cn.Open();Personperson=newPerson{FirstName="Foo",LastName="Bar"};intid=cn.Insert(person);cn.Close();}

Advanced Insert Operation (Composite Key)

publicclassCar{publicintModelId{get;set;}publicintYear{get;set;}publicstringColor{get;set;}} ...using(SqlConnectioncn=newSqlConnection(_connectionString)){cn.Open();Carcar=newCar{Color="Red"};varmultiKey=cn.Insert(car);cn.Close();intmodelId=multiKey.ModelId;intyear=multiKey.Year;}

Simple Update Operation

using(SqlConnectioncn=newSqlConnection(_connectionString)){cn.Open();intpersonId=1;Personperson=cn.Get<Person>(personId);person.LastName="Baz";cn.Update(person);cn.Close();}

Simple Delete Operation

using(SqlConnectioncn=newSqlConnection(_connectionString)){cn.Open();Personperson=cn.Get<Person>(1);cn.Delete(person);cn.Close();}

GetList Operation (with Predicates)

using(SqlConnectioncn=newSqlConnection(_connectionString)){cn.Open();varpredicate=Predicates.Field<Person>(f =>f.Active,Operator.Eq,true);IEnumerable<Person>list=cn.GetList<Person>(predicate);cn.Close();}

Generated SQL

SELECT [Person].[Id] , [Person].[FirstName] , [Person].[LastName] , [Person].[Active] , [Person].[DateCreated] FROM [Person] WHERE ([Person].[Active] = @Active_0) 

More information on predicates can be found in our wiki.

Count Operation (with Predicates)

using(SqlConnectioncn=newSqlConnection(_connectionString)){cn.Open();varpredicate=Predicates.Field<Person>(f =>f.DateCreated,Operator.Lt,DateTime.UtcNow.AddDays(-5));intcount=cn.Count<Person>(predicate);cn.Close();}

Generated SQL

SELECT COUNT(*) Total FROM [Person] WHERE ([Person].[DateCreated] < @DateCreated_0) 

More information on predicates can be found in our wiki.

License

Copyright 2011 Thad Smith, Page Brooks and contributors

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

About

Dapper Extensions is a small library that complements Dapper by adding basic CRUD operations (Get, Insert, Update, Delete) for your POCOs. For more advanced querying scenarios, Dapper Extensions provides a predicate system. The goal of this library is to keep your POCOs pure by not requiring any attributes or base class inheritance.

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 33

Languages