DatabaseManager实现数据库插件组件化

This commit is contained in:
FalconFly 2024-01-04 14:51:16 +08:00
parent 900c55a9c9
commit 0874e17cc2
3 changed files with 86 additions and 0 deletions

View File

@ -0,0 +1,31 @@
using Microsoft.Extensions.Logging;
using SqlSugar;
using System;
namespace Falcon.SugarApi.DatabaseManager
{
/// <summary>
/// 连接配置
/// </summary>
public class ConnectionConfigWallpaper:ConnectionConfig
{
/// <summary>
/// 构造连接配置
/// </summary>
/// <param name="def">数据库定义</param>
public ConnectionConfigWallpaper(DatabaseDefines def) {
base.ConfigId = def.Name;
base.ConnectionString = def.ConnectionString;
foreach(var dt in Enum.GetNames(typeof(DbType))) {
if(dt.ToLower() == def.DbType.ToLower()) {
base.DbType = (DbType)Enum.Parse(typeof(DbType),dt);
break;
}
}
base.IsAutoCloseConnection = true;
base.InitKeyType = InitKeyType.Attribute;
}
}
}

View File

@ -0,0 +1,28 @@
using SqlSugar;
namespace Falcon.SugarApi.DatabaseManager
{
/// <summary>
/// 数据库定义
/// </summary>
public class DatabaseDefines
{
/// <summary>
/// 数据库名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 是否记录数据库log
/// </summary>
public bool UseLogger { get; set; }
/// <summary>
/// 数据库类型
/// </summary>
public string DbType { get; set; }
/// <summary>
/// 链接字符串
/// </summary>
public string ConnectionString { get; set; }
}
}

View File

@ -0,0 +1,27 @@
using Microsoft.Extensions.Logging;
using SqlSugar;
using System;
namespace Falcon.SugarApi.DatabaseManager
{
/// <summary>
/// 数据库上下文基类
/// </summary>
public abstract class DbContextBase:SqlSugarClient
{
/// <summary>
/// 保存数据库日志
/// </summary>
public ILogger Logger { get; set; }
/// <summary>
/// 使用链接配置构造数据库链接
/// </summary>
/// <param name="config">数据库配置</param>
/// <param name="service">服务提供器</param>
public DbContextBase(ConnectionConfig config,IServiceProvider service) : base(config) {
this.Logger = service.GetService(typeof(ILogger<>).MakeGenericType(GetType())) as ILogger ?? throw new NullReferenceException("ILogger");
}
}
}