14
Dapper .Net

Dapper

Embed Size (px)

Citation preview

Page 1: Dapper

Dapper .Net

Page 2: Dapper

Agenda

• Introduction.

• Getting Dapper.

• CRUD Operations.

• Batch Insert.

• Stored Procedures.

• Views.

• Transaction Support.

• Performance Comparison.

• Dapper Extensions.

• Other Micro ORM’s.

• Q & A.

Page 3: Dapper

Introduction

• Dapper.NET is an Open-Source, Micro ORM developed by the developers of Stack Overflow.

• Single file with less than 700 lines of code (Including comments).

• Ultra fast – Performance is a key feature.

• Pure SQL.

• Enrich IDbCommand with extension methods for querying and executing SQL commands.

• Can map query results to list of POCOs or dynamics.

• Support for Stored Procedures, views and transactions.

• Queries caching.

Page 4: Dapper

Getting Dapper

• There are two ways to reference Dapper in your project.

• Since the core of Dapper.Net is only one file. It can be easily added into your project. You can directly access its repository and grab the file.

• In the meanwhile, it can be obtained from Nuget.

Page 5: Dapper

CRUD Operation

• Insertusing(var con=new SqlConnection(conString))

{

string query = @"insert into contactus(id, name, Comment)

values(@id, @name, @Comment)";

con.Execute(query, new {Id, Name, Comment });

}

• Selectusing(var con=new SqlConnection(conString))

{

var feedback = con.Query<ContactU>(@"select * from ContactUs);

return feedback;

}

Page 6: Dapper

CRUD Operation (Cont…)

• Updateusing (var con = new SqlConnection(conString))

{

string query = @"update contactus set name=@name, Comment =@Comment where id=@id";

con.Execute(query, new { newName, newComment, id });

}

• Deleteusing (var con = new SqlConnection(conString))

{

con.Execute("delete ContactUs where id=" + id);

}

Page 7: Dapper

Batch Insert

• There are two ways in achieving batch insert using dapper. One is to pass the array of objects and the other one is to pass the List (Dapper will internally iterate for us).

• Option 1string query = @"insert into contactus(id, name, Comment)

values(@id, @name, @Comment)";

con.Execute(query,

new[]{ new {id=1, name="Name1", comment="Hi1"},

new {id=2, name="Name2", comment="Hi2"},

new {id=3, name="Name3", comment="Hi3"}

});

• Option 2con.Execute(query, lstContact);

Page 8: Dapper

Stored Procedures

• Dapper supports store procedures. Please find the code as below,

using (var con = new SqlConnection(conString))

{

var p = new DynamicParameters();

p.Add("@a", 11);

p.Add("@b", dbType: DbType.Int32, direction: ParameterDirection.Output);

p.Add("@c", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue);

con.Execute("spMagicProc", p, commandType: CommandType.StoredProcedure);

int b = p.Get<int>("@b");

int c = p.Get<int>("@c");

}

Page 9: Dapper

View

• Dapper supports working with views as well. Please find the sample example code as below,

using (var con = new SqlConnection(conString))

{

var feedbacks = con.Query<ContactU>("Select * from view_ContactUs");

return feedbacks;

}

• View create statement.create view view_ContactUs

as

select * from ContactUs

Page 10: Dapper

Transaction Support

• Dapper provides extensive support for transaction. Please find the sample snippet as below,

using (var connection = new SqlConnection(conString)) {

connection.Open();

IDbTransaction transaction = connection.BeginTransaction(); try

{

connection.Execute(@"INSERT into table1 ...", new { p1, p2 }, transaction);

transaction.Commit();

}

catch (Exception ex)

{ transaction.Rollback();}

}

Page 11: Dapper

Performance Comparison

Page 12: Dapper

Dapper Extensions

• Dapper Contrib

• Source code available online, need to be copy the code to our project to reference the dapper contrib.

• Set of extension methods built upon the core of Dapper (SqlMapper).

• Provides intuitive API’s for CRUD and interesting features like SqlBuilder and Dirty tracking.

• Dapper Rainbow

• Available in Nuget.

• Provides a table class and some extension methods to encapsulate the tedious Sql statements.

Page 13: Dapper

Other Micro ORM

• Massive

• Simple.Data

• PetaPoco

Page 14: Dapper

Q & A