Dapper is a micro ORM product for Microsoft .NET Framework. It provides a set of actions for mapping POCO objects to Relational databases.with adequate performance.
Dapper is open-source and has Apache License 2.0 or the MIT License and is easily installable by NuGet.
Index
- Dapper features
- Database for the Test Project
- La clase DPGenericRepository
- Create DPGenericRepository object
- Methods descriptions
- All / AllAsync
- GetData(object parameters) / GetDataAsync
- GetData(string qry, object parameters) / GetDataAsync
- Find(object pks) / FindAsync
- Add(TEntity entity) / AddAsync
- Remove(object pk) / RemoveAsync
- Update(TEntity entity, object pk) / RemoveAsync
- InsertOrUpdate(TEntity entity, object pk) / RemoveAsync
Dapper features
Its main advantages as compared with other ORMs are given below.
- Performance is faster ORM in .NET.
- Few lines of code.
- Object Mapper.
- Choice of static/dynamic object binding.
- Easy handling of SQL query.
- Multiple query support.
- Support and easy handling of the stored procedures.
- Operating directly on IDBConnection class.
- Bulk data insert functionality.
Data from Wikipedia.
The biggest disadvantage of this wonderful ORM is the return to queries in strings because it is less useful and we lose the syntactic errors in compilation time.
I have created a Generic Repository based in Dapper, approaching its philosophy to Entity Framework, removing the string queries as far as possible, primarily for delete, update, insert, and for all method's queries.
I tried to create a custom library with the comfort of Entity Framework and with the performance of Dapper.
I have tested DPGenericRepository with Sql Server and Oracle, but it must be compatible with all databases that are Dapper supported.
I have other genericRepositories libraries of Entity Framework and EntityFramework+Dapper, and if my time allows, I will explain it in other articles. These libraries are compatible with each, and they will be easily replaced.
This project is open source and it is available in GitHub.
We can install through NuGet,

Database for the Test Project
Proceed, as shown below.

It has two tables given below.


DPGenericRepository Class
DPGenericRepository is a principal class of MoralesLarios.Data.Dapper namespace. It has the functionality to create and transform our classes in GenericRepositories for Dapper.
The next image shows the principal class for this article DPGenericRepository and then implements the interfaces. We can see the other generics repositories EFGenericRepository and MLGenericRepository, which will be visible in the deliveries given below with your compatibilities.

IGenericRepository Interface
- public interface IGenericRepository<TEntity> : IDisposable where TEntity : class
- {
- IEnumerable<TEntity> All();
- Task<IEnumerable<TEntity>> AllAsync();
- IEnumerable<TEntity> GetData(string qry, object parameters);
- Task<IEnumerable<TEntity>> GetDataAsync(string qry, object parameters);
- TEntity Find(object pksFields);
- Task<TEntity> FindAsync(object pksFields);
- int Add(TEntity entity);
- Task<int> AddAsync(TEntity entity);
- int Add(IEnumerable<TEntity> entities);
- Task<int> AddAsync(IEnumerable<TEntity> entities);
- void Remove(object key);
- Task RemoveAsync(object key);
- int Update(TEntity entity, object pks);
- Task<int> UpdateAsync(TEntity entity, object pks);
- int InstertOrUpdate(TEntity entity, object pks);
- Task<int> InstertOrUpdateAsync(TEntity entity, object pks);
- }
IDPGenericRepository Interface
- public interface IDPGenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class
- {
- IEnumerable<TEntity> GetData(object filter);
- Task<IEnumerable<TEntity>> GetDataAsync(object filter);
- }
DPGenericRepository implements the IDPGenericRepository, which in turn implements IGenericRepository for compatibility with other Generic Repositories of the library.
DPGenericRepository contains a set of the regular use of methods in the database, but we can extend your functionality through inheritance.
Creating a DPGenericRepository Object
Dapper is based on an extension class for the IDbConnection interface, therefore DPGenericRepository needs an object IDbConnection injected in your constructor.
We can create a DPGenericRepository in two different ways, which are given below.
Directly in code
- using (var conn = new SqlConnection(cs))
- {
- var departmentRepository = new DPGenericRepository<Departments>(conn);
- // inject IDbConnection
- }
We can use a second constructor with a new char parameter. This new parameter ‘parameterIdentified’ shows the char SQL parameter indicator. ‘@’ for default, gives compatibility with SQL Server, for Oracle we use ‘:’.
Example for Oracle
- using (var conn = new SqlConnection(cs))
- {
- var departmentRepository = new DPGenericRepository<Departments>(conn, parameterIdentified:':'
- );
- // inject IDbConnection
- }
For Inheritance


Supun SilvaPosted Sep 1, 2022, 7:23 AM
Can we use another table name by specifying the Table attribute [Table("tbl_Author")] and also for column names? Please refer to Dapper Contrib data annotation
Al JuniorPosted Aug 14, 2021, 6:16 PM
Hi, thank you for this post:can your implementation handle getting nested objects?
vinay khannaPosted Jul 21, 2021, 3:45 AM
I need the source of the generic repository
Juan Francisco Morales LariosPosted May 25, 2017, 5:10 AM
Hi parminder, my base class doesn't covered store procedure, but you extend your Dapper repository with your methods with stores procedures call. I put you the information of dapper, find sotre procedure section. https://github.com/StackExchange/Dapper
Parminder Pal SinghPosted May 25, 2017, 2:39 AM
I need to call the stored procedure and get output from it, how can i do with dapper ?