测试外部依赖注入
This commit is contained in:
parent
efb785a005
commit
04842ea5b4
|
@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
|
@ -9,6 +10,7 @@ using MySqlConnector;
|
||||||
using Sample.MySql.DbContexts;
|
using Sample.MySql.DbContexts;
|
||||||
using Sample.MySql.Shardings;
|
using Sample.MySql.Shardings;
|
||||||
using ShardingCore;
|
using ShardingCore;
|
||||||
|
using ShardingCore.EFCores.OptionsExtensions;
|
||||||
using ShardingCore.Helpers;
|
using ShardingCore.Helpers;
|
||||||
using ShardingCore.TableExists;
|
using ShardingCore.TableExists;
|
||||||
|
|
||||||
|
@ -41,7 +43,7 @@ namespace Sample.MySql
|
||||||
// op.AddShardingTableRoute<SysUserModVirtualTableRoute>();
|
// op.AddShardingTableRoute<SysUserModVirtualTableRoute>();
|
||||||
// op.AddShardingTableRoute<SysUserSalaryVirtualTableRoute>();
|
// op.AddShardingTableRoute<SysUserSalaryVirtualTableRoute>();
|
||||||
// });
|
// });
|
||||||
|
// services.AddSingleton<IShardingRuntimeContext, ShardingRuntimeContext>();
|
||||||
services.AddShardingDbContext<DefaultShardingDbContext>()
|
services.AddShardingDbContext<DefaultShardingDbContext>()
|
||||||
.AddEntityConfig(o =>
|
.AddEntityConfig(o =>
|
||||||
{
|
{
|
||||||
|
@ -89,6 +91,19 @@ namespace Sample.MySql
|
||||||
}
|
}
|
||||||
|
|
||||||
app.UseShardingCore();
|
app.UseShardingCore();
|
||||||
|
|
||||||
|
// using (var scope = app.ApplicationServices.CreateScope())
|
||||||
|
// {
|
||||||
|
// var dbContext = scope.ServiceProvider.GetService<DefaultShardingDbContext>();
|
||||||
|
// var shardingRuntimeContext = dbContext.GetService<IShardingRuntimeContext>();
|
||||||
|
// Console.WriteLine("123");
|
||||||
|
// }
|
||||||
|
// using (var scope = app.ApplicationServices.CreateScope())
|
||||||
|
// {
|
||||||
|
// var dbContext = scope.ServiceProvider.GetService<DefaultShardingDbContext>();
|
||||||
|
// var shardingRuntimeContext = dbContext.GetService<IShardingRuntimeContext>();
|
||||||
|
// Console.WriteLine("1231");
|
||||||
|
// }
|
||||||
app.UseRouting();
|
app.UseRouting();
|
||||||
|
|
||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
|
|
|
@ -88,7 +88,7 @@ namespace ShardingCore
|
||||||
{
|
{
|
||||||
var virtualDataSource = serviceProvider.GetRequiredService<IVirtualDataSourceManager<TShardingDbContext>>().GetCurrentVirtualDataSource();
|
var virtualDataSource = serviceProvider.GetRequiredService<IVirtualDataSourceManager<TShardingDbContext>>().GetCurrentVirtualDataSource();
|
||||||
var connectionString = virtualDataSource.GetConnectionString(virtualDataSource.DefaultDataSourceName);
|
var connectionString = virtualDataSource.GetConnectionString(virtualDataSource.DefaultDataSourceName);
|
||||||
var contextOptionsBuilder = virtualDataSource.ConfigurationParams.UseDbContextOptionsBuilder(connectionString, dbContextOptionsBuilder).UseSharding<TShardingDbContext>();
|
var contextOptionsBuilder = virtualDataSource.ConfigurationParams.UseDbContextOptionsBuilder(connectionString, dbContextOptionsBuilder).UseSharding<TShardingDbContext>();//serviceProvider.GetRequiredService<IShardingRuntimeContext>()
|
||||||
virtualDataSource.ConfigurationParams.UseShellDbContextOptionBuilder(contextOptionsBuilder);
|
virtualDataSource.ConfigurationParams.UseShellDbContextOptionBuilder(contextOptionsBuilder);
|
||||||
}
|
}
|
||||||
internal static IServiceCollection AddInternalShardingCore<TShardingDbContext>(this IServiceCollection services) where TShardingDbContext : DbContext, IShardingDbContext
|
internal static IServiceCollection AddInternalShardingCore<TShardingDbContext>(this IServiceCollection services) where TShardingDbContext : DbContext, IShardingDbContext
|
||||||
|
@ -155,7 +155,8 @@ namespace ShardingCore
|
||||||
}
|
}
|
||||||
public static DbContextOptionsBuilder UseSharding<TShardingDbContext>(this DbContextOptionsBuilder optionsBuilder) where TShardingDbContext : DbContext, IShardingDbContext
|
public static DbContextOptionsBuilder UseSharding<TShardingDbContext>(this DbContextOptionsBuilder optionsBuilder) where TShardingDbContext : DbContext, IShardingDbContext
|
||||||
{
|
{
|
||||||
return optionsBuilder.UseShardingWrapMark()
|
//,IShardingRuntimeContext shardingRuntimeContext
|
||||||
|
return optionsBuilder.UseShardingWrapMark()//shardingRuntimeContext
|
||||||
.ReplaceService<IDbSetSource, ShardingDbSetSource>()
|
.ReplaceService<IDbSetSource, ShardingDbSetSource>()
|
||||||
.ReplaceService<IQueryCompiler, ShardingQueryCompiler>()
|
.ReplaceService<IQueryCompiler, ShardingQueryCompiler>()
|
||||||
.ReplaceService<IDbContextTransactionManager, ShardingRelationalTransactionManager<TShardingDbContext>>()
|
.ReplaceService<IDbContextTransactionManager, ShardingRelationalTransactionManager<TShardingDbContext>>()
|
||||||
|
@ -165,14 +166,15 @@ namespace ShardingCore
|
||||||
|
|
||||||
private static DbContextOptionsBuilder UseShardingWrapMark(this DbContextOptionsBuilder optionsBuilder)
|
private static DbContextOptionsBuilder UseShardingWrapMark(this DbContextOptionsBuilder optionsBuilder)
|
||||||
{
|
{
|
||||||
var extension = optionsBuilder.CreateOrGetExtension();
|
//IShardingRuntimeContext shardingRuntimeContext
|
||||||
|
var extension = optionsBuilder.CreateOrGetExtension();//shardingRuntimeContext
|
||||||
((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(extension);
|
((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(extension);
|
||||||
return optionsBuilder;
|
return optionsBuilder;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ShardingWrapOptionsExtension CreateOrGetExtension(this DbContextOptionsBuilder optionsBuilder)
|
private static ShardingWrapOptionsExtension CreateOrGetExtension(this DbContextOptionsBuilder optionsBuilder)//,IShardingRuntimeContext shardingRuntimeContext
|
||||||
=> optionsBuilder.Options.FindExtension<ShardingWrapOptionsExtension>() ??
|
=> optionsBuilder.Options.FindExtension<ShardingWrapOptionsExtension>() ??
|
||||||
new ShardingWrapOptionsExtension();
|
new ShardingWrapOptionsExtension();//shardingRuntimeContext
|
||||||
|
|
||||||
public static DbContextOptionsBuilder UseInnerDbContextSharding<TShardingDbContext>(this DbContextOptionsBuilder optionsBuilder) where TShardingDbContext : DbContext, IShardingDbContext
|
public static DbContextOptionsBuilder UseInnerDbContextSharding<TShardingDbContext>(this DbContextOptionsBuilder optionsBuilder) where TShardingDbContext : DbContext, IShardingDbContext
|
||||||
{
|
{
|
||||||
|
|
|
@ -13,12 +13,40 @@ namespace ShardingCore.EFCores.OptionsExtensions
|
||||||
* @Ver: 1.0
|
* @Ver: 1.0
|
||||||
* @Email: 326308290@qq.com
|
* @Email: 326308290@qq.com
|
||||||
*/
|
*/
|
||||||
|
// public interface IShardingRuntimeContext
|
||||||
|
// {
|
||||||
|
// Guid id { get; }
|
||||||
|
// void Say();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// public class ShardingRuntimeContext : IShardingRuntimeContext
|
||||||
|
// {
|
||||||
|
// public Guid id { get; set; }=Guid.NewGuid();
|
||||||
|
//
|
||||||
|
// public ShardingRuntimeContext()
|
||||||
|
// {
|
||||||
|
// Console.WriteLine("ShardingRuntimeContext:"+id);
|
||||||
|
// }
|
||||||
|
// public void Say()
|
||||||
|
// {
|
||||||
|
// Console.WriteLine(id);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
#if EFCORE6
|
#if EFCORE6
|
||||||
public class ShardingWrapOptionsExtension : IDbContextOptionsExtension
|
public class ShardingWrapOptionsExtension : IDbContextOptionsExtension
|
||||||
{
|
{
|
||||||
|
// private readonly IShardingRuntimeContext _shardingRuntimeContext;
|
||||||
|
|
||||||
|
// public ShardingWrapOptionsExtension(IShardingRuntimeContext shardingRuntimeContext)
|
||||||
|
// {
|
||||||
|
// // _shardingRuntimeContext = shardingRuntimeContext;
|
||||||
|
// // Console.WriteLine("ShardingWrapOptionsExtension.ctor:"+shardingRuntimeContext.id);
|
||||||
|
// }
|
||||||
|
|
||||||
public void ApplyServices(IServiceCollection services)
|
public void ApplyServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
|
// services.AddSingleton<IShardingRuntimeContext>(_shardingRuntimeContext);
|
||||||
|
// Console.WriteLine("ShardingWrapOptionsExtension.ctor:ApplyServices");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Validate(IDbContextOptions options)
|
public void Validate(IDbContextOptions options)
|
||||||
|
|
Loading…
Reference in New Issue