• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Dapper: Dapper - a simple object mapper for .Net Suport TargetFrameworks: net40; ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

Dapper

开源软件地址:

https://gitee.com/stoneson/Dapper

开源软件介绍:

Dapper - a simple object mapper for .Net

Build status

Release Notes

Located at dapperlib.github.io/Dapper

Packages

MyGet Pre-release feed: https://www.myget.org/gallery/dapper

PackageNuGet StableNuGet Pre-releaseDownloadsMyGet
DapperDapperDapperDapperDapper MyGet
Dapper.EntityFrameworkDapper.EntityFrameworkDapper.EntityFrameworkDapper.EntityFrameworkDapper.EntityFramework MyGet
Dapper.EntityFramework.StrongNameDapper.EntityFramework.StrongNameDapper.EntityFramework.StrongNameDapper.EntityFramework.StrongNameDapper.EntityFramework.StrongName MyGet
Dapper.RainbowDapper.RainbowDapper.RainbowDapper.RainbowDapper.Rainbow MyGet
Dapper.SqlBuilderDapper.SqlBuilderDapper.SqlBuilderDapper.SqlBuilderDapper.SqlBuilder MyGet
Dapper.StrongNameDapper.StrongNameDapper.StrongNameDapper.StrongNameDapper.StrongName MyGet

Features

Dapper is a NuGet library that you can add 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

public static IEnumerable<T> Query<T>(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null)

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 });Assert.Equal(1,dog.Count());Assert.Null(dog.First().Age);Assert.Equal(guid, dog.First().Id);

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, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null)

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").AsList();Assert.Equal(1, (int)rows[0].A);Assert.Equal(2, (int)rows[0].B);Assert.Equal(3, (int)rows[1].A);Assert.Equal(4, (int)rows[1].B);

Execute a Command that returns no results

public static int Execute(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)

Example usage:

var count = 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 });Assert.Equal(2, count);

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:

var count = 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 } }  );Assert.Equal(3, count); // 3 rows inserted: "1,1", "2,2" and "3,3"

Another example usage when you already have an existing collection:

var foos = new List<Foo>{    { new Foo { A = 1, B = 1 } }    { new Foo { A = 2, B = 2 } }    { new Foo { A = 3, B = 3 } } };var count = connection.Execute(@"insert MyTable(colA, colB) values (@a, @b)", foos);Assert.Equal(foos.Count, count);

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 a SELECT statement against a DB (in various config, each labeled) and map the data returned to objects.

The benchmarks can be found in Dapper.Tests.Performance (contributions welcome!) and can be run via:

dotnet run -p .\benchmarks\Dapper.Tests.Performance\ -c Release -f netcoreapp3.1 -- -f * --join

Output from the latest run is:

