当前位置:网站首页>A gift for dapper users

A gift for dapper users

2020-12-07 14:04:02 itread01

## At the beginning As we all know Dapper yes .NET The lightest and fastest ORM, It likes to write SQL The gospel of Manon , Relative to SqlHelper It's more convenient , According to statistics 10 One .NETer Yes 9 One Used Dapper. Because of .NET The special environment , Yes Lambda Express the love of tree , So there are a lot of things on the market based on Dapper The lightweight of ORM, Almost all of them have in common , Let Dapper Support Lambda Representation tree , Writing is as smooth as running water . Today, we introduce a powerful ORM, He offers a more hidden API function , Use it and Dapper It's nothing different . ## Understanding Lambda Representation tree We have to start with Lambda The expression begins with , One of the words is missing “ Tree ” word , There's a big difference . The expression , The following grammar sugar operation , Generated IL As like as two peas : ```c# // Use C# 2.0 Get string length by anonymous method in Func strLength = delegate(string str) { return str.Length; }; Console.WriteLine(strLength("Hello World!")); // Use Lambda The expression //( Explicit type argument list )=> { Sentence },lambda The longest version of expression strLength = (string str) => { return str.Length; }; Console.WriteLine(strLength("Hello World!")); // A single expression as the body //( Explicit type argument list )=> The expression strLength = (string str) => str.Length; Console.WriteLine(strLength("Hello World!")); // List of arguments for implicit types //( List of implicit type arguments )=> The expression strLength = (str) => str.Length; Console.WriteLine(strLength("Hello World!")); // Fast syntax for a single argument // Argument name => The expression strLength = str => str.Length; Console.WriteLine(strLength("Hello World!")); ``` And the representation tree , The code is similar to the expression , as follows : ```c# Expression > strLength = str => str.Length; ``` > The representation tree does not support code blocks ( Curly braces ) Try to write simply , We usually write like this , Although it's almost the same as express code , But the return value is not the same as the expression , One more generic Expression<>. Representation tree is also called representation tree , Represents code in an abstract way as an object tree , Each node in the tree itself is a representation . The representation tree is not executable code , It's a data structure . It is the code that converts the code written into a tree structure during compilation , In order to carry out reverse analysis . > Above :(strLength.Body as MemberExpression).Member.Name You can get the value "Length" Due to the fact that the representation tree can be parsed reversely , In the last ten years EF It's always been the leader , Every year, China has to produce several ORM, Most of them are based on representation tree parsing . ## Representation tree parsing .NET Technical articles never lack such articles as representation tree parsing , Interested can baidu search , Numerous , But it's not easy to be perfect . FreeSql We have done a lot of work on the representation tree parsing , Examples : 1、 Sub table in Inquire about ```c# .Where(a => fsql.Select ().ToList(b => b.Id).Contains(a.Id)) //WHERE a.Id in (select id from t) ``` 2、 Sub table exists Inquire about ```c# .Where(a => fsql.Select ().Any(b => b.Id == a.Id)) //WHERE exists(select 1 from t where Id = a.Id) ``` 3、 Date formatting ```c# ToList(a => a.CreateTime.ToString("HH:mm:ss")) //date_format(a.`CreateTime`, '%H:%i:%s') ``` 4、 Windowing function ```c# ToList(a => SqlExt.Rank().Over().OrderBy(a.Id).OrderByDescending(b.EdiId).ToValue()) //rank() over(order by a.Id, b.EdiId desc) ``` 5、Join Sub table ```c# ToList(a => string.Join(",", fsql.Select ().ToList(b => b.Id))) //(SELECT group_concat(b.`Id` separator ',') FROM `StringJoin01` b) ``` These features are in different databases , We need to do the corresponding adaptation ,FreeSql It also supports representation tree parsing of navigation properties , I just want to prove that it's not easy to be meticulous . Instead of making trouble for yourself , It's better to use it directly ? ## Ado.Net Extension suite implementation Instead of making trouble for yourself , It's better to use it directly ?FreeSql Provides a non - staple API Usage habits , It's like Dapper There's no difference . Support MySql/SqlServer/PostgreSQL/Oracle/Sqlite/Firebird/ Reach a dream / Supernatural power / Renmin Jincang / Hangao /MsAccess More than ten database adaptations , Support Ado.net/Odbc, And support .NetFramework 4.0 Platform 、.NET5.0、.NET Core2.1 + Platform . First step : With the database SqlServer Visit for example , All you need to do is install the divided package : ```bash dotnet add packages FreeSql.Provider.SqlServer ``` or ```bash Install-Package FreeSql.Provider.SqlServer ``` The second step : Create entity class ```c# class TestConnectionExt { public Guid id { get; set; } public string title { get; set; } public DateTime createTime { get; set; } = DateTime.Now; } ``` The third step : Start CRUD ```c# using (var conn = new SqlConnection(connectString)) { var list = conn.Select ().Where(a => a.id == item.id).ToList(); } using (var conn = new SqlConnection(connectString)) { var item = new TestConnectionExt { title = "testinsert" }; var affrows = conn.Insert(item).ExecuteAffrows(); } using (var conn = new SqlConnection(connectString)) { var affrows = conn.Update () .Where(a => a.Id == xxx) .Set(a => a.title, "testupdated") .ExecuteAffrows(); } using (var conn = new SqlConnection(connectString)) { var affrows = conn.Delete () .Where(a => a.Id == xxx) .ExecuteAffrows(); } ``` Add or update : ```c# using (var conn = new SqlConnection(connectString)) { var affrows = conn.InsertOrUpdate () .SetSource(item) .ExecuteAffrows(); } ``` Added above 、 Delete 、 modify 、 Inquire about , Entity class operations are already supported , And support batch insert 、 Batch update 、 Bulk delete 、 Multi table query 、 Navigation property query . You can enjoy FreeSql Almost all functions . reflection : Use this API It seems that you can easily access to abp vnext in ? ## Conclusion FreeSql Use the world's most relaxed open source protocol MIT Trust in github:https://github.com/dotnetcore/FreeSql A stable version with two years of high frequency iterations has been released v2.0, Welcome to follow and use . Support .NetFramework 4.0+、.NetCore 2.1+、Xamarin Wait for support NetStandard All execution platforms . Support MySql/SqlServer/PostgreSQL/Oracle/Sqlite/Firebird/ Reach a dream / Supernatural power / Renmin Jincang / Hangao /MsAccess Database , Support Ado.net/Odbc. QQ Group :4336577( It's full )、8578575( Online )、52508226( Online ) ![](https://img2020.cnblogs.com/blog/31407/202012/31407-20201207092143960-960862465.png)

版权声明
本文为[itread01]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/20201207140317813q.html