浙江省高等学校教师教育理论培训

微信搜索“毛凌志岗前心得”小程序

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
 
Project Information
 Starred by 313 users
 Members
Links

Dapper - a simple object mapper for .Net

Official Github clone: https://github.com/SamSaffron/dapper-dot-net

Features

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

It provides 3 helpers:

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:

public class Dog
{
   
public int? Age { get; set; }
   
public Guid Id { get; set; }
   
public string Name { get; set; }
   
public float? Weight { get; set; }

   
public int IgnoredProperty { get { return 1; } }
}            
           
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);
posted on 2011-12-19 13:52  lexus  阅读(348)  评论(0编辑  收藏  举报