Insights

Using Dapper ORM

Sitecore Developer
Valtech

April 11, 2019

Setting up Dapper in Sitecore

In one of our projects, I encountered a situation in which every developer on the project added extra lines of code to the project to send and retrieve data to and from database. Then I realized that we should have had an ORM (Object Relational Mapper) to do that for us. I investigated the existing ORMs in the market, and came up with a list of them for .Net platform, like nHibernet, Entity Framework, Dapper, etc. However, some of them were heavy duty like Entity Framework, and didn’t fit to our solution. Therefore, I came up with a sample usage of Dapper, and got approval from the SA of the project to use it. 

The sample was so light and straightforward. It basically was contained of a class with 3 generic functions called “QueryToObject”, “QueryToList” and “ExecuteStatement”, each responsible for a type of data retrieval/execution. So the developers could send a sql statement and a list of parameters to them, and let it do the rest. 

It was fast and handy, and was a valid solution to a problem that every developer was facing. 

The following is an example of how to use Dapper: 

using (IDbConnection db = new SqlConnection(connectionString))
                         {
                                db.Execute(sqlStatement, parameters);
                                 result = true;
                          }

Meet The Challenges Of Today's Digital Economy

Ready to take that first step and rise to your digital potential? Contact Valtech today.
Talk to Us