Browse Source

使我们的FreeSql有了分库的能力

master
Nice 3 years ago
parent
commit
cb8f8a52c9
  1. 5
      Gear/Gear.csproj
  2. 4
      Gear/Gear/Abstractions/AppService.cs
  3. 3
      Gear/Gear/Abstractions/Use.cs
  4. 38
      Gear/Gear/Infrastructure/Extensions/FreeSqlManager.cs
  5. 54
      Gear/Gear/Infrastructure/Extensions/ServiceCollectionServiceExtensions.cs

5
Gear/Gear.csproj

@ -2,13 +2,13 @@
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Description>全网科技:微服务最底层驱动齿轮</Description>
<Description>✨全网科技❤❤❤</Description>
<RootNamespace />
<Authors>2017875139@qq.com</Authors>
<Company>全网科技</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>Gear</PackageId>
<Version>0.1.1</Version>
<Version>0.1.4</Version>
<Product>GearGear</Product>
<AssemblyName>Gear</AssemblyName>
</PropertyGroup>
@ -21,6 +21,7 @@
<PackageReference Include="FreeSql.DbContext" Version="2.5.200" />
<PackageReference Include="FreeSql.Provider.SqlServer" Version="2.5.200" />
<PackageReference Include="IdGen" Version="3.0.0" />
<PackageReference Include="IdleBus" Version="1.5.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.9" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="5.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />

4
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<IDecryptClaim>();
protected long GetUserId()
{
if ((Configuration["LocalDebug"] ?? "") == "true")

3
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<IFreeSql>();
protected IFreeSql FreeSql { get; set; } = FreeSqlManager.GetFreeSql();
protected IMapper Mapper => LazyServiceProvider.LazyGetService<IMapper>();

38
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<IFreeSql> 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);
}
}
}

54
Gear/Gear/Infrastructure/Extensions/ServiceCollectionServiceExtensions.cs

@ -24,6 +24,7 @@ namespace Gear.Infrastructure.Extensions
{
public static class ServiceCollectionServiceExtensions
{
/// <summary>
/// 齿轮配置 Gear.Abstractions 中的默认功能
/// </summary>
@ -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<ILazyServiceProvider>();
return use;
});
}
//else //不写了太难了
//{
// services.TryAddTransient(type, service =>
// {
// Use use = (Use)Activator.CreateInstance(type);
// use.LazyServiceProvider = service.GetService<ILazyServiceProvider>();
// 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<IConfiguration>();
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<MvcOptions>(options =>
{
options.Conventions.Add(new ApplicationConvention());

Loading…
Cancel
Save