微型 ORM 的第一篇 DapperLambda发布

引言:因为接触过多个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

微型 ORM 的第一篇 DapperLambda发布

或是 在引用->管理NuGet程序包中搜索 DapperLambda

微型 ORM 的第一篇 DapperLambda发布

二.dll引用入到我们的数据业务层.

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

它是我们与数据库操作中的上下文,所有的有关数据操作都调用它下面的方法;目前只针对MSSQL进行了单元测试,后续会继续完善在其他数据库中使用,所以非MSSQL,使用请谨慎

     public static DbContext GetContext()
{
string connectionStr = "server=(local);User ID=sa;Password=password01!;Database=LocalDB;Persist Security Info=True;Pooling=true;Max Pool Size=700";
return new DbContext().ConnectionString(connectionStr, DatabaseType.MSSQLServer);
}

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

  1. 支持Lamba设置条件和排序 ,最终会得到一个SQL,(关于如果监控执行过程,后续会介绍) 执行返回得到IEnumerable<T>
   [TestMethod]
public void TestOrderByMethod()
{
using (var context = DBHelper.GetContext())
{
var ls = context.Select<Application>().Where(p => p.CategoryID == ).OrderBy(p => p.ApplicationID).QueryMany();
Assert.IsTrue(ls.Count() > );
}
}

直接上图吧,最直接微型 ORM 的第一篇 DapperLambda发布

当然在这条件和排序都是可以进行多次组合如下:

   [TestMethod]
public void TestConditionMethod()
{
using (var context = DBHelper.GetContext())
{
var ls = context.Select<Application>().Where(p => p.ApplicationID == || p.CategoryID == ).And(c => c.Owner == "CNLIFAN").OrderBy(p => p.ApplicationID).QueryMany();
Assert.IsTrue(ls.Count() > );
}
}

2.Lambda指定条件、动态指定更新的字段,以防更新额外字段

  public void UpdateMethod()
{
using (var context = DBHelper.GetContext())
{
var item = context.Update<MobileForTest>().Set(new { MobileHolder = "TEST-10", MobilePhone = "" }).Where(p => p.ID == ).Execute();
Assert.IsTrue(item > );
}
}

3.查询语句嵌套,最终合并成一个大的SQL执行

   public void Delete3Method()
{
using (var context = DBHelper.GetContext())
{
var item = context.Delete<MobileForTest>().WhereIsIn(p => p.ID, context.Select<Application>(p => p.ApplicationID == ).Select(p => p.CategoryID)).Execute();
Assert.IsTrue(item > );
}
}
    [TestMethod]
public void SQLMethod13()
{
using (var context = DBHelper.GetContext())
{
var item = context.Select<MobileForTest>().WhereNotIn(p => p.ID, context.Select<Application>(p => p.ApplicationID == ).Select(p => p.CategoryID)).QuerySingle();
Assert.IsTrue(item.ID == );
}
}
   [TestMethod]
public void SQLMethod14()
{
using (var context = DBHelper.GetContext())
{
var curID = context.Select<MobileForTest>(p => p.ID == ).Select(item => new { ID = item.ID, Mobile = item.MobilePhone }).QueryDynamicSingle();
var pkID = curID;
}
}

灵活指定返回想要的字段

4.方便的使用事物

  [TestMethod]
public void UseTransactionMethod()
{
var pkid = ;
var model = new MobileForTest { MobileHolder = "TEST-linfengFang", MobilePhone = "", Status = };
using (var context = DBHelper.GetContext().UseTransaction(true))
{
context.Insert<MobileForTest>(model).Execute();
var item = context.Sql("UPDATE MobileForTest SET MobileHolder = 'SQLMethod-linfeng' WHERE ID=@ID", new { ID = pkid }).Execute();
context.Commit();
}
}

5.在项目中更加使用的分页功能

    [TestMethod]
public void SQLPage()
{
var record = ;
using (var context = DBHelper.GetContext())
{
var pageIndex = ;
var pageSize = ;
context.Select<MobileForTest>(p => p.MobilePhone == "" && p.ID != ).QueryPage(pageIndex, pageSize, out record);
Assert.IsTrue(record > );
}
}
[TestMethod]
public void SQLPage2()
{
var record = ;
using (var context = DBHelper.GetContext())
{
var pageIndex = ;
var pageSize = ;
var ls = context.Sql("select * from MobileForTest").QueryPage<MobileForTest>(pageIndex, pageSize, out record);
Assert.IsTrue(record > );
}
}

相对其他的ORM,做了以上的封装,还有大部分基本的功能与其他的语法,也比较类似,所以开篇没提到,目前都已经支持了。欢迎大家任意使用,如果有遇到问题,都可以给我留言,

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

上一篇:python----------进程、线程、协程


下一篇:Struts2 Result 类型和对应的用法详解 2