This commit is contained in:
xuejiaming 2022-06-27 08:26:13 +08:00
parent cc2d7e72b1
commit c4931470fe
4 changed files with 26 additions and 3 deletions

View File

@ -0,0 +1,11 @@
using System;
namespace ShardingCore.Core
{
public interface IShardingRuntimeContext
{
object GetService(Type serviceType);
TService GetService<TService>();
}
}

View File

@ -0,0 +1,7 @@
namespace ShardingCore.Core
{
public interface IShardingRuntimeContextFactory
{
IShardingRuntimeContext Create();
}
}

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Text; using System.Text;
using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using ShardingCore.Core;
namespace ShardingCore.EFCores.OptionsExtensions namespace ShardingCore.EFCores.OptionsExtensions
{ {
@ -17,12 +18,16 @@ namespace ShardingCore.EFCores.OptionsExtensions
#if EFCORE6 #if EFCORE6
public class ShardingWrapOptionsExtension : IDbContextOptionsExtension public class ShardingWrapOptionsExtension : IDbContextOptionsExtension
{ {
public ShardingWrapOptionsExtension() private readonly IShardingRuntimeContext _shardingRuntimeContext;
public ShardingWrapOptionsExtension(IShardingRuntimeContext shardingRuntimeContext)
{ {
_shardingRuntimeContext = shardingRuntimeContext;
Console.WriteLine("ShardingWrapOptionsExtension ctor"); Console.WriteLine("ShardingWrapOptionsExtension ctor");
} }
public void ApplyServices(IServiceCollection services) public void ApplyServices(IServiceCollection services)
{ {
services.AddSingleton<IShardingRuntimeContext>(sp => _shardingRuntimeContext);
Console.WriteLine("ShardingWrapOptionsExtension ApplyServices"); Console.WriteLine("ShardingWrapOptionsExtension ApplyServices");
} }

View File

@ -23,11 +23,11 @@ namespace ShardingCore.EFCores
private readonly IShardingDbContext _shardingDbContext; private readonly IShardingDbContext _shardingDbContext;
private readonly IShardingCompilerExecutor _shardingCompilerExecutor; private readonly IShardingCompilerExecutor _shardingCompilerExecutor;
public ShardingQueryCompiler(ICurrentDbContext currentContext) public ShardingQueryCompiler(ICurrentDbContext currentContext,IShardingRuntimeContext shardingRuntimeContext)
{ {
_shardingDbContext = currentContext.Context as IShardingDbContext ?? _shardingDbContext = currentContext.Context as IShardingDbContext ??
throw new ShardingCoreException("db context operator is not IShardingDbContext"); throw new ShardingCoreException("db context operator is not IShardingDbContext");
_shardingCompilerExecutor = ShardingRuntimeContext.GetInstance().GetService<IShardingCompilerExecutor>(); _shardingCompilerExecutor = shardingRuntimeContext.GetService<IShardingCompilerExecutor>();
} }
public TResult Execute<TResult>(Expression query) public TResult Execute<TResult>(Expression query)