Understanding what Dapper Is and Its Benefits
Learn about Dapper and its installation.
We'll cover the following
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
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:
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.
On the “Browse” tab, enter “Dapper” into the search box.
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.