Message to administrator
Имя:
Email:
Message:
Sign In
Username:
Password:

Donation  •  Journal  •  About  •  Advertisement  •  Place ads banner  •  Send content  •  Timeline  •  Translate  •  Featured  •  Message to admin Guests: 19    Members: 0 Авторизация Sign In   Sign Up 
Scientific Poke Method
RULVEN
Search  
Blackball iMag | интернет-журнал
RSS-лента
Share link:
Catalogue


Home » Software development » Dapper - King of Micro ORM (C#.NET)

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


Added: Пн 29.07.2019 • Sergeant
Author: Friyank Parikh
Source: source
Views: 797
Comments: 0


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.


   
 

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)db.Query(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(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(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().SingleOrDefault();
    var Addresses = multipleresult.Read

().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().SingleOrDefault();
    var Addresses = multipleresult.Read

().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("@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.



Мне нравится 0   Мне не нравится 0



Comments

Чтобы добавить видео с YouTube, нужно написать [@youtube=xxxxx] , где xxxxx – ID видео.


Комментарии: 0
Нет ни одного комментария.

Новое
Зал короля Артура оказался неолитическим загоном для скота 3 дня назад, 09:05
Зал короля Артура оказался неолитическим загоном для скота
15 действительно вкусных салатов с крабовыми палочками Сб 16.11.2024
15 действительно вкусных салатов с крабовыми палочками
Почему W-образные моторы уходят в прошлое, если они были лучше V-образных Ср 13.11.2024
Почему W-образные моторы уходят в прошлое, если они были лучше V-образных
Когда устал от алгоритмов: Ревью кода на собеседовании Вт 12.11.2024
Когда устал от алгоритмов: Ревью кода на собеседовании
Вирусы на Android: подробное руководство по обеспечению безопасности Пн 11.11.2024
Вирусы на Android: подробное руководство по обеспечению безопасности
Пн 11.11.2024
10 не самых очевидных причин, чтобы уволиться
Искусственный мозг против квантового компьютера: кто возьмет верх? Вс 10.11.2024
Искусственный мозг против квантового компьютера: кто возьмет верх?
10 лучших салатов с кукурузой Сб 09.11.2024
10 лучших салатов с кукурузой
10 вкусных салатов с фасолью, которые хочется готовить снова и снова Сб 02.11.2024
10 вкусных салатов с фасолью, которые хочется готовить снова и снова
Пишем одностраничное приложение с помощью htmx Вт 29.10.2024
Пишем одностраничное приложение с помощью htmx
Books
Blazor in Action Вт 04.06.2024
Blazor in Action
Год: 2022
Security for Containers and Kubernetes Вт 28.05.2024
Security for Containers and Kubernetes
Год: 2023
Designing Data-Intensive Applications Вт 14.05.2024
Designing Data-Intensive Applications
Год: 2017
Fundamentals of Software Architecture Вт 07.05.2024
Fundamentals of Software Architecture
Год: 2020
Разработано на основе BlackNight CMS
Release v.2024-11-16
© 2000–2024 Blackball
Design & programming:
AboutAdvertising
Visitors
Web-site performed by Sergey Drozdov
BlackballAdvertisingStatsПоддержка
MusicPlaylistsCinemaVideoGamesAudioDownloadsMagazinePicturesHumorForumWebsite journalSend contentFeatured