feat: 完成支持多db模式
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using SqlSugar;
|
||||
using ArgumentException = System.ArgumentException;
|
||||
|
||||
namespace Yi.Framework.SqlSugarCore.Abstractions
|
||||
{
|
||||
@@ -53,6 +54,5 @@ namespace Yi.Framework.SqlSugarCore.Abstractions
|
||||
/// 开启Saas多租户
|
||||
/// </summary>
|
||||
public bool EnabledSaasMultiTenancy { get; set; } = false;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using SqlSugar;
|
||||
|
||||
namespace Yi.Framework.SqlSugarCore.Abstractions
|
||||
{
|
||||
public interface ISqlSugarDbConnectionCreator
|
||||
{
|
||||
DbConnOptions Options { get; }
|
||||
Action<ISqlSugarClient> OnSqlSugarClientConfig { get; set; }
|
||||
Action<object, DataAfterModel> DataExecuted { get; set; }
|
||||
Action<object, DataFilterModel> DataExecuting { get; set; }
|
||||
Action<string, SugarParameter[]> OnLogExecuting { get; set; }
|
||||
Action<string, SugarParameter[]> OnLogExecuted { get; set; }
|
||||
Action<PropertyInfo, EntityColumnInfo> EntityService { get; set; }
|
||||
|
||||
ConnectionConfig Build(Action<ConnectionConfig>? action = null);
|
||||
void SetDbAop(ISqlSugarClient currentDb);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
using System.Reflection;
|
||||
using SqlSugar;
|
||||
|
||||
namespace Yi.Framework.SqlSugarCore;
|
||||
namespace Yi.Framework.SqlSugarCore.Abstractions;
|
||||
|
||||
public interface ISqlSugarDbContextDependencies
|
||||
{
|
||||
@@ -9,20 +9,31 @@ using Yi.Framework.SqlSugarCore.Abstractions;
|
||||
|
||||
namespace Yi.Framework.SqlSugarCore
|
||||
{
|
||||
public static class SqlsugarCoreExtensions
|
||||
public static class SqlSugarCoreExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 新增db对象,可支持多个
|
||||
/// </summary>
|
||||
/// <param name="service"></param>
|
||||
/// <param name="serviceLifetime"></param>
|
||||
/// <typeparam name="TDbContext"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static IServiceCollection AddYiDbContext<TDbContext>(this IServiceCollection service, ServiceLifetime serviceLifetime = ServiceLifetime.Transient) where TDbContext : class, ISqlSugarDbContextDependencies
|
||||
{
|
||||
service.Add(new ServiceDescriptor(typeof(ISqlSugarDbContextDependencies), typeof(TDbContext), serviceLifetime));
|
||||
service.AddTransient<ISqlSugarDbContextDependencies, TDbContext>();
|
||||
return service;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 新增db对象,可支持多个
|
||||
/// </summary>
|
||||
/// <param name="service"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <typeparam name="TDbContext"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static IServiceCollection AddYiDbContext<TDbContext>(this IServiceCollection service, Action<DbConnOptions> options) where TDbContext : class, ISqlSugarDbContextDependencies
|
||||
{
|
||||
service.Configure<DbConnOptions>(ops =>
|
||||
{
|
||||
options.Invoke(ops);
|
||||
});
|
||||
service.Configure<DbConnOptions>(options.Invoke);
|
||||
service.AddYiDbContext<TDbContext>();
|
||||
return service;
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using System.Reflection;
|
||||
using SqlSugar;
|
||||
using Volo.Abp.DependencyInjection;
|
||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||
|
||||
namespace Yi.Framework.SqlSugarCore;
|
||||
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
using System.Reflection;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Reflection;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SqlSugar;
|
||||
using Volo.Abp.Data;
|
||||
using Volo.Abp.DependencyInjection;
|
||||
using Volo.Abp.MultiTenancy;
|
||||
using Volo.Abp.Threading;
|
||||
using Volo.Abp.Users;
|
||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||
using Check = Volo.Abp.Check;
|
||||
@@ -17,6 +19,7 @@ namespace Yi.Framework.SqlSugarCore
|
||||
/// SqlSugar 客户端
|
||||
/// </summary>
|
||||
public ISqlSugarClient SqlSugarClient { get; private set; }
|
||||
|
||||
private IAbpLazyServiceProvider LazyServiceProvider { get; }
|
||||
|
||||
private ICurrentTenant CurrentTenant => LazyServiceProvider.LazyGetRequiredService<ICurrentTenant>();
|
||||
@@ -24,23 +27,29 @@ namespace Yi.Framework.SqlSugarCore
|
||||
|
||||
private ISerializeService SerializeService => LazyServiceProvider.LazyGetRequiredService<ISerializeService>();
|
||||
|
||||
private IEnumerable<ISqlSugarDbContextDependencies> SqlSugarDbContextDependencies=>LazyServiceProvider.LazyGetRequiredService<IEnumerable<ISqlSugarDbContextDependencies>>();
|
||||
private IEnumerable<ISqlSugarDbContextDependencies> SqlSugarDbContextDependencies =>
|
||||
LazyServiceProvider.LazyGetRequiredService<IEnumerable<ISqlSugarDbContextDependencies>>();
|
||||
|
||||
private static readonly ConcurrentDictionary<string, ConnectionConfig> ConnectionConfigCache = new();
|
||||
|
||||
public SqlSugarDbContextFactory(IAbpLazyServiceProvider lazyServiceProvider)
|
||||
{
|
||||
LazyServiceProvider = lazyServiceProvider;
|
||||
|
||||
var connectionString = GetCurrentConnectionString();
|
||||
|
||||
//获取连接配置操作,需要进行缓存
|
||||
var connectionConfig = BuildConnectionConfig(action: options =>
|
||||
var connectionConfig = ConnectionConfigCache.GetOrAdd(connectionString, (_) =>
|
||||
BuildConnectionConfig(action: options =>
|
||||
{
|
||||
options.ConnectionString = GetCurrentConnectionString();
|
||||
options.ConnectionString = connectionString;
|
||||
options.DbType = GetCurrentDbType();
|
||||
});
|
||||
}));
|
||||
SqlSugarClient = new SqlSugarClient(connectionConfig);
|
||||
//生命周期,以下都可以直接使用sqlsugardb了
|
||||
|
||||
// Aop及多租户连接字符串和类型,需要单独设置
|
||||
// Aop操作需要进行缓存
|
||||
// Aop操作不能进行缓存
|
||||
SetDbAop(SqlSugarClient);
|
||||
}
|
||||
|
||||
@@ -69,6 +78,7 @@ namespace Yi.Framework.SqlSugarCore
|
||||
|
||||
onSqlSugarClientConfig += dependency.OnSqlSugarClientConfig;
|
||||
}
|
||||
|
||||
//最先存放db操作
|
||||
onSqlSugarClientConfig(sqlSugarClient);
|
||||
|
||||
@@ -77,8 +87,6 @@ namespace Yi.Framework.SqlSugarCore
|
||||
|
||||
sqlSugarClient.Aop.DataExecuting =dataExecuting;
|
||||
sqlSugarClient.Aop.DataExecuted =dataExecuted;
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -89,11 +97,14 @@ namespace Yi.Framework.SqlSugarCore
|
||||
protected virtual ConnectionConfig BuildConnectionConfig(Action<ConnectionConfig>? action = null)
|
||||
{
|
||||
var dbConnOptions = Options;
|
||||
|
||||
#region 组装options
|
||||
|
||||
if (dbConnOptions.DbType is null)
|
||||
{
|
||||
throw new ArgumentException("DbType配置为空");
|
||||
}
|
||||
|
||||
var slavaConFig = new List<SlaveConnectionConfig>();
|
||||
if (dbConnOptions.EnabledReadWrite)
|
||||
{
|
||||
@@ -110,9 +121,11 @@ namespace Yi.Framework.SqlSugarCore
|
||||
slavaConFig.Add(new SlaveConnectionConfig() { ConnectionString = s });
|
||||
});
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 组装连接config
|
||||
|
||||
var connectionConfig = new ConnectionConfig()
|
||||
{
|
||||
ConfigId = ConnectionStrings.DefaultConnectionStringName,
|
||||
@@ -148,6 +161,7 @@ namespace Yi.Framework.SqlSugarCore
|
||||
{
|
||||
entityService += dependency.EntityService;
|
||||
}
|
||||
|
||||
entityService(c, p);
|
||||
}
|
||||
},
|
||||
@@ -165,7 +179,9 @@ namespace Yi.Framework.SqlSugarCore
|
||||
{
|
||||
action.Invoke(connectionConfig);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
return connectionConfig;
|
||||
}
|
||||
|
||||
@@ -177,7 +193,7 @@ namespace Yi.Framework.SqlSugarCore
|
||||
{
|
||||
var connectionStringResolver = LazyServiceProvider.LazyGetRequiredService<IConnectionStringResolver>();
|
||||
var connectionString =
|
||||
connectionStringResolver.ResolveAsync().ConfigureAwait(false).GetAwaiter().GetResult();
|
||||
AsyncHelper.RunSync(() => connectionStringResolver.ResolveAsync());
|
||||
|
||||
if (string.IsNullOrWhiteSpace(connectionString))
|
||||
{
|
||||
|
||||
@@ -6,12 +6,9 @@ using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SqlSugar;
|
||||
using Volo.Abp;
|
||||
using Volo.Abp.Auditing;
|
||||
using Volo.Abp.Data;
|
||||
using Volo.Abp.Domain;
|
||||
using Volo.Abp.Domain.Repositories;
|
||||
using Volo.Abp.Modularity;
|
||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||
using Yi.Framework.SqlSugarCore.Repositories;
|
||||
using Yi.Framework.SqlSugarCore.Uow;
|
||||
@@ -47,6 +44,7 @@ namespace Yi.Framework.SqlSugarCore
|
||||
//将默认db传递给abp连接字符串模块
|
||||
Configure<AbpDbConnectionOptions>(x => { x.ConnectionStrings.Default = dbConfig.Url; });
|
||||
|
||||
context.Services.AddYiDbContext<DefaultSqlSugarDbContext>();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
@@ -72,7 +70,6 @@ namespace Yi.Framework.SqlSugarCore
|
||||
|
||||
|
||||
logger.LogInformation(sb.ToString());
|
||||
//Todo:准备支持多租户种子数据及CodeFirst
|
||||
|
||||
if (options.EnabledCodeFirst)
|
||||
{
|
||||
|
||||
@@ -22,10 +22,7 @@ using Volo.Abp.AspNetCore.Serilog;
|
||||
using Volo.Abp.Auditing;
|
||||
using Volo.Abp.Autofac;
|
||||
using Volo.Abp.BackgroundJobs.Hangfire;
|
||||
using Volo.Abp.BackgroundWorkers;
|
||||
using Volo.Abp.BackgroundWorkers.Hangfire;
|
||||
using Volo.Abp.Caching;
|
||||
using Volo.Abp.Hangfire;
|
||||
using Volo.Abp.MultiTenancy;
|
||||
using Volo.Abp.Swashbuckle;
|
||||
using Yi.Abp.Application;
|
||||
@@ -271,6 +268,7 @@ namespace Yi.Abp.Web
|
||||
context.Token = cookiesToken;
|
||||
}
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
};
|
||||
@@ -314,7 +312,6 @@ namespace Yi.Abp.Web
|
||||
context.Services.AddAuthorization();
|
||||
|
||||
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
@@ -375,7 +372,8 @@ namespace Yi.Abp.Web
|
||||
app.UseAbpSerilogEnrichers();
|
||||
|
||||
//Hangfire定时任务面板,可配置授权,意框架支持jwt
|
||||
app.UseAbpHangfireDashboard("/hangfire", options =>
|
||||
app.UseAbpHangfireDashboard("/hangfire",
|
||||
options =>
|
||||
{
|
||||
options.AsyncAuthorization = new[] { new YiTokenAuthorizationFilter(app.ApplicationServices) };
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user