Skip to content

Commit 8a8e3d7

Browse files
committed
improve SlimBus registration
1 parent 8ab45be commit 8a8e3d7

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

src/DKNet.FW.sln.DotSettings.user

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@
579579
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=8275c8a4_002D4ae3_002D406a_002D9cd0_002D6dc520603cf1/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="All tests from &amp;lt;EfCore&amp;gt;\&amp;lt;EfCore.Specifications.Tests&amp;gt;" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
580580
&lt;Project Location="/Users/steven/_CODE/DRUNK/DKNet/src/EfCore/EfCore.Specifications.Tests" Presentation="&amp;lt;EfCore&amp;gt;\&amp;lt;EfCore.Specifications.Tests&amp;gt;" /&gt;
581581
&lt;/SessionState&gt;</s:String>
582-
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=bd8ac265_002D8b5d_002D4892_002D9e95_002Dccb01cb363e1/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from &amp;lt;EfCore&amp;gt;\&amp;lt;EfCore.Specifications.Tests&amp;gt;" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
582+
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=bd8ac265_002D8b5d_002D4892_002D9e95_002Dccb01cb363e1/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="All tests from &amp;lt;EfCore&amp;gt;\&amp;lt;EfCore.Specifications.Tests&amp;gt;" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
583583
&lt;Project Location="/Users/steven/_CODE/DRUNK/DKNet/src" Presentation="&amp;lt;EfCore&amp;gt;" /&gt;
584584
&lt;/SessionState&gt;</s:String>
585585

src/SlimBus/DKNet.SlimBus.Extensions/Behaviors/EfAutoSavePostProcessor.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
11
using DKNet.EfCore.Extensions.Extensions;
22
using FluentResults;
33
using Microsoft.EntityFrameworkCore;
4-
using Microsoft.Extensions.DependencyInjection;
54
using SlimMessageBus;
65
using SlimMessageBus.Host.Interceptor;
76

87
namespace DKNet.SlimBus.Extensions.Behaviors;
98

9+
internal static class EfAutoSavePostProcessorRegistration
10+
{
11+
#region Properties
12+
13+
public static HashSet<Type> DbContextTypes { get; } = [];
14+
15+
#endregion
16+
17+
#region Methods
18+
19+
public static void RegisterDbContextType<TDbContext>()
20+
where TDbContext : DbContext
21+
{
22+
DbContextTypes.Add(typeof(TDbContext));
23+
}
24+
25+
#endregion
26+
}
27+
1028
internal sealed class EfAutoSavePostProcessor<TRequest, TResponse>(IServiceProvider serviceProvider)
1129
: IRequestHandlerInterceptor<TRequest, TResponse>, IInterceptorWithOrder
1230
{
@@ -32,7 +50,9 @@ request is Fluents.Queries.IWitPageResponse<TResponse>
3250
|| request is Fluents.EventsConsumers.IHandler<IRequest>)
3351
return response;
3452

35-
var dbContexts = serviceProvider.GetServices<DbContext>();
53+
var dbContexts = EfAutoSavePostProcessorRegistration.DbContextTypes
54+
.Select(serviceProvider.GetService).OfType<DbContext>().ToArray();
55+
3656
foreach (var db in dbContexts.Where(db => db.ChangeTracker.HasChanges()))
3757
{
3858
await db.AddNewEntitiesFromNavigations(context.CancellationToken);

src/SlimBus/DKNet.SlimBus.Extensions/SlimBusEfCoreSetup.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using DKNet.SlimBus.Extensions.Behaviors;
88
using DKNet.SlimBus.Extensions.Handlers;
99
using Microsoft.EntityFrameworkCore;
10-
using SlimMessageBus.Host;
1110
using SlimMessageBus.Host.Interceptor;
1211

1312
// ReSharper disable once CheckNamespace
@@ -42,13 +41,18 @@ public IServiceCollection AddSlimBusEventPublisher<TDbContext>()
4241
/// request handlers run. Registers the <see cref="EfAutoSavePostProcessor{TRequest,TResponse}" />
4342
/// as an <see cref="IRequestHandlerInterceptor{TRequest,TResponse}" /> to be executed by SlimMessageBus.
4443
/// </summary>
45-
/// <param name="configure">A callback to configure the SlimMessageBus <see cref="MessageBusBuilder" />.</param>
4644
/// <returns>The same <see cref="IServiceCollection" /> instance for chaining.</returns>
47-
public IServiceCollection AddSlimBusForEfCore(Action<MessageBusBuilder> configure)
45+
public IServiceCollection AddSlimBusEfCoreInterceptor<TDbContext>()
46+
where TDbContext : DbContext
4847
{
48+
EfAutoSavePostProcessorRegistration.RegisterDbContextType<TDbContext>();
49+
50+
if (serviceCollection.Any(serviceDescriptor =>
51+
serviceDescriptor.ServiceType == typeof(IRequestHandlerInterceptor<,>)))
52+
return serviceCollection;
53+
4954
serviceCollection
50-
.AddScoped(typeof(IRequestHandlerInterceptor<,>), typeof(EfAutoSavePostProcessor<,>))
51-
.AddSlimMessageBus(configure);
55+
.AddScoped(typeof(IRequestHandlerInterceptor<,>), typeof(EfAutoSavePostProcessor<,>));
5256
return serviceCollection;
5357
}
5458
}

src/SlimBus/SlimBus.Extensions.Tests/Fixtures/Fixture.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public Fixture()
1717
.AddScoped<IMapper, ServiceMapper>()
1818
.AddDbContext<TestDbContext>(b => b.UseInMemoryDatabase(nameof(TestDbContext)))
1919
.AddScoped<DbContext>(p => p.GetRequiredService<TestDbContext>())
20-
.AddSlimBusForEfCore(mmb =>
20+
.AddSlimBusEfCoreInterceptor<TestDbContext>()
21+
.AddSlimMessageBus(mmb =>
2122
{
2223
//This is a global config for all the child busses
2324
mmb.AddJsonSerializer()

0 commit comments

Comments
 (0)