Fast Framework
项目地址 https://gitee.com/China-Mr-zhong/Fast.Framework
Author Mr-zhong
Wechat 850856667
Email [email protected]
QQ群 693979005
一、前言
1.为了实现快速开发,省去编写大量Sql时间,更好的面向对象编程由此诞生了 Fast Framework
2.Fast Framework 是一个基于NET6.0 封装的轻量级 ORM 框架 支持数据库 SqlServer Oracle MySql PostgreSql Sqlite 由于底层使用System.Data.Common 抽象类封装 理论支持所有Ado.Net 实现的类库,差异部分可能需要额外处理。
3.框架 优点: 体积小 流畅API 使用更加简单 性能高 缺点:不具备有自动建库建表迁移等复杂的功能 由于不同数据库差异较大 实现较为复杂 所以暂时不考虑实现
二、项目明细 (后缀为Test均为测试项目)
- Fast.Framework
- Fast.Framework.CustomAttribute
- Fast.Framework.Extensions
- Fast.Framework.Interfaces
- Fast.Framework.Logging
- Fast.Framework.Models
- Fast.Framework.Utils
三、快速入门
-
手动创建
var options = new DefaultDbOptions() //数据选项
{
DbType = DbType.MySQL,
ProviderName = "MySqlConnector",
FactoryName = "MySqlConnector.MySqlConnectorFactory,MySqlConnector",
ConnectionStrings = "server=localhost;database=Test;user=root;pwd=123456789;port=3306;min pool size=3;max pool size=100;connect timeout =30;AllowLoadLocalInfile=True;"
};
var ado = new Ado(options);//原生Ado
var db = new DbContext(ado);//数据库上下文
-
依赖注入
var builder = WebApplication.CreateBuilder(args);
// 正式项目请用配置文件注入,为了测试方便直接引用实现类库
builder.Services.AddScoped, Ado>();
builder.Services.AddScoped, DbContext>();
-
加载Json配置文件
builder.Services.Configure(configuration.GetSection("DbFactory:MySqlDb"));
-
Json 格式
"DbFactory": {
"MySQLDb": {
"DbType": "MySQL",
"ProviderName": "MySqlConnector",
"FactoryName": "MySqlConnector.MySqlConnectorFactory,MySqlConnector",
"ConnectionStrings": "server=localhost;database=Test;user=root;pwd=123456789;port=3306;min pool size=3;max pool size=100;connect timeout =30;"
},
"SQLServerDb": {
"DbType": "SQLServer",
"ProviderName": "System.Data.SqlClient",
"FactoryName": "System.Data.SqlClient.SqlClientFactory,System.Data",
"ConnectionStrings": "server=localhost;database=Test;user=sa;pwd=123456789;min pool size=3;max pool size=100;connect timeout =30;"
},
"OracleDb": {
"DbType": "Oracle",
"ProviderName": "Oracle.ManagedDataAccess.Client",
"FactoryName": "Oracle.ManagedDataAccess.Client.OracleClientFactory,Oracle.ManagedDataAccess",
"ConnectionStrings": "data source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=ORCL)));user id=system;password=Oracle2022;min pool size=3;max pool size=100;connect timeout =30;"
},
"PostgreSQLDb": {
"DbType": "PostgreSQL",
"ProviderName": "Npgsql",
"FactoryName": "Npgsql.NpgsqlFactory,Npgsql",
"ConnectionStrings": "host=localhost;database=test;userid=postgres;pwd=123456789;port=5432;"
},
"SQLiteDb": {
"DbType": "SQLite",
"ProviderName": "System.Data.SQLite",
"FactoryName": "System.Data.SQLite.SQLiteFactory,System.Data.SQLite",
"ConnectionStrings": "data source=mysqlite.db;pooling=true;"
}
}
-
Controller 构造方法注入已注册的DbContext对象
namespace Fast.Framework.Web.Test.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class ProductController : ControllerBase
{
///
/// 数据库
///
private readonly IDbContext db;
///
/// 日志
///
private readonly ILogger logger;
///
/// 构造方法
///
///
public ProductController(ILogger logger, IDbContext db)
{
this.db = db;
this.logger = logger;
}
}
}
-
示例
#region 增 删 改
//实体对象插入
{
var result = await db.Insert(new ProductModel() { ProductCode = "1001", ProductName = "测试产品1" }).ExecuteAsync();
Console.WriteLine(result);
}
//实体对象插入并返回自增ID 仅支持 SQLServer Mysql Sqlite
{
var result = await db.Insert(new ProductModel() { ProductCode = "1001", ProductName = "测试产品1" }).ExecuteReturnIdentityAsync();
Console.WriteLine(result);
}
//实体对象列表插入 列表插入不支持批量返回自增ID
{
var list = new List();
list.Add(new ProductModel() { ProductCode = "1001", ProductName = "测试产品1" });
list.Add(new ProductModel() { ProductCode = "1002", ProductName = "测试产品2" });
var result = await db.Insert(list).ExecuteAsync();
Console.WriteLine(result);
}
//匿名对象插入 必须使用 As 方法显示指定表名称
{
var result = db.Insert(new { ProductCode = "1001", ProductName = "测试产品1" }).As("Product").ExecuteAsync();
Console.WriteLine(result);
}
//匿名对象列表插入 必须使用As 显示指定表名称
{
var list = new List