BenchmarkDotNet=v0.12.1, OS=Windows 10.0.19041.208 (2004/?/20H1)Intel Core i7-7700HQ CPU 2.80GHz (Kaby Lake), 1 CPU, 8 logical and 4 physical cores.NET Core SDK=3.1.201  [Host]   : .NET Core 3.1.3 (CoreCLR 4.700.20.11803, CoreFX 4.700.20.12001), X64 RyuJIT  ShortRun : .NET Core 3.1.3 (CoreCLR 4.700.20.11803, CoreFX 4.700.20.12001), X64 RyuJIT
ORMMethodReturnMeanStdDevErrorGen 0Gen 1Gen 2Allocated
BelgradeExecuteReaderPost94.46 μs8.115 μs12.268 μs1.75000.5000-8.42 KB
Hand CodedDataTabledynamic105.43 μs0.998 μs1.508 μs3.0000--9.37 KB
Hand CodedSqlCommandPost106.58 μs1.191 μs1.801 μs1.50000.75000.12507.42 KB
DapperQueryFirstOrDefault<dynamic>dynamic119.52 μs1.320 μs2.219 μs3.6250--11.39 KB
Dapper'Query<dynamic> (buffered)'dynamic119.93 μs1.943 μs2.937 μs2.37501.00000.250011.73 KB
Massive'Query (dynamic)'dynamic120.31 μs1.340 μs2.252 μs2.25001.00000.125012.07 KB
DapperQueryFirstOrDefault<T>Post121.57 μs1.564 μs2.364 μs1.75000.7500-11.35 KB
Dapper'Query<T> (buffered)'Post121.67 μs2.913 μs4.403 μs1.87500.8750-11.65 KB
PetaPoco'Fetch<T> (Fast)'Post124.91 μs4.015 μs6.747 μs2.00001.0000-11.5 KB
MightyQuery<T>Post125.23 μs2.932 μs4.433 μs2.25001.0000-12.6 KB
LINQ to DBQuery<T>Post125.76 μs2.038 μs3.081 μs2.25000.75000.250010.62 KB
PetaPocoFetch<T>Post127.48 μs4.283 μs6.475 μs2.00001.0000-12.18 KB
LINQ to DB'First (Compiled)'Post128.89 μs2.627 μs3.971 μs2.50000.7500-10.92 KB
MightyQuery<dynamic>dynamic129.20 μs2.577 μs3.896 μs2.00001.0000-12.43 KB
MightySingleFromQuery<T>Post129.41 μs2.094 μs3.166 μs2.25001.0000-12.6 KB
MightySingleFromQuery<dynamic>dynamic130.59 μs2.432 μs3.677 μs2.00001.0000-12.43 KB
Dapper'Contrib Get<T>'Post134.74 μs1.816 μs2.746 μs2.50001.00000.250012.29 KB
ServiceStackSingleById<T>Post135.01 μs1.213 μs2.320 μs3.00001.00000.250015.27 KB
LINQ to DBFirstPost151.87 μs3.826 μs5.784 μs3.00001.00000.250013.97 KB
EF 6SqlQueryPost171.00 μs1.460 μs2.791 μs3.75001.0000-23.67 KB
DevExpress.XPOGetObjectByKey<T>Post172.36 μs3.758 μs5.681 μs5.50001.2500-29.06 KB
Dapper'Query<T> (unbuffered)'Post174.40 μs3.296 μs4.983 μs2.00001.0000-11.77 KB
Dapper'Query<dynamic> (unbuffered)'dynamic174.45 μs1.988 μs3.340 μs2.00001.0000-11.81 KB
DevExpress.XPOFindObject<T>Post181.76 μs5.554 μs9.333 μs8.0000--27.15 KB
DevExpress.XPOQuery<T>Post189.81 μs4.187 μs8.004 μs10.0000--31.61 KB
EF Core'First (Compiled)'Post199.72 μs3.983 μs7.616 μs4.5000--13.8 KB
NHibernateGet<T>Post248.71 μs6.604 μs11.098 μs5.00001.0000-29.79 KB
EF CoreFirstPost253.20 μs3.033 μs5.097 μs5.5000--17.7 KB
NHibernateHQLPost258.70 μs11.716 μs17.712 μs5.00001.0000-32.1 KB
EF CoreSqlQueryPost268.89 μs19.349 μs32.516 μs6.0000--18.5 KB
EF 6FirstPost278.46 μs12.094 μs18.284 μs13.5000--44.18 KB
EF Core'First (No Tracking)'Post280.88 μs8.192 μs13.765 μs3.00000.5000-19.38 KB
NHibernateCriteriaPost304.90 μs2.232 μs4.267 μs11.00001.0000-60.29 KB
EF 6'First (No Tracking)'Post316.55 μs7.667 μs11.592 μs8.50001.0000-50.95 KB
NHibernateSQLPost335.41 μs3.111 μs4.703 μs19.00001.0000-78.86 KB
NHibernateLINQPost807.79 μs27.207 μs45.719 μs8.00002.0000-53.65 KB

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

Alternatively, you might prefer Frans Bouma's RawDataAccessBencher test suite or OrmBenchmark.

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 your db platform's Query analyzer.

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

List Support

Dapper allows you to pass in IEnumerable<int> and will automatically parameterize your query.

For example:

