Dapper 介绍
?轉載:http://***/html/itweb/20130918/125194_125199_125210.htm
.NET 輕量級 ORM 框架 - Dapper 介紹
?
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
| Method | Duration | Remarks |
| Hand coded (using a?SqlDataReader) | 47ms | |
| Dapper?ExecuteMapperQuery<Post> | 49ms | |
| PetaPoco | 52ms | Can be faster |
| BLToolkit | 80ms | |
| SubSonic?CodingHorror | 107ms | |
| NHibernate SQL | 104ms | |
| Linq 2 SQL?ExecuteQuery | 181ms | |
| Entity framework?ExecuteStoreQuery | 631ms |
Performance of?SELECT?mapping over 500 iterations - dynamic serialization
| Method | Duration | Remarks |
| Dapper?ExecuteMapperQuery (dynamic) | 48ms | |
| Massive | 52ms | |
| Simple.Data | 95ms |
Performance of?SELECT?mapping over 500 iterations - typical usage
| Method | Duration | Remarks |
| Linq 2 SQL?CompiledQuery | 81ms | Not super typical involves complex code |
| NHibernate HQL | 118ms | |
| Linq 2 SQL | 559ms | |
| Entity framework | 859ms | |
| SubSonic?ActiveRecord.SingleOrDefault | 3619ms |
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 ListNote: 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 objectspublic 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 resultspublic 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 timesThe 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/hanjun0612/p/9779879.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
- 上一篇: 【C语言重点难点精讲】关键字精讲
- 下一篇: C++使用localtime函数需要注意