Skip to content

Commit

Permalink
Merge branch 'vv' of github.com:KawhiWei/Luck.Framework into vv
Browse files Browse the repository at this point in the history
  • Loading branch information
KawhiWei committed Jul 31, 2024
2 parents 74a7913 + 6ff38da commit 863a917
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
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
1 change: 1 addition & 0 deletions src/framework/Luck.Pipeline/DefaultDelegatePipe.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Luck.Framework.PipelineAbstract;
using Microsoft.Extensions.Logging;

namespace Luck.Pipeline;

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;
}
}
}
4 changes: 3 additions & 1 deletion test/Luck.UnitTest/Pipeline_Tests/PipelineTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public void CreatePipelineFactory()
public void CreateDelegatePipelineFactory()
{
IServiceCollection services = new ServiceCollection();
services.AddScoped<IPipelineFactory, PipelineFactory>()
services
.AddLogging()
.AddScoped<IPipelineFactory, PipelineFactory>()
.AddScoped<FetchOrderDetailDelegatePipePipe>()
.AddScoped<CreateCustomerDelegatePipe>()
.AddScoped<CancelDelegatePipe>();
Expand Down

0 comments on commit 863a917

Please sign in to comment.