diff --git a/Gear/Gear.csproj b/Gear/Gear.csproj index 67665f4..c19367a 100644 --- a/Gear/Gear.csproj +++ b/Gear/Gear.csproj @@ -2,13 +2,13 @@ net5.0 - 全网科技:微服务最底层驱动齿轮 + ✨全网科技❤❤❤ 2017875139@qq.com 全网科技 true Gear - 0.1.1 + 0.1.4 GearGear Gear @@ -21,6 +21,7 @@ + diff --git a/Gear/Gear/Abstractions/AppService.cs b/Gear/Gear/Abstractions/AppService.cs index 764f8d4..f51083c 100644 --- a/Gear/Gear/Abstractions/AppService.cs +++ b/Gear/Gear/Abstractions/AppService.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Http; +using System; using System.Security.Claims; namespace Gear.Abstractions @@ -17,6 +18,8 @@ namespace Gear.Abstractions "Patch" }; + + protected const string DaprPubSubName = "pubsub"; protected const string DaprStoreName = "store"; @@ -31,6 +34,7 @@ namespace Gear.Abstractions protected IDecryptClaim DecryptClaim => LazyServiceProvider.LazyGetService(); + protected long GetUserId() { if ((Configuration["LocalDebug"] ?? "") == "true") diff --git a/Gear/Gear/Abstractions/Use.cs b/Gear/Gear/Abstractions/Use.cs index df178ab..f3066b7 100644 --- a/Gear/Gear/Abstractions/Use.cs +++ b/Gear/Gear/Abstractions/Use.cs @@ -2,6 +2,7 @@ using Dapr.Client; using FreeSql; using Gear.Infrastructure.Exceptions; +using Gear.Infrastructure.Extensions; using Gear.Wapper; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; @@ -20,7 +21,7 @@ namespace Gear.Abstractions public ILazyServiceProvider LazyServiceProvider { get; set; } - protected IFreeSql FreeSql => LazyServiceProvider.LazyGetService(); + protected IFreeSql FreeSql { get; set; } = FreeSqlManager.GetFreeSql(); protected IMapper Mapper => LazyServiceProvider.LazyGetService(); diff --git a/Gear/Gear/Infrastructure/Extensions/FreeSqlManager.cs b/Gear/Gear/Infrastructure/Extensions/FreeSqlManager.cs new file mode 100644 index 0000000..001b7d9 --- /dev/null +++ b/Gear/Gear/Infrastructure/Extensions/FreeSqlManager.cs @@ -0,0 +1,38 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Gear.Infrastructure.Extensions +{ + public static class FreeSqlManager + { + private static readonly IdleBus ib = new(TimeSpan.FromMinutes(10)); + + public static void RegisterFreeSql(this IConfiguration configuration, string configurationKey = "ConnectionString") + { + string connectionString = configuration[configurationKey]; + ib.TryRegister(configurationKey, () => + { + string fsdy = (configuration["FreeSql.DataType"] ?? "SqlServer").ToUpper(); + + FreeSql.FreeSqlBuilder builder = new(); + if (fsdy == "SqlServer".ToUpper()) + builder.UseConnectionString(FreeSql.DataType.SqlServer, connectionString); + if (fsdy == "MySql".ToUpper()) + builder.UseConnectionString(FreeSql.DataType.MySql, connectionString); + if (fsdy == "Oracle".ToUpper()) + builder.UseConnectionString(FreeSql.DataType.Oracle, connectionString); + + return builder.Build(); + }); + } + public static IFreeSql GetFreeSql(string configurationKey = "ConnectionString") + { + return ib.Get(configurationKey); + } + } +} diff --git a/Gear/Gear/Infrastructure/Extensions/ServiceCollectionServiceExtensions.cs b/Gear/Gear/Infrastructure/Extensions/ServiceCollectionServiceExtensions.cs index 44228de..b81a7ad 100644 --- a/Gear/Gear/Infrastructure/Extensions/ServiceCollectionServiceExtensions.cs +++ b/Gear/Gear/Infrastructure/Extensions/ServiceCollectionServiceExtensions.cs @@ -24,6 +24,7 @@ namespace Gear.Infrastructure.Extensions { public static class ServiceCollectionServiceExtensions { + /// /// 齿轮配置 Gear.Abstractions 中的默认功能 /// @@ -49,36 +50,12 @@ namespace Gear.Infrastructure.Extensions typeof(ITransientDependency) != o && typeof(ITransientDependency).IsAssignableFrom(o))) { var interfaceType = type.GetInterfaces().FirstOrDefault(interfaceType => interfaceType.Name.EndsWith(type.Name)); - if (typeof(Use).IsAssignableFrom(type)) - { - - if (interfaceType != null) - { - services.TryAddTransient(type); - services.TryAddTransient(interfaceType, service => - { - Use use = (Use)service.GetService(type); - use.LazyServiceProvider = service.GetService(); - return use; - }); - } - //else //不写了太难了 - //{ - // services.TryAddTransient(type, service => - // { - // Use use = (Use)Activator.CreateInstance(type); - // use.LazyServiceProvider = service.GetService(); - // return use; - // }); - //} - } + + if (interfaceType != null) + services.TryAddTransient(interfaceType, type); else - { - if (interfaceType != null) - services.TryAddTransient(interfaceType, type); - else - services.TryAddTransient(type, type); - } + services.TryAddTransient(type, type); + } foreach (var type in types.Where(o => !o.IsInterface && !o.IsAbstract && o.IsClass && @@ -111,25 +88,6 @@ namespace Gear.Infrastructure.Extensions services.AddAutoMapper(assemblies); - services.AddSingleton(o => - { - var configuration = o.GetRequiredService(); - - string connectionString = configuration["ConnectionString"]; - - string fsdy = (configuration["FreeSql.DataType"] ?? "SqlServer").ToUpper(); - - FreeSql.FreeSqlBuilder builder = new(); - if (fsdy == "SqlServer".ToUpper()) - builder.UseConnectionString(FreeSql.DataType.SqlServer, connectionString); - if (fsdy == "MySql".ToUpper()) - builder.UseConnectionString(FreeSql.DataType.MySql, connectionString); - if (fsdy == "Oracle".ToUpper()) - builder.UseConnectionString(FreeSql.DataType.Oracle, connectionString); - - return builder.Build(); - }); - services.Configure(options => { options.Conventions.Add(new ApplicationConvention());