Skip to content

Commit

Permalink
addMessagingForModules
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidEggenberger committed Apr 8, 2024
1 parent 9ae1248 commit 2243994
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
18 changes: 17 additions & 1 deletion Source/Shared/Features/Messaging/Registrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ namespace Shared.Features.Messaging
{
public static class Registrator
{
public static IServiceCollection AddMessaging(this IServiceCollection services, Assembly[] assemblies)
public static IServiceCollection AddMessaging(this IServiceCollection services)
{
services.TryAddScoped<ICommandDispatcher, CommandDispatcher>();
services.TryAddScoped<IQueryDispatcher, QueryDispatcher>();
services.TryAddScoped<IIntegrationEventDispatcher, IntegrationEventDispatcher>();
services.TryAddScoped<IDomainEventDispatcher, DomainEventDispatcher>();

return services;
}

public static IServiceCollection AddMessagingForModule(this IServiceCollection services, Type moduleType)
{
var assemblies = new Assembly[] { moduleType.Assembly };

// INFO: Using https://www.nuget.org/packages/Scrutor for registering all Query and Command handlers by convention
services.Scan(selector =>
{
Expand All @@ -37,6 +44,14 @@ public static IServiceCollection AddMessaging(this IServiceCollection services,
})
.AsImplementedInterfaces()
.WithScopedLifetime();

selector.FromAssemblies(assemblies)
.AddClasses(filter =>
{
filter.AssignableTo(typeof(ICommandHandler<,>));
})
.AsImplementedInterfaces()
.WithScopedLifetime();
});
services.Scan(selector =>
{
Expand All @@ -58,6 +73,7 @@ public static IServiceCollection AddMessaging(this IServiceCollection services,
.AsImplementedInterfaces()
.WithScopedLifetime();
});

return services;
}
}
Expand Down
29 changes: 9 additions & 20 deletions Source/Shared/Features/Modules/Registrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static IServiceCollection AddModule<TModuleStartup>(this IServiceCollecti
}

public static IServiceCollection AddModule<TModule, TModuleStartup>(this IServiceCollection services, IConfiguration config = null)
where TModule : IModule
where TModule : class, IModule
where TModuleStartup : IModuleStartup
{
// Register assembly in MVC so it can find controllers of the module
Expand All @@ -33,34 +33,23 @@ public static IServiceCollection AddModule<TModule, TModuleStartup>(this IServic
var moduleStartup = (IModuleStartup)Activator.CreateInstance(typeof(TModuleStartup));
moduleStartup.ConfigureServices(services, config);

var module = (IModule)ActivatorUtilities.CreateInstance<TModule>(services.BuildServiceProvider());
var module = ActivatorUtilities.CreateInstance<TModule>(services.BuildServiceProvider());
services.AddScoped<IModule>(sp => module);
services.AddMessagingForModule(module.GetType());

return services;
}

public static void AddModules(this IServiceCollection services)
{
var serviceProvider = services.BuildServiceProvider();

var startupModules = serviceProvider.GetRequiredService<IEnumerable<IModule>>();

services.AddMessaging(startupModules.Where(sm => sm.FeaturesAssembly is not null).Select(sm => sm.FeaturesAssembly).ToArray());
}

public static IApplicationBuilder UseModulesMiddleware(this IApplicationBuilder app, IHostEnvironment env)
{
app.Use(async (context, next) =>
{
foreach (var module in context.RequestServices.GetRequiredService<IEnumerable<IModuleStartup>>())
{
module.Configure(app, env);
}
using var scope = app.ApplicationServices.CreateAsyncScope();

await next();
});
foreach (var moduleStartup in scope.ServiceProvider.GetRequiredService<IEnumerable<IModuleStartup>>())
{
moduleStartup.Configure(app, env);
}

return app;
}
}
}
}
3 changes: 1 addition & 2 deletions Source/Shared/Features/Registrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ public static IServiceCollection AddSharedFeatures(this IServiceCollection servi
{
var serviceProvider = services.BuildServiceProvider();
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
var startupModules = serviceProvider.GetRequiredService<IEnumerable<IModule>>();

services.AddMessaging(startupModules.Where(sm => sm.FeaturesAssembly is not null).Select(x => x.FeaturesAssembly).ToArray());
services.AddMessaging();
services.AddEFCore(configuration);
services.AddEmailSender(configuration);

Expand Down
1 change: 0 additions & 1 deletion Source/Web/Server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public void ConfigureServices(IServiceCollection services)
services.AddModule<TenantIdentityModule, TenantIdentityModuleStartup>(Configuration);
services.AddModule<SubscriptionsModule, SubscriptionsModuleStartup>(Configuration);
services.AddModule<LandingPagesModuleStartup>(Configuration);
services.AddModules();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down

0 comments on commit 2243994

Please sign in to comment.