引言:因為接觸過多個ORM,但使用的時候都遇到了各自的一些不夠理想的地方,從最早開始開始公司自己分裝的,到后面用EF,以及Dapper和DapperExtensions 到現在用的FluentData,就說說我自己的使用體驗,在這幾個相比之下,Dapper應該是最輕量級,而且性能也是最好的,但是相對比較簡單了點。EF的最新版也沒去使用,所以現在不是很了解,EF在這幾個相比一下,功能是最強大的,但是啟動加載慢,以及復雜的功能,后續人優化麻煩。FluentData 怎么說呢,用的都挺好用,而且語法是最容易讓人理解,但是還是不支持Lambda。基于以上這些,所以萌生了我自己動手做一個ORM,性能能跟Dapper比肩,語法要簡單,容易理解,所以很多地方借鑒了FluentData ,但是內部的邏輯是完全不一樣的。
總的來講的話, DapperLambda 就是Dapper和DapperExtensions的二次分裝,使用語法和FluentData基本類似,并加上了Lambda.原則:封裝和豐富這些功能不是為了不寫SQL,而是做到簡單的SQL,不希望在開發中重復寫。。。
下面我將介紹在開發過程中的運用.
一:下載該項目并且引用DapperLambda.dll,在VS的命令窗口中執行 Install-Package DapperLambda
或是 在引用->管理NuGet程序包中搜索 DapperLambda
二.dll引用入到我們的數據業務層.
它是我們與數據庫操作中的上下文,所有的有關數據操作都調用它下面的方法;目前只針對MSSQL進行了單元測試,后續會繼續完善在其他數據庫中使用,所以非MSSQL,使用請謹慎
1 public static DbContext GetContext()2 {3 string connectionStr = "server=(local);User ID=sa;PassWord=password01!;Database=LocalDB;Persist Security Info=True;Pooling=true;Max Pool Size=700";4 return new DbContext().ConnectionString(connectionStr, DatabaseType.MSSQLServer);5 }
1 [TestMethod]2 public void TestOrderByMethod()3 {4 using (var context = DBHelper.GetContext())5 {6 var ls = context.Select<application>().Where(p => p.CategoryID == 1).OrderBy(p => p.ApplicationID).QueryMany();7 Assert.IsTrue(ls.Count() > 0);8 }9 }
直接上圖吧,最直接
當然在這條件和排序都是可以進行多次組合如下:
1 [TestMethod]2 public void TestConditionMethod()3 {4 using (var context = DBHelper.GetContext())5 {6 var ls = context.Select<Application>().Where(p => p.ApplicationID == 1000 || p.CategoryID == 1).And(c => c.Owner == "CNLIFAN").OrderBy(p => p.ApplicationID).QueryMany();7 Assert.IsTrue(ls.Count() > 0);8 }9 }
2.Lambda指定條件、動態指定更新的字段,以防更新額外字段
1 public void UpdateMethod()2 {3 using (var context = DBHelper.GetContext())4 {5 var item = context.Update<MobileForTest>().Set(new { MobileHolder = "TEST-10", MobilePhone = "18923456789" }).Where(p => p.ID == 1).Execute();6 Assert.IsTrue(item > 0);7 }8 }
3.查詢語句嵌套,最終合并成一個大的SQL執行
1 public void Delete3Method()2 {3 using (var context = DBHelper.GetContext())4 {5 var item = context.Delete<MobileForTest>().WhereIsIn(p => p.ID, context.Select<Application>(p => p.ApplicationID == 10001).Select(p => p.CategoryID)).Execute();6 Assert.IsTrue(item > 0);7 }8 }
1 [TestMethod]2 public void SQLMethod13()3 {4 using (var context = DBHelper.GetContext())5 {6 var item = context.Select<MobileForTest>().WhereNotIn(p => p.ID, context.Select<Application>(p => p.ApplicationID == 10001).Select(p => p.CategoryID)).QuerySingle();7 Assert.IsTrue(item.ID == 1);8 }9 }
1 [TestMethod]2 public void SQLMethod14()3 {4 using (var context = DBHelper.GetContext())5 {6 var curID = context.Select<MobileForTest>(p => p.ID == 1).Select(item => new { ID = item.ID, Mobile = item.MobilePhone }).QueryDynamicSingle();7 var pkID = curID;8 }9 }
靈活指定返回想要的字段
4.方便的使用事物
1 [TestMethod] 2 public void UseTransactionMethod() 3 { 4 var pkid = 1; 5 var model = new MobileForTest { MobileHolder = "TEST-linfengFang", MobilePhone = "18911112222", Status = 0 }; 6 using (var context = DBHelper.GetContext().UseTransaction(true)) 7 { 8 context.Insert<MobileForTest>(model).Execute(); 9 var item = context.Sql("UPDATE MobileForTest SET MobileHolder = 'SQLMethod-linfeng' WHERE ID=@ID", new { ID = pkid }).Execute();10 context.Commit();11 }12 }
5.在項目中更加使用的分頁功能
1 [TestMethod] 2 public void SQLPage() 3 { 4 var record = 0; 5 using (var context = DBHelper.GetContext()) 6 { 7 var pageIndex = 0; 8 var pageSize = 3; 9 context.Select<MobileForTest>(p => p.MobilePhone == "18911112222" && p.ID != 1).QueryPage(pageIndex, pageSize, out record);10 Assert.IsTrue(record > 0);11 }12 }13 [TestMethod]14 public void SQLPage2()15 {16 var record = 0;17 using (var context = DBHelper.GetContext())18 {19 var pageIndex = 0;20 var pageSize = 3;21 var ls = context.Sql("select * from MobileForTest").QueryPage<MobileForTest>(pageIndex, pageSize, out record);22 Assert.IsTrue(record > 0);23 }24 }
相對其他的ORM,做了以上的封裝,還有大部分基本的功能與其他的語法,也比較類似,所以開篇沒提到,目前都已經支持了。歡迎大家任意使用,如果有遇到問題,都可以給我留言,
代碼在后續會版本穩定和代碼規整后,會考慮進行開源。
新聞熱點
疑難解答