Understanding what Dapper Is and Its Benefits

Learn about Dapper and its installation.

Dapper

Dapper is a performance-focused simple object mapper for .NET that helps map SQL query output to instances of a C# class. It is built and maintained by the Stack Overflow team, has been released as open source, and is a popular alternative to Microsoft’s Entity Framework.

So, why use Dapper rather than Entity FrameworkEntity Framework (EF) serves as an object-relational mapper, empowering .NET developers to interact with relational data through domain-specific objects. This streamlines the process by significantly reducing the necessity for developers to manually create data-access code.? The goal of Entity Framework is to abstract away the database, so it trades learning SQL for Entity Framework-specific objects such as DBSet and DataContext. We generally don’t write SQL with Entity Framework – instead, we write LINQLanguage-Integrated Query (LINQ) represents a collection of technologies that seamlessly incorporate query functionalities directly into the C# language. Historically, data queries were formulated as basic strings devoid of compile-time type validation or IntelliSense assistance. Additionally, mastering distinct query languages for varying data sources like SQL databases, XML files, and diverse web services was necessary. LINQ revolutionizes this by elevating queries to a fundamental language element, akin to classes, methods, and events. queries, which are translated into SQL by Entity Framework.

If we are implementing a large database that serves a large number of users, Entity Framework can be a challenge because the queries it generates can be inefficient. We need to understand Entity Framework well to make it scale, which can be a significant investment. When we find Entity Framework queries that are slow, we need to understand SQL to properly understand the root cause. So, it makes sense to invest time in learning SQL really well rather than the abstraction that the Entity Framework provides. Also, if we have a team with good database and SQL skills, it doesn’t make sense to not use these.

Dapper is much simpler than Entity Framework. Later in this section, we’ll see that we can read and write data from a SQL database with just a few lines of C# code. This allows us to interact with stored procedures in the database, thus automatically mapping C# class instances to SQL parameters, along with the results of the query. In the next section, we will install and start using Dapper to access our data.

Installing and configuring Dapper

In this section, we are going to install and configure Dapper. We will also install the Microsoft SQL Server client package that Dapper uses. Let’s carry out the following steps:

  1. Let’s open the backend project in Visual Studio. Go to the “Tools” menu and then “NuGet Package Manager” and choose “Manage NuGet Packages for Solution…”

Note: NuGet is a tool that downloads third-party and Microsoft libraries and manages the references to them so that the libraries can easily be updated.

  1. On the “Browse” tab, enter “Dapper” into the search box.

  2. Select the “Dapper” package by Sam Saffron, Marc Gravell, and Nick Craver. Tick our project and click the “Install” button with the latest stable version selected. Refer to the following screenshot:

Get hands-on with 1400+ tech skills courses.