Dapper is a micro ORM product for Microsoft .NET Framework. It provides a set of actions for mapping POCO objects to Relational Database.
It was developed by StackExchange team for your web sites (Stack Overflow, Mathematics, etc) because Linq To Sql did not provide them adequate performance.
Dapper is open source and has Apache License 2.0 or the MIT License and is easily installable by Nuget:
- Dapper features
- Database for the Test Project
- La clase DPGenericRepository
- Creando un objeto DPGenericRepository
- Descripción de métodos
- 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
- Its main advantages as compared with other ORMs are:
- Performance, it’s a faster ORM in .NET.
- Fewer lines of code.
- Object mapper.
- Choice of static/dynamic object binding.
- Easy handling of SQL query.
- Multiple query support.
- Support and easy handling of 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 is less useful and we lost 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 far as possible, primarily for delete, update, insert and for All methods queries.
I tried to create a custom library with comfort of Entity Framework and with performance of Dapper.
I have tested DPGenericRepository with Sql Server and Oracle, but it must compatible with all databases Dapper supported.
This project is open source and it is available in Git Hub.
We can to install through Nuget:
Database for the Test Project
The DataBase Tests is inside of project and must run automatically in the test project:
It has two tables:
DPGenericRepository is a principal class of MoralesLarios.Data.Dapper namespace. It has a functionality for create and transform our classes in GenericRepositories for Dapper.
The next image shows the principal class for this article DPGenericRepository and the implements interfaces. We can see the other generics repositories EFGenericRepository and MLGenericRepository that we will see in the following deliveries with your compatibilities.
DPGenericRepository implements IDPGenericRepository which, in turn implments IGenericRepository for compatibility with others Generics Repositories of the library.
DPGenericRepository contains a set of regular use methods in database, but we can extend your functionality through inherits.
Creating a DPGenericRepository Object
Dapper is based in an extension class for the IDbConnection interface, therefore DPGenericRepository need an object IDbConnection injected in your constructor.
We can create a DPGenericRepository of two different ways:
1.- Directly in code:
We can use a second constructor with a new char parameter, this new parameter ‘parameterIdentified’ show the char SQL parameter indicator. ‘@’ for default, give compatibility with Sql Server, for Oracle we use ‘:’.
Example for Oracle:
2.- For Inheritance:
Has happend in the first way, change de default value parameterIdentified to ‘:’ for Oracle:
Let’s try to explain all DPGenericRepository methods, with an example for each one.
It’s important to point that any methods take an object parameter with ‘pk’ or ‘parameters’ definitions. These methods create with an anonymous type with an alias type equals to relational type in the POCO object.
Let’s see your methods:
All / AllAsync
The All method obtains all data of the table:
GetData(object parameters) / GetDataAsync
GetData method with one parameter built a query for Dapper with an equals sequences of ‘ands’ for parameter value.
GetData(string qry, object parameters) / GetDataAsync
GetData method with two parameters, is a method with less automation, because it isn’t possible infer data and your execution is practically equal with a normal query dapper execute.
Find(object pks) / FindAsync
Find method is very similar to GetData(object paramaters). This signature exists for down compatibility with Entity Framework GenericRepository, in which it has sense for its architecture. The parameter pks, would be the pks properties in the table ordered.
Add(TEntity entity) / AddAsync
Add an entity in Database.
Add(IEnumerable<TEntity> entities) / AddAsync
Insert a set of entities in Database. For the Dapper versatility, this insertion is carried through bulk copy. Is a very faster process.
Remove(object pk) / RemoveAsync
Remove row of database from pk.
Update(TEntity entity, object pk) / RemoveAsync
Update the row in database from pk.
InsertOrUpdate(TEntity entity, object pk) / RemoveAsync
This method checks if entity exists in database through its pk. If row exists, update its value and if it doesn’t exist insert the entitiy in database.
It is practical with a tipical screen for insert/update values, because we can reuse the code.
You can test all methods y the Test Project.