
The Dapper ORM(object-relational mapper) has gotten extensive popularity for dealing with databases in.NET due to the fact that of its high speed and simpleness. We discovered the fundamentals of working with Dapper in a previous post here. We also went over working with the Dapper Extensions library in an earlier short article. In this post, we’ll have a look at some innovative features of Dapper.To utilize the code examples supplied in this article, you should have Visual Studio 2022 set up in your system. If you don’t currently have a copy, you can download Visual Studio 2022 here. Produce an ASP.NET Core Web API project in Visual Studio 2022 First of all, let’s create an ASP.NET Core 7 job in Visual Studio 2022.
Follow these steps: Release the Visual Studio 2022
IDE. Click “Create brand-new project.”In the” Develop brand-new job”window, choose” ASP.NET Core Web API”from the list of templates showed.
Click Next. In the”
Configure your brand-new project” window, define the name and location for the new job. Optionally inspect the”Place solution and task
in the exact same directory” check box, depending upon your preferences. Click Next.
In the “Extra Info”window revealed next, leave the” Usage controllers(uncheck to utilize minimal APIs)”box inspected.
We won’t be utilizing very little APIs in this job. Leave the “Authentication Type”set to “None” (the default). Ensure that the check boxes “Enable Open API Assistance, “”Set up for HTTPS,” and “Enable Docker”stay unchecked. We will not be utilizing those features here. Click Develop. We’ll utilize this ASP.NET Core 7 Web API task to deal with sophisticated functions of Dapper in the areas below.What is Dapper? Why use it?Object-
relational mappers have been used for many years to deal with the “impedance inequality” in between object models in programs languages and data models in relational databases. The Stack Overflow team created Dapper to be a simple ORM for.NET. Dapper is an open source project on GitHub. Dapper is a light-weight, high performance, micro-ORM framework for.NET and.NET Core that supports a wide range of databases. These consist of Microsoft SQL Server, Oracle Database, MySQL, PostgreSQL, SQLite, and SQL CE. Compared to other ORMs, Dapper is lightweight, fast, and simple
, developed with efficiency and functionality in mind. Advanced features in Dapper In this section, we’ll analyze some of the advanced features of Dapper with appropriate code examples.Multi-mapping Dapper’s multi-mapping functionality allows you to map query outcomes to several things. This type of query can be utilized to get pertinent information from many tables with a single inquiry. By passing a delegate function as an argument to the Question method or QueryAsync technique, you can instruct Dapper to map the results of an inquiry to different things. Think about
the following two classes. public class Author public int Id get; set; public string FirstName get; set; public string LastName public class Book p> public string ISBN get; set; The following code snippet shows how you can execute multi-mapping using Dapper.string connectionString= “Define the connection string to connect to your database …”; utilizing(var connection =new SqlConnection(connectionString ))
Broad database support Dapper supplies support for connecting to and working with numerous kinds of databases such as SQL Server, Oracle Database, MySQL, and PostgreSQL. The following code snippets demonstrate how you can link to these various databases utilizing Dapper.var connection = new SqlConnection( connectionString );// Linking to SQL Server databasevar connection = new OracleConnection(connectionString);/ / Linking to Oracle databasevar connection = new MySqlConnection (connectionString);// Linking to MySQL databasevar
connection=new NpgsqlConnection(connectionString);// Linking to PostgreSQL database Bulk insert Dapper provides bulk insert operations that can substantially improve efficiency when you’re trying to insert big volumes of information into your database. The SqlBulkCopy class in ADO.NET allows effective information transfer between a source and a database. In Dapper, you can perform bulk inserts by passing an enumerable collection of challenge SqlMapperExtensions.Insert.The following code example shows how you can carry out bulk inserts utilizing Dapper in your ASP.NET Core 7 application. utilizing (var connection=new SqlConnection(connectionString)) Kept treatments You can utilize the Inquiry, QueryFirstOrDefault, Execute, and similar techniques to conjure up kept treatments in Dapper. Dapper likewise supports interaction with the output criteria and return values of kept treatments through
the DynamicParameters class, which simplifies how you can work with parameters.The code example provided listed below shows how you can deal with saved procedures using Dapper in your ASP.NET Core 7 application.using(var connection=brand-new SqlConnection (connectionString)). var criteria=new DynamicParameters(); int customerId =27; string orderStatus= “Pending “; parameters.Add(“@CustomerId”, customerId, DbType.Int32 ); parameters.Add (“@OrderStatus”, orderStatus, DbType.String ); var orders =connection.Query(
“GetOrdersByStatus”, parameters, commandType: CommandType.StoredProcedure); Inquiry pagination When dealing with large outcome sets, pagination is vital to improving efficiency and efficiency. The Avoid and Take methods in Dapper can be used to carry out question pagination. By combining these approaches with the SQL provisions OFFSET and FETCH NEXT, you can effectively obtain a subset of information from the database.Below is a code example that highlights how query pagination can be utilized in Dapper. string connectionString =”Specify the connection string to connect to your database … “; utilizing (var connection= new SqlConnection(connectionString )) Inquiry caching This is a great feature in Dapper that enhances performance significantly, especially if inquiries are run regularly. You can cache inquiries and results in Dapper. When you execute the query for the very first time, the results upon execution of the query are saved in the cache. Whenever the exact same question is performed again and the data is readily available in the cache, the cached data is returned instead of retrieving the information from an API call or a database.The following code example shows how query caching can be used.var sql=”SELECT * FROM Authors WHERE AuthorId= @AuthorId”; var author = connection.QueryFirstOrDefault(sql, brand-new AuthorId = authorId.);// When the inquiry is performed the next time, it will utilize the cached outcome var cachedAuthor=connection.QueryFirstOrDefault (sql, new. AuthorId =authorId ); Dynamic specifications Dapper permits parameter worths to be passed to questions utilizing anonymous types or dictionaries. This function is useful when the variety of criteria or their worths are unidentified at compile time. The following code bit illustrates how you can utilize this function in Dapper.using (var connection=brand-new SqlConnection(connectionString ))var parameters=new CustomerId =1, OrderStatus =”Shipped”; var inquiry =” Select * From Orders Where CustomerId=@CustomerId AND Status =@OrderStatus”; var orders=connection.Query(question, specifications). ToList();Dapper is a light-weight and personalized ORM concentrating on efficiency and simpleness. Dapper’s advanced features can substantially enhance information access abilities and allow developers to engage better with databases in ASP.NET Core applications.Although Dapper has relatively rich performance, it does not supply all of the features of more powerful ORMs such as Entity Structure. Therefore, it is important to analyze your application’s requirements and select the appropriate data access innovation. Copyright © 2023 IDG Communications, Inc. Source