DapperLambda发布

来源:互联网 发布:php require use 编辑:程序博客网 时间:2024/03/29 20:12

引言:因为接触过多个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引用入到我们的数据业务层.

1.)创建并且初始化一个DbContext.

   它是我们与数据库操作中的上下文,所有的有关数据操作都调用它下面的方法;目前只针对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         }
复制代码
复制代码

2.)接下来就正式开始介绍DapperLambda的语法,因为部分语法都一致,所以在第一篇就不介绍了,后续都有详细介绍,就挑几个不一样的了解一下

  1. 支持Lamba设置条件和排序 ,最终会得到一个SQL,(关于如果监控执行过程,后续会介绍) 执行返回得到IEnumerable<T>
复制代码
复制代码
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,做了以上的封装,还有大部分基本的功能与其他的语法,也比较类似,所以开篇没提到,目前都已经支持了。欢迎大家任意使用,如果有遇到问题,都可以给我留言,

代码在后续会版本稳定和代码规整后,会考虑进行开源。

0 0
原创粉丝点击