In the past, we have ADO.NET that use data reader and dataset that provides awesome performance in .NET era. Some of our customer uses ORM like XPO or Core Entity Framework that works just like ORM. f you're looking for a lightweight and high-performance ORM (Object-Relational Mapper) for your ASP.NET applications, Dapper is a fantastic choice. Known for its speed and simplicity, Dapper allows you to execute SQL queries and map the results to strongly typed objects with minimal overhead. In this blog post, we'll walk you through the steps to get started with Dapper in your ASP.NET project.
What is Dapper?
Dapper is a micro-ORM developed by the team at Stack Exchange. Unlike full-fledged ORMs like Entity Framework, Dapper focuses on providing a simple and efficient way to execute SQL queries and map the results to .NET objects. It's often regarded as one of the fastest ORMs available for .NET
Setting Up Your Project
-
Create a New ASP.NET Project: Start by creating a new ASP.NET project in Visual Studio. You can choose either ASP.NET Core or ASP.NET MVC, depending on your preference.
-
Install Dapper: You can install Dapper via NuGet Package Manager. Open the NuGet Package Manager Console and run the following command:
-
Configure Your Database Connection: In your appsettings.json
file, add your database connection string:
json
{"ConnectionStrings":{"DefaultConnection":"YourConnectionStringHere"}}
-
Create a Database Context: Create a class to manage your database connection. This class will use IDbConnection
from System.Data
to establish a connection to your database.
csharp
usingSystem.Data; usingSystem.Data.SqlClient; usingMicrosoft.Extensions.Configuration; publicclassDapperContext{ privatereadonlyIConfiguration _configuration; privatereadonlystring_connectionString; public DapperContext(IConfiguration configuration){ _configuration = configuration; _connectionString = _configuration.GetConnectionString("DefaultConnection"); } public IDbConnection CreateConnection()=> newSqlConnection(_connectionString); }
Performing CRUD Operations
Now that your project is set up, let's look at how to perform basic CRUD (Create, Read, Update, Delete) operations using Dapper.
-
Create:
csharp
public async Task<int> AddUser(User user){ varsql = "INSERT INTO Users (Name, Email) VALUES (@Name, @Email)"; using(varconnection = _context.CreateConnection()) { returnawaitconnection.ExecuteAsync(sql, new{ user.Name, user.Email }); } }
-
Read:
csharp
publicasyncTask<IEnumerable<User>> GetUsers() { varsql = "SELECT * FROM Users"; using(varconnection = _context.CreateConnection()) { returnawaitconnection.QueryAsync<User>(sql); } }
-
Update:
csharp
public async Task<int> UpdateUser(User user){ varsql = "UPDATE Users SET Name = @Name, Email = @Email WHERE Id = @Id"; using(varconnection = _context.CreateConnection()) { returnawaitconnection.ExecuteAsync(sql, new{ user.Name, user.Email, user.Id }); } }
-
Delete:
csharp
public async Task<int> DeleteUser(int id){ varsql = "DELETE FROM Users WHERE Id = @Id"; using(varconnection = _context.CreateConnection()) { returnawaitconnection.ExecuteAsync(sql, new{ Id = id }); } }
Conclusion
Dapper is a powerful tool for developers who need a fast and efficient way to interact with their databases in ASP.NET applications. By following the steps outlined in this blog post, you should be well on your way to leveraging Dapper's capabilities in your projects.