Skip to content

Commit

Permalink
feature:
Browse files Browse the repository at this point in the history
添加池化上下文对象
  • Loading branch information
KawhiWei committed Jul 22, 2024
1 parent 8b57f99 commit e923836
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,33 @@ public static IServiceCollection AddLuckDbContext<TDbContext>(this IServiceColle

// var serviceDescriptor = services.FirstOrDefault<ServiceDescriptor>((Func<ServiceDescriptor, bool>) (d => d.ServiceType == typeof (TContextImplementation)));

services.AddDbContext<LuckDbContextBase, TDbContext>((provider, dbContextBuilder) =>
{
var config = new EfDbContextConfig();
efDbContextAction.Invoke(config);
var dbType = config.Type;
var drivenProvider = provider.GetServices<IDbContextDrivenProvider>()
.FirstOrDefault(x => x.Type.Equals(dbType));

if (drivenProvider == null)
throw new LuckException($"{nameof(drivenProvider)}没有对应的{dbType}的实现!");
var builder = drivenProvider.Builder(dbContextBuilder, config.ConnectionString,
config.QuerySplittingBehavior);
optionsAction?.Invoke(provider, builder);
});

return services;
}
public static IServiceCollection AddLuckDbContextPool<TDbContext>(this IServiceCollection services,
Action<EfDbContextConfig> efDbContextAction,
Action<IServiceProvider, DbContextOptionsBuilder>? optionsAction = null)
where TDbContext : LuckDbContextBase
{
if (efDbContextAction == null)
throw new LuckException(nameof(efDbContextAction));

// var serviceDescriptor = services.FirstOrDefault<ServiceDescriptor>((Func<ServiceDescriptor, bool>) (d => d.ServiceType == typeof (TContextImplementation)));

services.AddDbContextPool<LuckDbContextBase, TDbContext>((provider, dbContextBuilder) =>
{
var config = new EfDbContextConfig();
Expand All @@ -56,7 +83,9 @@ public static IServiceCollection AddLuckDbContext<TDbContext>(this IServiceColle

return services;
}


[Obsolete("存在部分问题未解决")]
public static IServiceCollection AddPooledLuckDbContextFactory<TDbContext>(this IServiceCollection services,
Action<EfDbContextConfig> efDbContextAction,
Action<IServiceProvider, DbContextOptionsBuilder>? optionsAction = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public EntityFrameworkCoreTests(ITestOutputHelper testOutputHelper)
}

[Fact]
public async Task AggregateRootRepositoryAddAsync()
public async Task Test_LuckDbContextAsync()
{
try
{
Expand All @@ -56,4 +56,37 @@ public async Task AggregateRootRepositoryAddAsync()
throw;
}
}

[Fact]
public async Task Test_LuckDbContextPoolAsync()
{
var services = new ServiceCollection();
services.AddLuckDbContextPool<TestContext>(x =>
{
x.ConnectionString = Guid.NewGuid().ToString();
x.Type = DataBaseType.MemoryDataBase;
})
.AddLogging()
.AddUnitOfWork()
.AddMemoryDriven()
.AddDefaultRepository();
var serviceProvider = services.BuildServiceProvider();
var unitOfWork = serviceProvider.GetRequiredService<IUnitOfWork>();
var orderAggregateRootRepository =
serviceProvider.GetRequiredService<IAggregateRootRepository<Order, string>>();
try
{
var order = new Order("Pual", "Los");
orderAggregateRootRepository.Add(order);
await unitOfWork.CommitAsync();
var result = await orderAggregateRootRepository.FindAsync(x => x.Name == "Pual");
Assert.NotNull(result);
Assert.True(result.Name == order.Name);
}
catch (Exception e)
{
_testOutputHelper.WriteLine(e.ToString());
throw;
}
}
}

0 comments on commit e923836

Please sign in to comment.