connection.Query<int>("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

Literal replacements

Dapper supports literal replacements for bool and numeric types.

connection.Query("select * from User where UserTypeId = {=Admin}", new { UserTypeId.Admin });

The literal replacement is not sent as a parameter; this allows better plans and filtered index usage but should usually be used sparingly and after testing. This feature is particularly useful when the value being injectedis actually a fixed value (for example, a fixed "category id", "status code" or "region" that is specific to the query). For live data where you are considering literals, you might also want to consider and test provider-specific query hints like OPTIMIZE FOR UNKNOWN with regular parameters.

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:

Consider 2 classes: Post and User

class Post{    public int Id { get; set; }    public string Title { get; set; }    public string Content { get; set; }    public User Owner { get; set; }}class User{    public int Id { get; set; }    public string Name { get; set; }}

Now let us say that we want to map a query that joins both the posts and the users table. Until now if we needed to combine the result of 2 queries, we'd need a new object to express it but it makes more sense in this case to put the User object inside the Post object.

This is the use case for multi mapping. You tell dapper that the query returns a Post and a User object and then give it a function describing what you want to do with each of the rows containing both a Post and a User object. In our case, we want to take the user object and put it inside the post object. So we write the function:

(post, user) => { post.Owner = user; return post; }

The 3 type arguments to the Query method specify what objects dapper should use to deserialize the row and what is going to be returned. We're going to interpret both rows as a combination of Post and User and we're returning back a Post object. Hence the type declaration becomes

<Post, User, Post>

Everything put together, looks like this:

var sql =@"select * from #Posts pleft join #Users u on u.Id = p.OwnerIdOrder by p.Id";var data = connection.Query<Post, User, Post>(sql, (post, user) => { post.Owner = user; return post;});var post = data.First();Assert.Equal("Sams Post1", post.Content);Assert.Equal(1, post.Id);Assert.Equal("Sam", post.Owner.Name);Assert.Equal(99, post.Owner.Id);

Dapper is able to split the returned row by making an assumption that your Id columns are named Id or id. If your primary key is different or you would like to split the row at a 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 = @idselect * from Orders where CustomerId = @idselect * from Returns where CustomerId = @id";using (var multi = connection.QueryMultiple(sql, new {id=selectedId})){   var customer = multi.Read<Customer>().Single();   var orders = multi.Read<Order>().ToList();   var returns = multi.Read<Return>().ToList();   ...}

Stored Procedures

Dapper fully supports stored procs:

var user = cnn.Query<User>("spGetUser", new {Id = 1},        commandType: CommandType.StoredProcedure).SingleOrDefault();

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<int>("@b");int c = p.Get<int>("@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<Thing>("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.

Type Switching Per Row

Usually you'll want to treat all rows from a given table as the same data type. However, there are some circumstances where it's useful to be able to parse different rows as different data types. This is where IDataReader.GetRowParser comes in handy.

Imagine you have a database table named "Shapes" with the columns: Id, Type, and Data, and you want to parse its rows into Circle, Square, or Triangle objects based on the value of the Type column.

var shapes = new List<IShape>();using (var reader = connection.ExecuteReader("select * from Shapes")){    // Generate a row parser for each type you expect.    // The generic type <IShape> is what the parser will return.    // The argument (typeof(*)) is the concrete type to parse.    var circleParser = reader.GetRowParser<IShape>(typeof(Circle));    var squareParser = reader.GetRowParser<IShape>(typeof(Square));    var triangleParser = reader.GetRowParser<IShape>(typeof(Triangle));    var typeColumnIndex = reader.GetOrdinal("Type");    while (reader.Read())    {        IShape shape;        var type = (ShapeType)reader.GetInt32(typeColumnIndex);        switch (type)        {            case ShapeType.Circle:            	shape = circleParser(reader);            	break;            case ShapeType.Square:            	shape = squareParser(reader);            	break;            case ShapeType.Triangle:            	shape = triangleParser(reader);            	break;            default:            	throw new NotImplementedException();        }      	shapes.Add(shape);    }}

User Defined Variables in MySQL

In order to use Non-parameter SQL variables with MySql Connector, you have to add the following option to your connection string:

Allow User Variables=True

Make sure you don't provide Dapper with a property to map.

Limitations and caveats

Dapper caches information about every query it runs, this allows it to materialize objects quickly and process parameters quickly. The current implementation caches this information in a ConcurrentDictionary object. Statements that are only used once are routinely flushed from this cache. Still, if you are generating SQL strings on the fly without using parameters it is possible you may hit memory issues.

Dapper's simplicity means that many feature that ORMs ship with are stripped out. It worries about the 95% scenario, and gives you the tools you need most of the time. It doesn't attempt to solve every problem.

Will Dapper work with my DB provider?

Dapper has no DB specific implementation details, it works across all .NET ADO providers including SQLite, SQL CE, Firebird, Oracle, MySQL, PostgreSQL and SQL Server.

Do you have a comprehensive list of examples?

Dapper has a comprehensive test suite in the test project.

Who is using this?

Dapper is in production


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap