Skip to content

Commit

Permalink
Merge pull request #144 from koenbeuk/missing-registration-overload
Browse files Browse the repository at this point in the history
Added AddTriggeredDbContextPool overload that accepts an additional contract type
  • Loading branch information
koenbeuk authored Apr 19, 2022
2 parents a402e84 + 6c1c0ca commit a7185e1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static IServiceCollection AddTriggeredDbContext<TContext>(this IServiceCo
return serviceCollection;
}

public static IServiceCollection AddTriggeredDbContextPool<TContext>(this IServiceCollection serviceCollection, Action<DbContextOptionsBuilder>? optionsAction = null, int poolSize = 128)
public static IServiceCollection AddTriggeredDbContextPool<TContext>(this IServiceCollection serviceCollection, Action<DbContextOptionsBuilder>? optionsAction = null, int poolSize = 1024)
where TContext : DbContext
{
serviceCollection.AddDbContextPool<TContext>(options => {
Expand All @@ -55,6 +55,27 @@ public static IServiceCollection AddTriggeredDbContextPool<TContext>(this IServi
return serviceCollection;
}

public static IServiceCollection AddTriggeredDbContextPool<TContext, TImplementation>(this IServiceCollection serviceCollection, Action<DbContextOptionsBuilder>? optionsAction = null, int poolSize = 1024)
where TContext : class where TImplementation : DbContext, TContext
{
serviceCollection.AddDbContextPool<TContext, TImplementation>(options => {
optionsAction?.Invoke(options);
options.UseTriggers();
}, poolSize);

var serviceDescriptor = serviceCollection.FirstOrDefault(x => x.ServiceType == typeof(TContext));
if (serviceDescriptor?.ImplementationFactory != null)
{
serviceCollection.Replace(ServiceDescriptor.Describe(
serviceType: typeof(TContext),
implementationFactory: serviceProvider => SetApplicationTriggerServiceProviderAccessor(serviceDescriptor.ImplementationFactory(serviceProvider), serviceProvider),
lifetime: ServiceLifetime.Transient
));
}

return serviceCollection;
}

public static IServiceCollection AddTriggeredDbContextFactory<TContext>(this IServiceCollection serviceCollection, Action<DbContextOptionsBuilder>? optionsAction = null, ServiceLifetime lifetime = ServiceLifetime.Singleton)
where TContext : DbContext
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,27 @@ public void AddTriggeredDbContextPool_SupportsAScopedLifetime()
Assert.Equal(context1, context1);
}

[Fact]
public void AddTriggeredDbContextPool_SupportAContractType()
{
var subject = new ServiceCollection();
subject.AddTriggeredDbContextPool<DbContext, TestDbContext>(options => {
options.UseInMemoryDatabase("test");
options.ConfigureWarnings(warningOptions => {
warningOptions.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning);
});
});

var serviceProvider = subject.BuildServiceProvider();

using var scope = serviceProvider.CreateScope();

var context1 = scope.ServiceProvider.GetRequiredService<DbContext>();
var context2 = scope.ServiceProvider.GetRequiredService<TestDbContext>();

Assert.Equal(context1, context1);
}

#if EFCORETRIGGERED2 || EFCORETRIGGERED3
[Fact]
public void AddTriggeredDbContextFactory_ReusesScopedServiceProvider()
Expand Down

0 comments on commit a7185e1

Please sign in to comment.