Dapper Framework in C#

 Dapper is a simple object mapper for .NET and owns the title of King of Micro ORM in terms of speed and is virtually as fast as using a raw ADO.NET data reader. 

An ORM is an Object Relational Mapper responsible for mapping between a database and a programming language.

Dapper is a popular open source Object-Relational Mapping (ORM) Library for .NET. It makes it easy to work with data in your application by mapping objects to tables in a database.

Dapper is fast, reliable, and well-tested and has been used in production by some of the world's largest companies for many years. It is very easy to use and has a lot of features that make it a powerful tool for data access.

Dapper is faster than Entity Framework for CRUD operation (querying and saving) due to its simplicity. Unlike EF Core, it doesn't have to add all complexity, such as tracking values, writing inefficient SQL queries, and supporting features like lazy loading and all inheritance by default.

However, EF Core is relatively very fast as well. The question about which ORM is the best for you should be more about if you want to write most of your SQL query (Dapper) or if you prefer to write LINQ and have EF Core write the SQL query for you.

How does Dapper works?

Dapper provides a simple and concise way to manage your data model without having to write a lot of code. It is also very easy to use, and its code is clean and readable.

Dapper extends the IDbConnection interface by providing helpful extension methods to query your database. It uses dynamic method generation to enable it to inflate POCOs directly from query results. In addition, it allows you to map database columns directly to properties on your POCO.

A POCO entity is a class that doesn't depend on any framework-specific base class. It is like any other normal . NET CLR class, which is why it is called "Plain Old CLR Objects". POCO entities are supported in both EF 6 and EF Core

When using Dapper, all you need is a connection string and a POCO, and then it is a three-step process.

  • Create an IDbConnection object.
  • Write a query to perform CRUD operations.
  • Pass the query as a parameter in any Execute or Query method.

Dapper is case insensitive when mapping column to property. It doesn't matter if the column name returned is CustomerID or customerid. In both cases, it will be mapped to the right property, such as CustomerId, no matter the casing

Requirements?

Dapper works with any .NET project. This means that it can be used with the following frameworks:

  • .NET Core 5.0 and above
  • .NET Framework 4.6.1 and above
  • .NET Standard 2.0 and above
  • (Older version is also supported by using older version of Dapper)

Dapper also requires to add a provider package such as:

Microsoft.Data.SqlClient (for SQL Server)

System.Data.SqlClient (alternative for SQL Server)

Microsoft.Data.Sqlite (for SQLite)

Any other provider package!

Dapper works with any database provider since there is no database-specific implementation.

Once you have installed the required NuGet packages, you can start using Dapper in your project.

Methods?

Dapper extension methods can be used to perform various operations in the database, such as fetching data, inserting records, updating records, and deleting records.

Dapper will extend your IDbConnection interface with multiple methods and some of the most commonly used dapper extension methods.

  1. Execute: It can execute a command one or multiple times.
  2. ExecuteReader: It can execute a command and return a reader.
  3. Executescalar: It can execute a command and return a scalar value.
  4. Query: Used to fetch data from the database.
  5. QueryFirst: It can execute a query and map the first result.
  6. QueryFirstOrDefault: It can execute a query and map the first result, or a default value if the sequence contains no elements.
  7. QuerySingle: It can execute a query and map the first result and throws an exception if there is not exactly one element in the sequence.
  8. QuerySingleOrDefault: It can execute a query and map the first result, or a default value if the sequence is empty; this method throws an exception if there is more than one element in the sequence.
  9. QueryMultiple: It can execute multiple queries within the same command and map results

Parameters?

Dapper supports many different parameter types. Execute and queries method can use parameters from multiple different ways:

Anonymous: Useful for simple queries where you don't need to create a separate class to represent your data.

Dynamic: Useful for when you need to create a dynamic list of parameters, or when you need to dynamically change the value of a parameter.

List: This allows you to specify multiple parameters on an IN clause by using a list.

String: Useful when working with SQL Server stored procedures that accept varchar input parameters

Dapper also provides some additional functionalities that can greatly improve performance in some scenarios.

  • Async: Dapper also provides an Async (asynchronous) version of extension methods
  • Buffered: A buffered query return the entire reader at once
  • Transaction: Support the transaction and TransactionScope
  • Stored Procedure: Has built-in support for caching stored procedures.


Comments

Popular posts from this blog

Email Sending through O365 using OAuth Protocol

IoT Technology

What is reverse proxy?