Skip to content

Commit

Permalink
Merge pull request #953 from sitkoru/app/modules-order
Browse files Browse the repository at this point in the history
Register configuration modules before others
  • Loading branch information
SonicGD authored Jun 24, 2024
2 parents 31454c1 + 11e8dcc commit dec43ec
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/Sitko.Core.App.Web/AppWebConfigurationModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class AppWebConfigurationModule : BaseApplicationModule<AppWebConfigurati
{
public override string OptionsKey => "Application:Web";

public void PostConfigureWebHost(IApplicationContext applicationContext, ConfigureWebHostBuilder webHostBuilder,
public void ConfigureWebHost(IApplicationContext applicationContext, ConfigureWebHostBuilder webHostBuilder,
AppWebConfigurationModuleOptions options)
{
if (options.Ports.Count != 0)
Expand Down
5 changes: 0 additions & 5 deletions src/Sitko.Core.App.Web/IWebApplicationModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ void ConfigureWebHost(IApplicationContext applicationContext, ConfigureWebHostBu
TModuleOptions options)
{
}

void PostConfigureWebHost(IApplicationContext applicationContext, ConfigureWebHostBuilder webHostBuilder,
TModuleOptions options)
{
}
}

public interface IAuthApplicationModule : IWebApplicationModule;
9 changes: 0 additions & 9 deletions src/Sitko.Core.App.Web/SitkoCoreWebApplicationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,6 @@ protected override void ConfigureHostBuilder<TModule, TModuleOptions>(Applicatio
}
}

protected override void PostConfigureModule<TModule, TModuleOptions>(TModule instance, TModuleOptions options)
{
base.PostConfigureModule(instance, options);
if (instance is IWebApplicationModule<TModuleOptions> webModule)
{
webModule.PostConfigureWebHost(BootApplicationContext, webApplicationBuilder.WebHost, options);
}
}

protected override void BeforeContainerBuild()
{
base.BeforeContainerBuild();
Expand Down
4 changes: 4 additions & 0 deletions src/Sitko.Core.App/ApplicationModuleRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ public override (bool isSuccess, IEnumerable<Type> missingModules) CheckRequired

public override bool IsEnabled(IApplicationContext context) => CreateOptions(context).Enabled;

public override void ClearOptionsCache() => optionsCache.Clear();

private TModuleOptions CreateOptions(IApplicationContext applicationContext, bool validateOptions = false)
{
TModuleOptions options;
Expand Down Expand Up @@ -261,4 +263,6 @@ public abstract (bool isSuccess, IEnumerable<Type> missingModules) CheckRequired
Type[] registeredModules);

public abstract bool IsEnabled(IApplicationContext context);

public abstract void ClearOptionsCache();
}
39 changes: 19 additions & 20 deletions src/Sitko.Core.App/SitkoCoreBaseApplicationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Sitko.Core.App;
public abstract class SitkoCoreBaseApplicationBuilder : ISitkoCoreApplicationBuilder
{
private readonly List<ApplicationModuleRegistration> moduleRegistrations = new();
private readonly List<Action> modulePostConfigureCallbacks = new();
private readonly List<Action> moduleConfigurationCallbacks = new();
private readonly SerilogConfigurator serilogConfigurator = new();

private IApplicationContext? bootApplicationContext;
Expand Down Expand Up @@ -174,22 +174,23 @@ private void RegisterModule<TModule, TModuleOptions>(
new ApplicationModuleRegistration<TModule, TModuleOptions>(instance, configureOptions, optionsKey);
if (registration.IsEnabled(BootApplicationContext))
{
BeforeModuleRegistration<TModule, TModuleOptions>(BootApplicationContext, registration);

registration.ConfigureAppConfiguration(BootApplicationContext, Configuration);
registration.ConfigureOptions(BootApplicationContext, Services);
registration.ConfigureServices(BootApplicationContext, Services);

AfterModuleRegistration<TModule, TModuleOptions>(BootApplicationContext, registration);
}

moduleRegistrations.Add(registration);
modulePostConfigureCallbacks.Add(() =>
moduleConfigurationCallbacks.Add(() =>
{
if (registration.IsEnabled(BootApplicationContext))
{
var (_, options) = registration.GetOptions(BootApplicationContext);
PostConfigureModule(instance, (TModuleOptions)options);
if (registration.IsEnabled(BootApplicationContext))
{
BeforeModuleRegistration<TModule, TModuleOptions>(BootApplicationContext, registration);

registration.ConfigureOptions(BootApplicationContext, Services);
registration.ConfigureServices(BootApplicationContext, Services);

AfterModuleRegistration<TModule, TModuleOptions>(BootApplicationContext, registration);
}
}
});
Services.AddSingleton<ApplicationModuleRegistration>(registration);
Expand Down Expand Up @@ -235,22 +236,20 @@ protected virtual LoggerConfiguration ConfigureDefautLogger(LoggerConfiguration
protected virtual void BeforeContainerBuild()
{
var enabledModules = ModulesHelper.GetEnabledModuleRegistrations(BootApplicationContext, moduleRegistrations);
serilogConfigurator.ApplyLogging(BootApplicationContext, enabledModules);
foreach (var moduleRegistration in enabledModules)
foreach (var applicationModuleRegistration in enabledModules)
{
moduleRegistration.PostConfigureServices(BootApplicationContext, Services);
applicationModuleRegistration.ClearOptionsCache();
}

foreach (var postConfigureCallback in modulePostConfigureCallbacks)
serilogConfigurator.ApplyLogging(BootApplicationContext, enabledModules);
foreach (var postConfigureCallback in moduleConfigurationCallbacks)
{
postConfigureCallback();
}
}

protected virtual void PostConfigureModule<TModule, TModuleOptions>(TModule instance, TModuleOptions options)
where TModule : IApplicationModule<TModuleOptions>, new()
where TModuleOptions : BaseModuleOptions, new()
{
foreach (var moduleRegistration in enabledModules)
{
moduleRegistration.PostConfigureServices(BootApplicationContext, Services);
}
}

protected void LogInternal(string message) => InternalLogger.LogInformation("Check log: {Message}", message);
Expand Down

0 comments on commit dec43ec

Please sign in to comment.