Fast.Framework ORM 试用

简介

Fast.Framework 是一款基于 .NET 6 封装的轻量级ORM框架,支持多种数据库(SQL Server、Oracle、MySQL、PostgreSQL、SQLite)。

优点

  1. 性能好
  2. 使用简单

如何使用

1. 安装

NuGet搜索Fast.Framework并安装最新版本

2. 创建DbContext对象

DbContext是轻量级的,可以频繁创建,一个线程创建一个DbContext,不要跨线程使用。

private IDbContext GetDbContext()
{
    IDbContext _db = new DbContext(new List<DbOptions>() {
            new DbOptions()
            {
                DbId = "1",
                DbType = DbType.MySQL,
                ProviderName = "MySqlConnector",
                FactoryName = "MySqlConnector.MySqlConnectorFactory,MySqlConnector",
                ConnectionStrings = "Data Source=localhost;Port=3306;User ID=root;Password=123456;Initial Catalog=fast_framework_test;Charset=utf8mb4;SslMode=none;Allow User Variables=True;connection timeout=600;"
            }
        });

    if (_printSql)
    {
        _db.Aop.DbLog = (sql, dp) =>
        {
            Console.WriteLine($"执行Sql:{sql}");
            if (dp != null)
            {
                foreach (var item in dp)
                {
                    Console.WriteLine($"参数名称:{item.ParameterName} 参数值:{item.Value}");
                }
            }
        };
    }

    return _db;
}

实体类

实体类原生支持微软特性,兼容Entity Framework。

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Models
{
    /// <summary>
    /// 用户表
    /// </summary>
    [Serializable]
    [Table("sys_user")]
    public partial class SysUser
    {

        /// <summary>
        /// 主键
        /// </summary>
        [Key]
        [Column("id")]
        public long Id { get; set; }

        /// <summary>
        /// 用户名
        /// </summary>
        [Column("user_name")]
        public string UserName { get; set; }

        /// <summary>
        /// 用户姓名
        /// </summary>
        [Column("real_name")]
        public string RealName { get; set; }

        /// <summary>
        /// 用户密码
        /// </summary>
        [Column("password")]
        public string Password { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        [Column("remark")]
        public string Remark { get; set; }

        /// <summary>
        /// 创建者ID
        /// </summary>
        [Column("create_userid")]
        public string CreateUserid { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        [Column("create_time")]
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 更新者ID
        /// </summary>
        [Column("update_userid")]
        public string UpdateUserid { get; set; }

        /// <summary>
        /// 更新时间
        /// </summary>
        [Column("update_time")]
        public DateTime? UpdateTime { get; set; }

    }
}

.NET 6 依赖注入FastDbContext

配置文件

"DbConfig": [
  {
    "DbId": 1,
    "DbType": "MySQL",
    "IsDefault": true,
    "ProviderName": "MySqlConnector",
    "FactoryName": "MySqlConnector.MySqlConnectorFactory,MySqlConnector",
    "ConnectionStrings": "Data Source=localhost;Port=3306;User ID=root;Password=123456;Initial Catalog=fast_framework_test;Charset=utf8mb4;SslMode=none;Allow User Variables=True;connection timeout=600;"
  }
]

注入代码

//注入FastDbContext
builder.Services.Configure<List<DbOptions>>(builder.Configuration.GetSection("DbConfig"));
builder.Services.AddFastDbContext();

循环插入

await db.Ado.BeginTranAsync();
foreach (SysUser user in userList)
{
    await db.Insert(user).ExceuteAsync();
}
await db.Ado.CommitTranAsync();

批量插入

await GetDbContext().Insert(userList).ExceuteAsync();

循环修改

await db.Ado.BeginTranAsync();
foreach (SysUser user in userList)
{
    await db.Update(user).ExceuteAsync();
}
await db.Ado.CommitTranAsync();

批量修改

await GetDbContext().Update(userList).ExceuteAsync();

条件查询

Pagination pagination = new Pagination();
pagination.Page = 10;
pagination.PageSize = 200;

var list = await _db.Query<SysUser>()
    .Where(t => t.Id > 20 && t.RealName.Contains("测试"))
    .OrderBy(t => t.CreateTime, "desc")
    .OrderBy(t => t.Id).ToPageListAsync(pagination);

分组聚合统计查询

这个写法很优雅。

var list = await db.Query<BsOrderDetail>()
    .LeftJoin<BsOrder>((d, o) => o.Id == d.OrderId)
    .GroupBy((d, o) => new { o.Id, o.OrderTime, o.Remark })
    .Having((d, o) => SqlFunc.Sum(d.Quantity * d.Price) > 1000)
    .Select((d, o) => new
    {
        o.Id,
        o.OrderTime,
        o.Remark,
        Amount = SqlFunc.Sum(d.Quantity * d.Price)
    }).ToListAsync();

性能测试

性能测试使用Winform测试
与其它ORM的对比请参考《ORM增删改查并发性能测试2

常规测试

并发查询

并发插入

JMeter性能测试

经过第一轮10000次请求的充分预热后,取第二轮10000次请求的测试结果。

JMeter设置

吞吐量

吞吐量测试结果,虽然比不上EFCore,但比SqlSugar和FreeSql还要高一点。

响应时间

结论

感觉还是非常快的,性能非常优异。

posted @ 2022-09-16 15:59  0611163  阅读(515)  评论(8编辑  收藏  举报