引言:因?yàn)榻佑|過(guò)多個(gè)ORM,但使用的時(shí)候都遇到了各自的一些不夠理想的地方,從最早開(kāi)始開(kāi)始公司自己分裝的,到后面用EF,以及Dapper和DapperExtensions 到現(xiàn)在用的FluentData,就說(shuō)說(shuō)我自己的使用體驗(yàn),在這幾個(gè)相比之下,Dapper應(yīng)該是最輕量級(jí),而且性能也是最好的,但是相對(duì)比較簡(jiǎn)單了點(diǎn)。EF的最新版也沒(méi)去使用,所以現(xiàn)在不是很了解,EF在這幾個(gè)相比一下,功能是最強(qiáng)大的,但是啟動(dòng)加載慢,以及復(fù)雜的功能,后續(xù)人優(yōu)化麻煩。FluentData 怎么說(shuō)呢,用的都挺好用,而且語(yǔ)法是最容易讓人理解,但是還是不支持Lambda。基于以上這些,所以萌生了我自己動(dòng)手做一個(gè)ORM,性能能跟Dapper比肩,語(yǔ)法要簡(jiǎn)單,容易理解,所以很多地方借鑒了FluentData ,但是內(nèi)部的邏輯是完全不一樣的。
總的來(lái)講的話, DapperLambda 就是Dapper和DapperExtensions的二次分裝,使用語(yǔ)法和FluentData基本類似,并加上了Lambda.原則:封裝和豐富這些功能不是為了不寫(xiě)SQL,而是做到簡(jiǎn)單的SQL,不希望在開(kāi)發(fā)中重復(fù)寫(xiě)。。。
下面我將介紹在開(kāi)發(fā)過(guò)程中的運(yùn)用.
一:下載該項(xiàng)目并且引用DapperLambda.dll,在VS的命令窗口中執(zhí)行 Install-Package DapperLambda
或是 在引用->管理NuGet程序包中搜索 DapperLambda
二.dll引用入到我們的數(shù)據(jù)業(yè)務(wù)層.
它是我們與數(shù)據(jù)庫(kù)操作中的上下文,所有的有關(guān)數(shù)據(jù)操作都調(diào)用它下面的方法;目前只針對(duì)MSSQL進(jìn)行了單元測(cè)試,后續(xù)會(huì)繼續(xù)完善在其他數(shù)據(jù)庫(kù)中使用,所以非MSSQL,使用請(qǐng)謹(jǐn)慎
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 }
直接上圖吧,最直接
當(dāng)然在這條件和排序都是可以進(jìn)行多次組合如下:
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指定條件、動(dòng)態(tài)指定更新的字段,以防更新額外字段
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.查詢語(yǔ)句嵌套,最終合并成一個(gè)大的SQL執(zhí)行
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.在項(xiàng)目中更加使用的分頁(yè)功能
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 }
相對(duì)其他的ORM,做了以上的封裝,還有大部分基本的功能與其他的語(yǔ)法,也比較類似,所以開(kāi)篇沒(méi)提到,目前都已經(jīng)支持了。歡迎大家任意使用,如果有遇到問(wèn)題,都可以給我留言,
代碼在后續(xù)會(huì)版本穩(wěn)定和代碼規(guī)整后,會(huì)考慮進(jìn)行開(kāi)源。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注