引言:由于接触过多个ORM,但使用的时候都遇到了各自的一些不够理想的地方,从最先开始开始公司本身分装的,到后面用EF,以及Dapper和DapperExtensions 到如今用的FluentData,就说说我本身的使用体验,在这几个相比之下,Dapper应该是最轻量级,并且性能也是最好的,可是相对比较简单了点。EF的最新版也没去使用,因此如今不是很了解,EF在这几个相比一下,功能是最强大的,可是启动加载慢,以及复杂的功能,后续人优化麻烦。FluentData 怎么说呢,用的都挺好用,并且语法是最容易让人理解,可是仍是不支持Lambda。基于以上这些,因此萌生了我本身动手作一个ORM,性能能跟Dapper比肩,语法要简单,容易理解,因此不少地方借鉴了FluentData ,可是内部的逻辑是彻底不同的。数据库
总的来说的话, DapperLambda 就是Dapper和DapperExtensions的二次分装,使用语法和FluentData基本相似,并加上了Lambda.原则:封装和丰富这些功能不是为了避免写SQL,而是作到简单的SQL,不但愿在开发中重复写。。。app
下面我将介绍在开发过程当中的运用.性能
一:下载该项目而且引用DapperLambda.dll,在VS的命令窗口中执行 Install-Package DapperLambda单元测试
或是 在引用->管理NuGet程序包中搜索 DapperLambda测试
二.dll引用入到咱们的数据业务层.优化
它是咱们与数据库操做中的上下文,全部的有关数据操做都调用它下面的方法;目前只针对MSSQL进行了单元测试,后续会继续完善在其余数据库中使用,因此非MSSQL,使用请谨慎spa
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 }
直接上图吧,最直接code
固然在这条件和排序都是能够进行屡次组合以下:server
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指定条件、动态指定更新的字段,以防更新额外字段blog
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,作了以上的封装,还有大部分基本的功能与其余的语法,也比较相似,因此开篇没提到,目前都已经支持了。欢迎你们任意使用,若是有遇到问题,均可以给我留言,
代码在后续会版本稳定和代码规整后,会考虑进行开源。