Skip to content

Commit

Permalink
feat(app): Allow to hook into init process
Browse files Browse the repository at this point in the history
  • Loading branch information
SonicGD committed Dec 6, 2023
1 parent 6af5db8 commit 968960f
Showing 1 changed file with 42 additions and 22 deletions.
64 changes: 42 additions & 22 deletions src/Sitko.Core.App/SitkoCoreBaseApplicationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,30 @@ public abstract class SitkoCoreBaseApplicationBuilder : ISitkoCoreApplicationBui
private readonly List<ApplicationModuleRegistration> moduleRegistrations = new();
private readonly SerilogConfigurator serilogConfigurator = new();

private IApplicationContext? bootApplicationContext;

private ILogger<ISitkoCoreApplicationBuilder>? internalLogger;

protected SitkoCoreBaseApplicationBuilder(string[] args, IServiceCollection services,
IConfigurationBuilder configuration, IApplicationEnvironment environment, ILoggingBuilder logging)
{
Args = args;
Services = services;
Configuration = configuration;
Environment = environment;
Logging = logging;
var argsProvider = new ApplicationArgsProvider(args);
var bootConfig = Configuration.Build();
BootApplicationContext = new BuilderApplicationContext(bootConfig, Environment, argsProvider);

// configure logging
Configuration.Add(new SerilogDynamicConfigurationSource());
InternalLogger = CreateInternalLogger();
Logging.ClearProviders();
Logging.AddSerilog();
serilogConfigurator.Configure(ConfigureDefautLogger);

AddModule<CommandsModule>();

Services.AddSingleton<IApplicationArgsProvider>(argsProvider);
Services.AddSingleton(environment);
Services.AddSingleton<IApplicationContext, BuilderApplicationContext>();
Services.AddTransient<IScheduler, Scheduler>();
Services.AddFluentValidationExtensions();
Services.AddTransient(typeof(ILocalizationProvider<>), typeof(LocalizationProvider<>));
Services.AddHostedService<HostedLifecycleService>(); // только Hosted? Проверить для Wasm
Init();
}

protected ILogger<ISitkoCoreApplicationBuilder> InternalLogger { get; }
protected IApplicationContext BootApplicationContext { get; }
protected ILogger<ISitkoCoreApplicationBuilder> InternalLogger => internalLogger ??
throw new InvalidOperationException(
"Application init is not executed");

protected IApplicationContext BootApplicationContext => bootApplicationContext ??
throw new InvalidOperationException(
"Application init is not executed");

public string[] Args { get; }
protected IServiceCollection Services { get; }
protected IConfigurationBuilder Configuration { get; }
protected IApplicationEnvironment Environment { get; }
Expand Down Expand Up @@ -103,6 +95,31 @@ public ISitkoCoreApplicationBuilder AddModule<TModule, TModuleOptions>(
public bool HasModule<TModule>() where TModule : IApplicationModule =>
moduleRegistrations.Any(r => r.Type == typeof(TModule));

private void Init()
{
var argsProvider = new ApplicationArgsProvider(Args);
SetupConfiguration(Configuration);
var bootConfig = Configuration.Build();
bootApplicationContext = new BuilderApplicationContext(bootConfig, Environment, argsProvider);

// configure logging
Configuration.Add(new SerilogDynamicConfigurationSource());
internalLogger = CreateInternalLogger();
Logging.ClearProviders();
Logging.AddSerilog();
serilogConfigurator.Configure(ConfigureDefautLogger);

AddModule<CommandsModule>();

Services.AddSingleton<IApplicationArgsProvider>(argsProvider);
Services.AddSingleton(Environment);
Services.AddSingleton<IApplicationContext, BuilderApplicationContext>();
Services.AddTransient<IScheduler, Scheduler>();
Services.AddFluentValidationExtensions();
Services.AddTransient(typeof(ILocalizationProvider<>), typeof(LocalizationProvider<>));
Services.AddHostedService<HostedLifecycleService>(); // только Hosted? Проверить для Wasm
}

private ILogger<ISitkoCoreApplicationBuilder> CreateInternalLogger()
{
var tmpLoggerConfiguration = new LoggerConfiguration();
Expand Down Expand Up @@ -162,6 +179,9 @@ protected virtual void AfterModuleRegistration<TModule, TModuleOptions>(IApplica
{
}

protected virtual IConfigurationBuilder SetupConfiguration(IConfigurationBuilder configurationBuilder) =>
configurationBuilder;

protected virtual LoggerConfiguration ConfigureDefautLogger(LoggerConfiguration loggerConfiguration)
{
loggerConfiguration = loggerConfiguration.ReadFrom.Configuration(BootApplicationContext.Configuration);
Expand Down

0 comments on commit 968960f

Please sign in to comment.