7

.NET ORM框架HiSql实战-第一章-集成HiSql - pengxianyuan

 1 year ago
source link: https://www.cnblogs.com/pengxianyuan/p/16427131.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

.NET ORM框架HiSql实战-第一章-集成HiSql

一、引言

做.Net这么多年,出现了很多很多ORM框架,比如Dapper,Sqlsugar,Freesql等等。在之前的项目中,用到的ORM框架也大多数是这几个老牌的框架。
不过最近园子关于.NET ORM HiSql的讨论挺多的,本系列将通过不断学习 HiSql官网教程,尝试将之前使用SqlSuger ORM的一个项目,使用HiSql框架实现相关功能,看看hisql能带给我们哪些惊喜。
c# 国内外ORM 框架 dapper efcore sqlsugar freesql hisql sqlserver数据常规插入测试性能对比对比

HiSql GitGub地址
HiSql官网教程
项目介绍:项目是一个通用的后台管理系统,包含菜单管理、权限管理、组织架构、用户管理等等。
数据库采用SqlServer 2016;前端使用Element-UI;后端采用.Net5 Web Api。

二、集成HiSql到项目

  1. 安装 HiSql 核心包、 HiSql.SqlServer。
    打开管理NuGet程序包
安装HiSql 核心包、 HiSql.SqlServer

2、在项目中新建类文件 HiSqlSetupExtension.cs, 用于注入数据库配置,hisql数据库访问对象。

using HiSql;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;

namespace H.CRM.Action.API.Helper
{

    public static class HiSqlSetupExtension
    {
        public static IServiceCollection AddHiSqlSetup(this IServiceCollection services)
        {
            //注入HiSqlConfig
            services.AddTransient<HiSqlConfig>();
            //注入HiSqlClient
            services.AddTransient<HiSqlClient>((d) =>
            {
                var config = d.GetService<HiSqlConfig>();
                var hisql = new HiSqlClient(config);
                return hisql;
            });
            return services;
        }
    }

    class HiSqlConfig : ConnectionConfig
    {
        static readonly NLog.Logger logger = NLog.LogManager.GetLogger("HiSqlSetup");
        public HiSqlConfig(IConfiguration configuration)
        {
            DbType = DBType.SqlServer;
            DbServer = "HISQL";
            ConnectionString = configuration.GetSection("ConnectionStrings:Admin").Value;
            Schema = "dbo";
            SqlExecTimeOut = 1000 * 5;
            AppEvents = new AopEvent()
            {
                OnDbDecryptEvent = (connstr) =>
                {
                    //解密连接字段
                    return connstr;
                },
                OnLogSqlExecuting = (sql, param) =>
                {
                    //sql执行前 日志记录 (异步)
#if DEBUG
                    logger.Trace($"执行前sql:{sql} time:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff")}");
#endif
                },
                OnLogSqlExecuted = (sql, param) =>
                {
#if DEBUG
                    //sql执行后 日志记录 (异步)
                    logger.Trace($"执行后sql:{sql} time:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff")}");
#endif
                },
                OnSqlError = (sqlEx) =>
                {
                    //sql执行错误后 日志记录 (异步)
                    logger.Error($"执行错误:{sqlEx.Message} sql:{sqlEx.Sql} time:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff")}");
                },
                OnTimeOut = (int timer) =>
                {
                    //logger.Trace($"执行超时:{timer} time:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff")}");
                }
            };
        }
    }

}

3、在 Startup 中的ConfigureServices方法中,添加 hisql的使用。

//注入Hisql相关
            services.AddHiSqlSetup();
493017-20220630154853434-1222305133.png

4、新建 控制器 HiSqlController,添加初始化方法。

  using Microsoft.AspNetCore.Mvc;
using HiSql;
using System.Linq;

namespace HSMB.Admin.WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class HiSqlController : ControllerBase
    {
        private readonly HiSqlClient sqlClient;
       
        public HiSqlController(         
             HiSqlClient hiSqlClient
            )
        {
            this.sqlClient = hiSqlClient;
        }
        [HttpGet, HttpPost]
        public IActionResult Install()
        {
            sqlClient.CodeFirst.InstallHisql();
            var tables = sqlClient.DbFirst.GetTables().ToList().Where(t=>t.TabName.StartsWith("H"));
            return new JsonResult(tables);
        }
    }
}

5、启动项目后,访问 项目地址 http://localhost:8868/api/hisql/Install 初始化hisql。
如图表示初始化成功,同时在数据库也可以看到,系统创建了下图的4个基础表:

493017-20220630152122650-100414172.png

1.Hi_TabModel #表结构信息主表
2.Hi_FieldModel #表结构信息明细表
3.Hi_Domain #数据域
4.Hi_DataElement #数据元素

到此,项目就完成了HiSql的引入了,后面就可以愉快的使用HiSql各个功能。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK