Dapper.Common基于Dapper的开源LINQ超轻量扩展
Dapper.Common
Dapper.Common是基于Dapper的LINQ實(shí)現(xiàn),支持.net core,遵循Linq語法規(guī)則、鏈?zhǔn)秸{(diào)用、配置簡單、上手快,支持Mysql,Sqlserver(目前只實(shí)現(xiàn)了這兩個(gè)數(shù)據(jù)庫,實(shí)現(xiàn)其他數(shù)據(jù)庫也很輕松),支持單表,多表,自定義函數(shù)等功能。源碼及其簡單,直白,解析Lambda只有300行左右代碼。嚴(yán)格區(qū)分C#函數(shù)和數(shù)據(jù)庫函數(shù),你可以在表達(dá)式中調(diào)用C#函數(shù)(不推薦,推薦將計(jì)算結(jié)果保存到變量,在寫入lambda表達(dá)式),性能損失在表達(dá)式編譯:常量>變量>函數(shù)。損失多少可以通過ExpressionUtil.BuildExpression()來測試,幾萬次耗時(shí)百毫秒及別。
開源地址:https://github.com/1448376744/Dapper.Common
Nuget:Install-Package Dapper.Common -Version 1.5.0
0.Test
1.Mapper
2.Config
//在App啟動時(shí)執(zhí)行一次即可SessionFactory.AddDataSource(new DataSource()
{
Name = "mysql",
Source = () => new SqlConnection("connectionString"),
SourceType = DataSourceType.SQLSERVER,
UseProxy = true//使用Session的靜態(tài)代理實(shí)現(xiàn),記錄日志,執(zhí)行耗時(shí),線上環(huán)境建議關(guān)閉代理
});
//獲取數(shù)據(jù)庫上下文
using (var session = SessionFactory.GetSession("msql"))
{
//linq to sql
}
3.Insert
var entity = new User(){
CreateTime=DateTime.Now,
NickName="dapper",
};
//絕大部分接口可以設(shè)置condition已決定是否執(zhí)行,支持批量更新
session.From<User>().Insert(entity,condition:1>2);
//查看日志,如果出現(xiàn)異常,應(yīng)該在catch里,查看session.Loggers
var loggers = session.Loggers;
2.Update
var entity = new User(){
Id=2,
NickName="李四"
};
//更新所有字段(where id=2),支持批量,顯然除NickName之外將被更新成null
session.From<User>().Update(entity);
//更新部分字段
session.From<User>()
.Set(a => a.NickName, "李四", condition: true)//condition為true時(shí)更新該字段
.Set(a => a.Balance, a => a.Balance + 100)//余額在原來基礎(chǔ)增加100
.Where(a => a.Id.In(1,2,3))//將id為1,2,3的記錄進(jìn)行更新
.Update();
3.Delete
//刪除id>5||nick_name like '%da%'session.From<User>()
.Where(a=>a.Id>5||a.NickName.Like("da"))
.Delete();
4.Single
//查詢?nèi)孔侄?/span>var user1 = session.From<User>()
.Where(a=>a.Id==2)
.Single();
//查詢部分字段
var user2 = session.From<User>()
.Where(a => a.Id == 2)
.Single(s=>new
{
s.Id,
s.NickName
});
5.Select
//查詢:where id in(1,2,3)var list = session.From<User>()
.Where(a => a.Id.In("1,2,3".Split(',')))
.Select();
6.Where
//構(gòu)建動態(tài)查詢,condition: true執(zhí)行,通過condition選擇分支,多個(gè)where之間用 and 連接var list = session.From<User>()
.Where(a => a.Id.In(1, 2, 3), condition: true)
.Where(a => a.NickName.Like("da"), condition: false)
.Where(a => a.Id > 2 || (a.NickName.Like("da") && a.Balance > 50))
.Where("select * from user_bill where user_bill.user_id=user.id")//同樣可以當(dāng)作字符串拼接工具
.Select();
7.Function
/// <summary>/// 自定義函數(shù)
/// </summary>
public static class MySqlFun
{
//這里使用泛型并不是必須的,只用函數(shù)名在數(shù)據(jù)庫存在即可,泛型為了指定返回?cái)?shù)據(jù)類型
[Function]//Dapper.Common嚴(yán)格區(qū)分C#函數(shù)和數(shù)據(jù)庫函數(shù),一定要用該特性標(biāo)識數(shù)據(jù)庫函數(shù)
public static T COUNT<T>(T column)
{
return default(T);
}
[Function]
public static T MAX<T>(T column)
{
return default(T);
}
[Function]
public static T DISTINCT<T>(T column)
{
return default(T);
}
[Function]
public static T DATE<T>(T column)
{
return default(T);
}
}
8.GroupBy
var list = session.From<Order>().GroupBy(a => a.UserId)//多個(gè)條件可以new一個(gè)匿名對象,也可以并聯(lián)多個(gè)group
.Having(a => MySqlFun.COUNT(MySqlFun.DISTINCT(a.UserId)) > 10)//count(distinct(user_id))>10
.Select(s => new
{
s.UserId,
OrderCount = MySqlFun.COUNT(1L),//這里應(yīng)該返回long int,
MaxFee = MySqlFun.MAX(s.TotalFee)
});
9.Join
var list = session.From<Order, User>().Join((a, b) => a.UserId == b.Id, JoinType.Inner)
.GroupBy((a, b) => a.UserId)
.Having((a, b) => MySqlFun.COUNT(MySqlFun.DISTINCT(a.UserId)) > 10)//count(distinct(user_id))>10
.Select((a, b) => new
{
a.UserId,
b.NickName,
OrderCount = MySqlFun.COUNT(1L),//這里應(yīng)該返回long int,
MaxFee = MySqlFun.MAX(a.TotalFee)
});
10.SubQuery
var list = session.From<Order>().GroupBy(a => a.UserId)
.Having(a => MySqlFun.COUNT(MySqlFun.DISTINCT(a.UserId)) > 10)
.Select(a => new
{
a.UserId,
UserName=Convert.ToString("select nick_name from user where user.id=order.user_id"),//如果這個(gè)子查詢返回的是int:Convert.ToInt32(sql)
OrderCount = MySqlFun.COUNT(1L),//這里應(yīng)該返回long int【這就是為什么定義成泛型函數(shù)】,
MaxFee = MySqlFun.MAX(a.TotalFee)
});
11.Page
//分頁應(yīng)該寫在Where,Having,Group之后(如果有)var list = session.From<User>()
.Where(a=>a.NickName != null)
.Page(1,10,out long total)
.Select();
12.Take
var list = session.From<User>().Take(5)
.Select();
13.Skip
//從數(shù)據(jù)庫索引為1的位置(跳過1之前的記錄),獲取10var list = session.From<User>()
.Skip(1,10)
.Select();
14.Sum
var list= session.From<User>().Sum(s=>s.Balance*s.Id);
15.Exists
//內(nèi)部采用exist子查詢判斷滿足where條件的記錄是否存在var flag = seesion.From<User>()
.Where(a=>a.Id > 10)
.Exists();
16.OrderBy
var list1 = session.From<User>().Order(a=>a.Id)
.Select();
var list2 = session.From<User>()
.GroupBy(a => MysqlFun.DATE(a.CreateTime))
.OrderByDescending(a => MysqlFun.DATE(a.CreateTime))
.Select(s=>new
{
Date=MysqlFun.DATE(s.CreateTime),
Count = MysqlFun.Count(1L)
});
17.Filter
var user =new User (){
Id = 12
Balance = 50,
NickName = "張三",
CreateTime = Datetime.Now
};
//Filter會在Insert,Update,Select,過濾掉不想要的字段
//這將不會更新余額及創(chuàng)建時(shí)間
var row = session.From<User>()
.Filter(f=>new
{
f.CreateTime,
f.Balance,
})
.Update(user);
?18.Transaction
//獲取數(shù)據(jù)庫上下文ISession session = null;
try
{
session=SessionFactory.GetSession();
//開啟事務(wù)
session.Open(true);
//sql
//提交事務(wù)
session.Commit();
}
catch (Exception)
{
session?.Rollback();
throw;
}
finally
{
session?.Close();
}
原文地址:https://www.cnblogs.com/chaeyeon/p/11028480.html
.NET社區(qū)新聞,深度好文,歡迎訪問公眾號文章匯總?http://www.csharpkit.com?
總結(jié)
以上是生活随笔為你收集整理的Dapper.Common基于Dapper的开源LINQ超轻量扩展的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 从零开始制作 NuGet 源代码包(全面
- 下一篇: .NET 使用 ILRepack 合并多