Advertisement
Поиск  
Always will be ready notify the world about expectations as easy as possible: job change page
Jul 15, 2019

Dapper - King of Micro ORM (C#.NET)

Автор:
Friyank Parikh
Источник:
Просмотров:
2006

ORM: Object Relationship Mapper that maps your database directly with C# objects.

There are many ORM tools available. One of them is Dapper. Dapper is known as the king of ORM.

The following are the key features of Dapper:

  • Speed and fast in performance.
  • Fewer lines of code.
  • Object Mapper.
  • Static Object Binding.
  • Dynamic Object Binding.
  • Easy Handling of SQL Query.
  • Easy Handling of Stored Procedure.
  • Operating directly to IDBConnection class that provides smoothness and running query directly to the database instead of passing data using various objects as we do in EF and ADO.NET.
  • Multiple Query Support.
  • Support for Stored Procedure.
  • Bulk Data insert functionality.
  • Dapper also allows fetching multiple data based on multiple inputs.

Why Dapper

Dapper is the second fastest ORM.

Image reference: Dapper dotnet.

 

  • Perform CRUD operations directly using IDBConnection object.
  • Provide querying static and dynamic data over database.
  • Get generic result for simple or complex data type.
  • Dapper allow to store bulk data at once.

How to Install Dapper

There are two ways to install Dapper:

  1. DapperMappingFileForC#4.5.cs.

    Add this SqlMapperAsync.cs file to your project and get started with Dapper functionality.

    You can SqlMapperAsync.cs depending on the .Net framework you use.
     
  2. NuGet package Manager.

    In Visual Studio, create a new console project and in Solution Explorer right-click References and select Manage NuGet package Manager and search for Dapper and using the NuGet Package Manager Console command for the Nugget Package Manager “install-package dapper”, and this will install Dapper into your project.

How Dapper Works

“Dapper Majorly Include Three Steps”

Step 1

Create an IDBConnection object with Connection String.

Step 2

Write a query and store it in a normal string variable.

Step 3

Call db.execute() and pass the query and it's done.

There are many other ways as well that we will explore in the following example.

For this example I have one database named "ContactDB" and include one table called Contacts. Using Dapper let's perform CRUD operations on this contacts table.

 

Create a Console application and install Dapper and include the namespace for Dapper.

 

Example 1

Let's first start by getting all the data and printing it onto the console.

So:

Step 1

Create an object of an IDbConnection class and new instance of SqlConnection.

static IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlServerConnString"].ConnectionString); 

SqlServerConnString is a connection string name that you can write in app.config of your console application.

<connectionStrings>
    <add name="SqlServerConnString" providerName="System.Data.SqlClient" connectionString="Data Source=Friyank\MSSQLSERVER2012;Initial Catalog=ContactDB;Integrated Security=True;MultipleActiveResultSets=True" />
</connectionStrings>
 

Step 2

Write a Query and store it into a string.

String query = "select * from contacts"; 

Step 3

Fire a query on db instance and type cast the generic return type into a list of contacts.

(List<Contact>)db.Query<Contact>(query); 

You have successfully retrieved all the data from the contact table just by using these three simple steps to populate this data whereever you need it.

Print the entire list onto Grid view of a Windows Forms form or WPF or pass the entire List of data to any external WCF.

For demo purpose I will just print this data onto the console.

 

Example 2

Now let's insert data into a Contact table. This also includes the same three steps.

Step 1

Create an object of the IDbConnection class and a new instance of SqlConnection.

static IDbConnection db. = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlServerConnString"].ConnectionString); 

Step 2

string query = @"Insert into contacts values (@FirstName, @LastName, @Email, @Company, @Title);
Select Cast (Scope_Identity() as int)"; 

Here, we include two queries to get the last inserted id of the contact table.

Step 3

Now fire the query over the db instance and for the return type, we will get a single value and that will be an int. So, store it into an int variable and pass the contact object with the query as follows.

int id = db.Query<int>(query,contact).Single(); 

And again in these three simple steps, the data will be inserted into the database.

 

Example 3

Now let's get a single amount of data from the database contact table by passing an id in the where clause.

Step 1

Create an object of the IDbConnection class and a new instance of SqlConnection.

static IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlServerConnString"].ConnectionString); 

Step 2

string query = "select * from contacts where id = @id"; 

Step 3

Now fire the query over the db instance and for the return type, we will get a single value and that will be a contact. So, store it into the contact object.

Contact cont = (Contact)db.Query<Contact>(query, new {id = id }).SingleOrDefault(); 

Again in these three simple steps, data will be fetched from the database using id.

 

Note: In the same way we can update and delete records of the database.

We can even use db.Execute().

The main difference between db.query and db.execute is, in db.query we get a return value as we desired since it's a generic method and db.execute is not a generic method so it always returns an int.

Working With Advanced Dapper

  • We may have a requirement to deal with multiple tables, for example querying more than two tables at once or fetching data based on foreign key.
  • We can access multiple tables at once in Dapper and that is also very smooth.
  • Pass a list of object and Dapper itself will identify the insertion as a bulk insert.
  • Fetch data based on various parameters and Dapper will automatically convert the array into CSV and return all in a list of objects.

Example 4

Let's say I have one more table called address as in the following:

 

We will perform CRUD operations on both tables together using the same contact id as the primary key for the contact table and foreign key for the address table.

Now let's get multiple data from the database contact table and database address table passing an id.

Step 1

Create an object for the IDbConnection class and a new instance of SqlConnection.

static IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlServerConnString"].ConnectionString); 

Step 2

Now create a string for multiple queries.

string query = "select * from contacts where id = @id ; select * from addresses where ContactId = @id;"; 

Step 3

Now fire a query on db instance as follows:

using (var multipleresult = db.QueryMultiple(query, new { id = id }))
{
    var contact = multipleresult.Read<Contact>().SingleOrDefault();
    var Addresses = multipleresult.Read<Address>().ToList();
    if (contact != null && Addresses != null)
    {
        contact.Addresses.AddRange(Addresses);
    }

And its done, you have successfully retrieved multiple records from multiple tables just using three steps.

 

Example 5

Let's use a Stored Procedure for data access from the database.

I have one Stored Procedure that excepts one parameter (ID) and returns contact data and address data based on that ID.

USE [ContactDB]
GO
/****** Object: StoredProcedure [dbo].[sp_GetContact_Address] Script Date: 3/3/2015 3:29:25 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create proc [dbo].[sp_GetContact_Address]
  @id int
as
begin
select * from contacts where id = @id ;
select * from addresses where ContactId = @id;

end
 

To call a Stored Procedure using Dapper.

Step 1

Create an object for the IDbConnection class and a new instance of SqlConnection.

static IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlServerConnString"].ConnectionString); 

Here we will not write any query and then pass it to the database.

Instead we will call a Stored Procedure that is situated inside the database.

The changes we will do here is to pass a Stored Procedure name instead of query and will pass one additional parameter called command type.

Step 2

Now fire a query on db instance as follows:

using (var multipleresult = db.QueryMultiple(“sp_GetContact_Address”, new { id = id }, commandType: CommandType.StoredProcedure))
{
    var contact = multipleresult.Read<Contact>().SingleOrDefault();
    var Addresses = multipleresult.Read<Address>().ToList();
    if (contact != null && Addresses != null)
    {
        contact.Addresses.AddRange(Addresses);
    }
}
 

And in just two simple steps, you have successfully made a call to a database Stored Procedure.

Example 6

We can also pass a dynamic object or values to a Stored Procedure when we need to deal with runtime objects.

The following is a sample Stored Procedure that adds a contact into the contact table:

create procedure [dbo].[SaveContact]
    @Id int output,
    @FirstName varchar(50),
    @LastName varchar(50),
    @Company varchar(50),
    @Title varchar(50),
    @Email varchar(50)
AS
BEGIN
INSERT INTO [dbo].[Contacts]
    ([FirstName],[LastName],[Company],[Title],[Email])
VALUES
    (@FirstName, @LastName, @Company, @Title, @Email);
    SET @Id = cast(scope_identity() as int)
END;
 

The following shows how to call a Stored Procedure using Dapper and pass dynamic values.

Step 1

Create an object of the IDbConnection class and a new instance of SqlConnection.

static IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlServerConnString"].ConnectionString); 

Step 2

Create a dynamic object and pass a value to that object.

var parameter = new DynamicParameters();
parameter.Add("@Id",con.Id,dbType: DbType.Int32,direction:ParameterDirection.InputOutput);
parameter.Add("@FirstName", con.FirstName);
parameter.Add("@LastName", con.LastName);
parameter.Add("@Company", con.Company);
parameter.Add("@Title", con.Title);
parameter.Add("@Email", con.Email); 

Step 3

Call a Stored Procedure using the db.execute method.

db.Execute("SaveContact",parameter,commandType:CommandType.StoredProcedure);

//To get newly created ID back
con.Id = parameter.Get<int>("@Id");
 

Passing data to the Stored Procedure.

#region Insert Dynamic Object To Database
dynamic c = new Contact();
Program p = new Program();
Console.WriteLine("Enter First Name : ");
c.FirstName = Console.ReadLine();
Console.WriteLine("Enter Last Name : ");
c.LastName = Console.ReadLine();
Console.WriteLine("Enter Email Address : ");
c.Email = Console.ReadLine();
Console.WriteLine("Enter Company Name: ");
c.Company = Console.ReadLine();
Console.WriteLine("Enter Title : ");
c.Title = Console.ReadLine();
Console.WriteLine("New Contact Created With ID {0} ", p.dynamicspcall(c).Id);
#endregion 

And these are the ways to manipulate a database using C# dynamic objects.

Похожее
Dec 16, 2023
Author: MuhammedAKBAS
At some point in our careers, many of us have found ourselves working on a project that requires performance tuningThe need to write high performance .NET code should come from performance issues or business needs.Today, we’ll take a look at...
Oct 26, 2023
Author: Matt Bentley
How to implement CQRS in ASP.NET using MediatR. A guided example using CQRS with separate Read and Write models using Enity Framework Core for Commands and Dapper for Queries.When people think about CQRS they often think about complex, event-driven, distributed...
Apr 12, 2023
Author: Jaimin Soni
HTTP/3 is the latest version of the HTTP protocol, built on top of the QUIC transport protocol. It offers several advantages over its predecessors, including faster and more reliable connections, improved security, and reduced latency. In this article, we will...
Oct 31, 2023
Author: Mohamad Ashour
In the article, we are going to examine how to optimize SQL queries and improve query performance by using SQL query optimization tips and techniques and many others.Before going towards the main topic of SQL Query optimization, let us first...
Написать сообщение
Почта
Имя
*Сообщение


© 1999–2024 WebDynamics
1980–... Sergey Drozdov
Area of interests: .NET Framework | .NET Core | C# | ASP.NET | Windows Forms | WPF | HTML5 | CSS3 | jQuery | AJAX | Angular | React | MS SQL Server | Transact-SQL | ADO.NET | Entity Framework | IIS | OOP | OOA | OOD | WCF | WPF | MSMQ | MVC | MVP | MVVM | Design Patterns | Enterprise Architecture | Scrum | Kanban