@@ -23,34 +23,60 @@ Action<OutboxServiceConfiguration> configuration
2323 var serviceConfig = new OutboxServiceConfiguration ( ) ;
2424 configuration . Invoke ( serviceConfig ) ;
2525
26- // register all assigned handlers
27- services . TryAddEnumerable ( serviceConfig . HandlersWithLifetime ) ;
28-
2926 services . AddScoped < IOutboxDbContext > ( sp => sp . GetRequiredService < TContext > ( ) ) ;
3027 services . AddScoped < AddMessageToOutbox > ( ) ;
3128 services . AddScoped < IOutbox , Outbox > ( ) ;
32- services . AddScoped < IMessageDispatcher , DirectInvocationDispatcher > ( ) ;
33- services . AddScoped < IMessageExceptionHandler , DiscardMessageOnExceptionHandler > ( ) ;
34- services . AddScoped < ProcessExceptionFromHandler > ( ) ;
29+ services . AddScoped < IMessageDispatcher < OutboxMessage > , OutboxDispatcher > ( ) ;
30+
31+ AddGenericServices < OutboxMessage , IOutboxDbContext > ( services , serviceConfig ) ;
32+
33+ return services ;
34+ }
35+
36+ public static IServiceCollection AddInboxServices < TContext > (
37+ this IServiceCollection services ,
38+ Action < InboxServiceConfiguration > configuration
39+ ) where TContext : DbContext , IInboxDbContext
40+ {
41+ var serviceConfig = new InboxServiceConfiguration ( ) ;
42+ configuration . Invoke ( serviceConfig ) ;
43+
44+ services . AddScoped < IInboxDbContext > ( sp => sp . GetRequiredService < TContext > ( ) ) ;
45+ services . AddScoped < AddMessageToInbox > ( ) ;
46+ services . AddScoped < IInbox , Inbox > ( ) ;
47+ services . AddScoped < IMessageDispatcher < InboxMessage > , InboxDispatcher > ( ) ;
48+
49+ AddGenericServices < InboxMessage , IInboxDbContext > ( services , serviceConfig ) ;
50+
51+ return services ;
52+ }
53+
54+ private static void AddGenericServices < TEntity , TContext > ( this IServiceCollection services , ServiceConfiguration serviceConfig )
55+ where TEntity : class , IMessage
56+ where TContext : IDbContext
57+ {
58+ // register all assigned handlers
59+ services . TryAddEnumerable ( serviceConfig . HandlersWithLifetime ) ;
60+
61+ services . AddScoped < IMessageExceptionHandler < TEntity > , DiscardMessageOnExceptionHandler < TEntity > > ( ) ;
62+ services . AddScoped < ProcessExceptionFromHandler < TEntity > > ( ) ;
3563 services . AddSingleton (
36- provider => new OutboxProcessor (
64+ provider => new Processor < TEntity > (
3765 serviceConfig ,
3866 provider . GetRequiredService < IServiceScopeFactory > ( ) ,
39- provider . GetRequiredService < IMessageDispatcher > ( ) ,
40- provider . GetRequiredService < ILogger < OutboxProcessor > > ( )
67+ provider . GetRequiredService < IMessageDispatcher < TEntity > > ( ) ,
68+ provider . GetRequiredService < ILogger < Processor < TEntity > > > ( )
4169 )
4270 ) ;
43- services . AddHostedService < OutboxBackgroundService > ( ) ;
71+ services . AddHostedService < BackgroundService < TEntity > > ( ) ;
4472
4573 var serviceProvider = services . BuildServiceProvider ( ) ;
46- var dbContext = serviceProvider . GetRequiredService < IOutboxDbContext > ( ) ;
74+ var dbContext = serviceProvider . GetRequiredService < TContext > ( ) ;
4775 var connectionString = dbContext . Database . GetConnectionString ( ) ;
4876 if ( string . IsNullOrEmpty ( connectionString ) )
4977 {
5078 throw new ArgumentException ( "Database connection string is not set. Please ensure the DbContext is properly configured." ) ;
5179 }
5280 services . AddSingleton < IDistributedLockProvider > ( _ => new PostgresDistributedSynchronizationProvider ( connectionString ) ) ;
53-
54- return services ;
5581 }
5682}
0 commit comments