Fasten your Data Layer with Dapper (A .NET Micro ORM)

Jawadhasan
6 min readJan 29, 2021

Introduction

Dapper is .NET based light-weight, fast and simple to use Micro ORM created by brilliant Stack Exchange team. The word ORM means Object Relation Mapper, means it facilitates mapping b/w .NET objects and database and that is its focus.

One great thing about Dapper that it works with any database. So, its not just for SQL Server. You can use it with PostgreSQL, MySQL or others.

You can download the code for sample application from this GitHub repo.

Simplified API

The simple API from Dapper, make it very easy to use. Dapper provide three primary helper methods:

  • A Query method that maps to strongly typed objects.
  • A Query method that maps to dynamic objects.
  • An Execute method for commands that do not return results (e.g. Insert)

We will see these functionalities in action by creating a .NET Core Application.

Create .NET Core Console Application

I started by creating a .NET Core Console application using visual studio:

Install Nuget Packages

First we install dapper main package to our project.

As mentioned above, you can use dapper with variety of databases and in order to work with a database you need to install corresponding nuget package.

I will be using Postgres and another package we need to install is Npgsql:

Other database providers

We’ll be not using those but if you want to use dapper with SqlServer you can install the following package:

System.Data.SqlClient

For MySQL install the following package:

MySql.Data

Database Setup

I have Postgres database is setup with a users table. I will be using this for demos in post. You can refer to my earlier post Fun with SQL using Postgres and Azure Data Studio for more information.

--

--

Jawadhasan

Software Solutions Team Lead | Cloud Architect | Solutions Architect