基于NET|基于NET 6.0 封装的 Fast.Framework
Fast Framework
项目地址 https://gitee.com/China-Mr-zhong/Fast.Framework
Author Mr-zhong
Wechat 850856667
Email 850856667@qq.com
QQ群 693979005
一、前言
1.为了实现快速开发,省去编写大量Sql时间,更好的面向对象编程由此诞生了 Fast Framework
2.Fast Framework 是一个基于NET6.0 封装的轻量级 ORM 框架 支持数据库 SqlServer Oracle MySql PostgreSql Sqlite 由于底层使用System.Data.Common 抽象类封装 理论支持所有Ado.Net 实现的类库,差异部分可能需要额外处理。
【基于NET|基于NET 6.0 封装的 Fast.Framework】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=https://www.it610.com/article/(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
方法太多 写文档是个体力活 更多请看源码
推荐阅读
- OpenCV从入门到项目实战|OpenCV-Python实战(番外篇)——基于 Haar 级联的猫脸检测器
- 【译】ASP.NET|【译】ASP.NET Core 6 中的性能改进
- SpringBoot|SpringBoot 搭建基于 MinIO 的高性能存储服务
- ansible二进制部署kubernetes集群
- Kubernetes上安装Metrics-Server
- .NetCore|.NetCore Web Api 利用ActionFilterAttribute统一接口返回值格式及问题解析
- 基于mybatis|基于mybatis plus实现数据源动态添加、删除、切换,自定义数据源的示例代码
- 基于.Net的公务员线上学习考试系统的源代码和开发文档
- 数据可视化|基于ECharts数据可视化案例--世界疫情实时展示
- 国产化之 Arm64 CPU + 银河麒麟系统 安装 .NetCore