bltoolkit mysql_Dapper - 一款轻量级对象关系映射(ORM)组件,DotNet 下

Dapper - a simple object mapper for .Net

Documentation you can improve

The Dapper tag wiki on Stack Overflow can be improved by any Stack Overflow users. Feel free to add relevant information there.

License

Dapper is offered under dual license - take your pick:

Both are liberal licenses allowing you to use, modify and distribute the software as you choose, in both free and commercial works, on an "as is, no warranties" basis (see the linked license terms for details).

Many software repositories (code.google.com, nuget.org) only allow a single license to be listed; we list Apache for convenience - however, either license is acceptable.

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 Query(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("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 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 for some T.

Performance

A key feature of Dapper is performance. The following metrics show how long it takes to execute 500 SELECT statements against a DB and map the data returned to objects.

The performance tests are broken in to 3 lists:

POCO serialization for frameworks that support pulling static typed objects from the DB. Using raw SQL.

Dynamic serialization for frameworks that support returning dynamic lists of objects.

Typical framework usage. Often typical framework usage differs from the optimal usage performance wise. Often it will not involve writing SQL.

Performance of SELECT mapping over 500 iterations - POCO serialization

Method

Duration

Remarks

Hand coded (using a SqlDataReader)

47ms

Dapper ExecuteMapperQuery

49ms

BLToolkit

80ms

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

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

Performance benchmarks are available here: http://code.google.com/p/dapper-dot-net/source/browse/Tests/PerformanceTests.cs , Feel free to submit patches that include other ORMs -when running benchmarks, be sure to compile in Release and not attach a debugger (ctrl F5)

Parameterized queries

Parameters are passed in as anonymous classes. This allow you to name your parameters easily and gives you the ability to simply cut-and-paste SQL snippets and run them in Query analyzer.

new {A = 1, B = "b"} // A will be mapped to the param @A, B to the param @B

Advanced features

List Support

Dapper allow you to pass in IEnumerable and will automatically parameterize your query.

For example:

connection.Query("select * from (select 1 as Id union all select 2 union all select 3) as X where Id in @Ids", new { Ids = new int[] { 1, 2, 3 });

Will be translated to:

select * from (select 1 as Id union all select 2 union all select 3) as X where Id in (@Ids1, @Ids2, @Ids3)" // @Ids1 = 1 , @Ids2 = 2 , @Ids2 = 3

Buffered vs Unbuffered readers

Dapper's default behavior is to execute your sql and buffer the entire reader on return. This is ideal in most cases as it minimizes shared locks in the db and cuts down on db network time.

However when executing huge queries you may need to minimize memory footprint and only load objects as needed. To do so pass, buffered: false into the Query method.

Multi Mapping

Dapper allows you to map a single row to multiple objects. This is a key feature if you want to avoid extraneous querying and eager load associations.

Example:

var sql =

@"select * from #Posts p

left join #Users u on u.Id = p.OwnerId

Order by p.Id";

var data = connection.Query(sql, (post, user) => { post.Owner = user; return post;});

var post = data.First();

post.Content.IsEqualTo("Sams Post1");

post.Id.IsEqualTo(1);

post.Owner.Name.IsEqualTo("Sam");

post.Owner.Id.IsEqualTo(99);

important note Dapper assumes your Id columns are named "Id" or "id", if your primary key is different or you would like to split the wide row at point other than "Id", use the optional 'splitOn' parameter.

Multiple Results

Dapper allows you to process multiple result grids in a single query.

Example:

var sql =

@"

select * from Customers where CustomerId = @id

select * from Orders where CustomerId = @id

select * from Returns where CustomerId = @id";

using (var multi = connection.QueryMultiple(sql, new {id=selectedId}))

{

var customer = multi.Read().Single();

var orders = multi.Read().ToList();

var returns = multi.Read().ToList();

...

}

Stored Procedures

Dapper supports fully stored procs:

var user = cnn.Query("spGetUser", new {Id = 1},

commandType: CommandType.StoredProcedure).First();}}}

If you want something more fancy, you can do:

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);

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

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

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

Ansi Strings and varchar

Dapper supports varchar params, if you are executing a where clause on a varchar column using a param be sure to pass it in this way:

Query("select * from Thing where Name = @Name", new {Name = new DbString { Value = "abcde", IsFixedLength = true, Length = 10, IsAnsi = true });

On Sql Server it is crucial to use the unicode when querying unicode and ansi when querying non unicode.

Limitations and caveats

Dapper caches information about every query it runs, this allow it to materialize objects quickly and process parameters quickly. The current implementation caches this information in a ConcurrentDictionary object. The objects it stores are never flushed. If you are generating SQL strings on the fly without using parameters it is possible you will hit memory issues. We may convert the dictionaries to an LRU Cache.

Dapper's simplicity means that many feature that ORMs ship with are stripped out, there is no identity map, there are no helpers for update / select and so on.

Dapper does not manage your connection's lifecycle, it assumes the connection it gets is open AND has no existing datareaders enumerating (unless MARS is enabled)

Will dapper work with my db provider?

Dapper has no DB specific implementation details, it works across all .net ado providers including sqlite, sqlce, firebird, oracle, MySQL and SQL Server

Do you have a comprehensive list of examples?

Who is using this?

Dapper is in production use at:

(if you would like to be listed here let me know)

Tools and extensions for Dapper (3rd party)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值