.NET 轻量级 ORM 框架 - Dapper 介绍

mac2022-06-30  57

转自:https://blog.csdn.net/hanjun0612/article/details/52170204

Dapper简单介绍:

Dapper is a single file you can drop in to your project that will extend your IDbConnection interface.

Dapper是一个轻型的开源ORM类,代码就一个SqlMapper.cs文件,编译后就40多K的一个很小的Dll. 官方资料:点击这里

Dapper支持Mysql,SqlLite,Mssql2000,Mssql2005,Oracle等一系列的数据库,当然如果你知道原理也可以让它支持Mongo db

Dapper的r支持多表并联的对象。支持一对多 多对多的关系。并且没侵入性,想用就用,不想用就不用。无XML无属性。代码以前怎么写现在还怎么写。

Dapper原理通过Emit反射IDataReader的序列队列,来快速的得到和产生对象。性能提升了很多;(比采用常规的反射)

Dapper支持net2.0,3.0,3.5,4.0。不过就是要配置下。如果不知道如何配置查看我博客里的在2.0下使用3.5就可以了。

语法十分简单。并且无须迁就数据库的设计。

Dapper执行效率:

 

30W条数据,取其中的一个对象,和第一页前15条数据,耗时0.0906879秒。这个速度超过Datable。

官方的测试代码以及数据

Performance of SELECT mapping over 500 iterations - POCO serialization

MethodDurationRemarksHand coded (using a SqlDataReader)47msDapper ExecuteMapperQuery<Post>49msPetaPoco52msCan be fasterBLToolkit80msSubSonic CodingHorror107msNHibernate SQL104msLinq 2 SQL ExecuteQuery181msEntity framework ExecuteStoreQuery631ms

Performance of SELECT mapping over 500 iterations - dynamic serialization

MethodDurationRemarksDapper ExecuteMapperQuery (dynamic)48msMassive52msSimple.Data95ms

Performance of SELECT mapping over 500 iterations - typical usage

MethodDurationRemarksLinq 2 SQL CompiledQuery81msNot super typical involves complex codeNHibernate HQL118msLinq 2 SQL559msEntity framework859msSubSonic ActiveRecord.SingleOrDefault3619ms

Dapper使用介绍:

如果你使用的是vs2012,可以使用NuGet来进行安装,会自动添加引用,使用时写入命名空间即可;

 

 using Dapper;

 

下面的代码可以作为使用参考:

public static readonly string sqlconnectionString = "Data Source=xxx;Initial Catalog=Express;User ID=sa;Password=123";

  public static readonly string mysqlconnectionString = @"server=xxx;database=dddd;uid=xxx;pwd=123;charset='gbk'";   public static SqlConnection SqlConnection() {     var connection = new SqlConnection(sqlconnectionString);     connection.Open();     return connection; }   public static  MySqlConnection  MySqlConnection() {     var connection = new MySqlConnection(mysqlconnectionString);     connection.Open();     return connection;

}

调用方法

 

 

SqlConnection connection = Program.SqlConnection();

 

 

获得一个实体对象

var d = connection.Query<Dog>("select * from dog where id = 1"null).Single<Dog>();

获得实体对象结合

var dd = connection.Query<Dog>("select * from dog where id < 10", null).ToList<Dog>();

插入数据

//动态参数 connection.Execute("INSERT INTO dog (Age,Name,Weight) VALUES (@age,@name,@Weight)", new { @age = i,@name = Guid.NewGuid().ToString(), @Weight = i }); //直接传入实体 connection.Execute("INSERT INTO dog (Age,Name,Weight) VALUES (@age,@name,@Weight)",model);

 

Execute a query and map the results to a strongly typed List Note: all extension methods assume the connection is already open, they will fail if the connection is closed. public static IEnumerable<T> Query<T>(this IDbConnection cnn, string sql, object param = null, SqlTransaction transaction = null, bool buffered = true) Example usage: publicclassDog { publicint?Age{get;set;} publicGuidId{get;set;} publicstringName{get;set;} publicfloat?Weight{get;set;} publicintIgnoredProperty{get{return1;}} } var guid =Guid.NewGuid(); var dog = connection.Query<Dog>("select Age = @Age, Id = @Id",new{Age=(int?)null,Id= guid }); dog.Count() .IsEqualTo(1); dog.First().Age .IsNull(); dog.First().Id .IsEqualTo(guid); Execute a query and map it to a list of dynamic objects public static IEnumerable<dynamic> Query (this IDbConnection cnn, string sql, object param = null, SqlTransaction transaction = null, bool buffered = true) This method will execute SQL and return a dynamic list. Example usage: var rows = connection.Query("select 1 A, 2 B union all select 3, 4"); ((int)rows[0].A) .IsEqualTo(1); ((int)rows[0].B) .IsEqualTo(2); ((int)rows[1].A) .IsEqualTo(3); ((int)rows[1].B) .IsEqualTo(4); Execute a Command that returns no results public static int Execute(this IDbConnection cnn, string sql, object param = null, SqlTransaction transaction = null) Example usage: connection.Execute(@" set nocount on create table #t(i int) set nocount off insert #t select @a a union all select @b set nocount on drop table #t",new{a=1, b=2}) .IsEqualTo(2); Execute a Command multiple times The same signature also allows you to conveniently and efficiently execute a command multiple times (for example to bulk-load data) Example usage: connection.Execute(@"insert MyTable(colA, colB) values (@a, @b)", new[]{new{ a=1, b=1},new{ a=2, b=2},new{ a=3, b=3}} ).IsEqualTo(3);// 3 rows inserted: "1,1", "2,2" and "3,3" This works for any parameter that implements IEnumerable<T> for some T.

 

 

 

 

var dd = connection.Query<Dog>("select * from dog where id < 10", null).ToList<Dog>();

转载于:https://www.cnblogs.com/zhoufangcheng04050227/p/10254347.html

相关资源:dotnet-基于Dapper的轻量级的高性能的简单的灵活的ORM框架
最新回复(0)