Fast Framework
作者 Mr-zhong
代码改变世界....
一、前言
Fast Framework 基于NET6.0 封装的轻量级 ORM 框架 支持多种数据库 SqlServer Oracle MySql PostgreSql Sqlite
优点: 体积小、原生支持微软特性、流畅API、使用简单、性能高、模型数据绑定采用 Expression、强大的表达式解析、支持多种子查询可实现较为复杂查询、源代码可读性强、支持AOT 编译。
缺点:目前仅支持Db Frist
开源地址:https://github.com/China-Mr-zhong/Fast.Framework (唯一)
ps:权重随机算法、支持故障转移、故障回调(可做日志记录或通知)
一、appsettings.json 配置
{
"DbOptions": [
{
"DbId": "db_01",
"DbType": "MySQL",
"IsDefault": true,
"ConnectionStrings": "server=localhost;database=Test;user=root;pwd=123456789;port=3306;min pool size=0;max pool size=100;connect timeout=120;AllowLoadLocalInfile=true;",
"UseMasterSlaveSeparation": true, //使用主从分离 注意所有事务将强制走主库
"SlaveItems": [
{
"DbId": "A",
"Weight": 60,
"ConnectionStrings": "server=localhost;database=Test1;user=root;pwd=123456789;port=3306;min pool size=0;max pool size=100;connect timeout=120;AllowLoadLocalInfile=true;",
"Description": "A数据库"
},
{
"DbId": "B",
"Weight": 40,
"ConnectionStrings": "server=localhost;database=Test2;user=root;pwd=123456789;port=3306;min pool size=0;max pool size=100;connect timeout=120;AllowLoadLocalInfile=true;",
"Description": "B数据库"
}
],
"Description": "主库连接配置"
}
]
}
二、 Program (入口配置)
builder.Services.Configure<List<DbOptions>>(builder.Configuration.GetSection("DbOptions"));//注册Options接口
builder.Services.AddFastDbContext();//添加上下文
四、使用示例
db.Aop.SlaveDbFault = (options, ex) =>
{
//故障回调
Console.WriteLine($"从库ID:{options.DbId} 发生故障!!! 异常信息:{ex.Message}");
};
var data = db.Query<Product>().First();
Console.WriteLine(Json.Serialize(data));
Console.WriteLine();
Console.WriteLine($"从库索引:{db.Ado.CurrentSlaveDbIndex} 从库ID:{db.Ado.SlaveDbOptions.DbId}");
其它很棒的功能(原创基于作用域概念设计的子查询)
-
Join子查询
-
示例代码
var subQuery1 = db.Query<Product>().Select(s => new { s.ProductId, s.CategoryId, s.ProductCode, s.ProductName, s.DeleteMark }); var data = db.Query<Category>().InnerJoin(subQuery1, (a, b) => a.CategoryId == b.CategoryId).ToList();
-
执行后Sql
SELECT p1.`CategoryId`,p1.`CategoryName`,p2.`ProductId`,p2.`ProductCode`,p2.`ProductName`,p2.`DeleteMark` FROM `Category` `p1` INNER JOIN ( SELECT `ProductId` AS `ProductId`,`CategoryId` AS `CategoryId`,`ProductCode` AS `ProductCode`,`ProductName` AS `ProductName`,`DeleteMark` AS `DeleteMark` FROM `Product` ) `p2` ON ( `p1`.`CategoryId` = `p2`.`CategoryId` )
-
-
From子查询
-
示例代码
var subQuery2 = db.Query<Product>().Select(s=>new { s.ProductId, s.CategoryId, s.ProductCode, s.ProductName, s.DeleteMark }); var data = db.Query(subQuery2).ToList();
-
执行后Sql
SELECT * FROM ( SELECT `ProductId` AS `ProductId`,`CategoryId` AS `CategoryId`,`ProductCode` AS `ProductCode`,`ProductName` AS `ProductName`,`DeleteMark` AS `DeleteMark` FROM `Product` ) x
-
-
Select子查询
-
示例代码
var data = db.Query<Product>().Select(s => new { CategoryName = db.Query<Category>().Where(w => w.CategoryId == 1).Select(s => s.CategoryName).First() }).First();
-
执行后Sql
SELECT ( SELECT `p2`.`CategoryName` FROM `Category` `p2` WHERE ( `p2`.`CategoryId` = 1 ) Limit 1 ) AS `CategoryName` FROM `Product` `p1` Limit 1
-
-
Select嵌套查询
-
示例代码
var data1 = db.Query<Product>().Select(s => new { NestedQuery = db.Query<Category>().Where(w => w.CategoryId == s.CategoryId).ToList() }).First(); var data2 = db.Query<Product>().Where(w => w.ProductId == 1).Select(s => new { NestedQuery = db.Query<Category>().Where(w => w.CategoryId == s.CategoryId).ToList() }).First();
-
执行后Sql
//内部机制主查询有结果才执行嵌套查询,懒加载实现 SELECT 0 AS `fast_args_index_0` FROM `Product` `p1` Limit 1 -------------------------------------------------------------------------- SELECT p2.`CategoryId`,p2.`CategoryName`,p1.`ProductId`,p1.`ProductCode`,p1.`ProductName`,p1.`DeleteMark`,p1.`CreateTime`,p1.`ModifyTime`,p1.`Custom1`,p1.`Custom2`,p1.`Custom3`,p1.`Custom4`,p1.`Custom5`,p1.`Custom6`,p1.`Custom7`,p1.`Custom8`,p1.`Custom9`,p1.`Custom10`,p1.`Custom11`,p1.`Custom12` FROM `Category` `p2` RIGHT JOIN `Product` `p1` ON ( `p2`.`CategoryId` = `p1`.`CategoryId` ) SELECT 0 AS `fast_args_index_0` FROM `Product` `p1` WHERE ( `p1`.`ProductId` = 1 ) Limit 1
-
-
Where子查询
-
示例代码
var data = db.Query<Category>().Where(w => w.CategoryId == 1 && db.Query<Product>().Where(w => w.CategoryId == 1).Select(s => 1).Any()).First();//Any支持取反
-
执行后Sql
SELECT p1.`CategoryId`,p1.`CategoryName` FROM `Category` `p1` WHERE ( ( `p1`.`CategoryId` = 1 ) AND EXISTS ( SELECT 1 FROM `Product` `p2` WHERE ( `p2`.`CategoryId` = 1 ) ) ) Limit 1
-
更多示例 https://www.cnblogs.com/China-Mr-zhong/p/17852177.html
标签:p2,p1,读写,db,Fast,ORM,Query,CategoryId,SELECT From: https://www.cnblogs.com/China-Mr-zhong/p/17879642.html