Donation  •  Journal  •  Ads free  •  About  •  Advertisement  •  Place ads banner  •  Fleshlight  •  Send content  •  Timeline  •  Translate Guests: 14    Members: 0 Авторизация Sign In   Sign Up 
Scientific Poke Method
RULVEN
Search  
Blackball iMag | интернет-журнал
Catalogue


Home » Software development » Dapper - King of Micro ORM (C#.NET)
I'll be lucky!

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


Added: Пн 29.07.2019 • Sergeant
Author: Friyank Parikh
Source: источник
Views: 767
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
Нет ни одного комментария.
RSS-лента
Share link:
Удаление грудей
Георазведка
Vite.js: что это и зачем нужно веб-разработчику Vite.js: что это и зачем нужно веб-разработчику
Failed nations: латыши и литовцы отказываются от Латвии и Литвы Failed nations: латыши и литовцы отказываются от Латвии и Литвы
8 лучших напольных акустических систем 8 лучших напольных акустических систем
Регулярные выражения — это не трудно Регулярные выражения — это не трудно
15 потрясающих соусов для свиных рёбрышек 15 потрясающих соусов для свиных рёбрышек
25 фактов о свинге и свингерах 25 фактов о свинге и свингерах
9 лучших дронов с функцией следования 9 лучших дронов с функцией следования
Алкогольные игры для компании — как спасти вечеринку, если стало скучно Алкогольные игры для компании — как спасти вечеринку, если стало скучно

Новое
11 способов быстро и вкусно засолить скумбрию вчера, 09:06
11 способов быстро и вкусно засолить скумбрию
HDMI или Display Port: в чëм разница, и чем лучше выводить изображение на монитор Ср 01.05.2024
HDMI или Display Port: в чëм разница, и чем лучше выводить изображение на монитор
300+ вопросов по JavaScript на собеседовании Пн 29.04.2024
300+ вопросов по JavaScript на собеседовании
25 простых и вкусных маринадов для рыбы Сб 27.04.2024
25 простых и вкусных маринадов для рыбы
Ср 24.04.2024
6 самых мощных немецких автомобилей с двигателем V8
Минусы профессии программиста, что не нравится в работе Пн 22.04.2024
Минусы профессии программиста, что не нравится в работе
15 потрясающих соусов для свиных рёбрышек Сб 20.04.2024
15 потрясающих соусов для свиных рёбрышек
5 ошибок при разработке высоконагруженных сервисов Ср 17.04.2024
5 ошибок при разработке высоконагруженных сервисов
Soft skills: 18 самых важных навыков, которыми должен владеть каждый работник Ср 17.04.2024
Soft skills: 18 самых важных навыков, которыми должен владеть каждый работник
30 вопросов на собеседовании фронтенд разработчика Пн 15.04.2024
30 вопросов на собеседовании фронтенд разработчика
Books
Refactoring with C# Вт 23.04.2024
Refactoring with C#
Год: 2023
Building IoT Visualizations using Grafana Вт 09.04.2024
Building IoT Visualizations using Grafana
Год: 2022
Getting Started with Grafana Вт 02.04.2024
Getting Started with Grafana
Год: 2022

Разработано на основе BlackNight CMS
Release v.2024-04-19
© 2000–2024 Blackball
Design & programming:
AboutAdvertising
Visitors
Web-site performed by Sergey Drozdov
BlackballAdvertisingStatsПоддержка | MusicPlaylistsCinemaVideoGamesAudioDownloadsMagazinePicturesHumorForumWebsite journalSend content