From 2a7a932187a6b66904f28211ddceb60106ac600e Mon Sep 17 00:00:00 2001 From: Hazel K Date: Mon, 29 Jul 2024 14:11:48 -0400 Subject: [PATCH] chore: separate DI registration from startup (resolves #66) --- ModShark/ModSharkModule.cs | 62 ++++++++++++++++++++++++++++++++++++ ModShark/Program.cs | 47 +-------------------------- SharkeyDB/SharkeyDBModule.cs | 2 +- 3 files changed, 64 insertions(+), 47 deletions(-) create mode 100644 ModShark/ModSharkModule.cs diff --git a/ModShark/ModSharkModule.cs b/ModShark/ModSharkModule.cs new file mode 100644 index 0000000..1ca57bc --- /dev/null +++ b/ModShark/ModSharkModule.cs @@ -0,0 +1,62 @@ +using ModShark.Reports; +using ModShark.Reports.Reporter; +using ModShark.Rules; +using ModShark.Services; +using SharkeyDB; + +namespace ModShark; + +public static class ModSharkModule +{ + public static T AddModShark(this T services, IConfiguration configuration) + where T : IServiceCollection + { + // Register dependencies first. + // Order doesn't really matter, but it makes more sense conceptually. + services.AddSharkeyDB(); + + // Read and register config. + // This will be reworked later. + var config = configuration + .GetSection("ModShark") + .Get() + ?? throw new ApplicationException("Configuration file is invalid: could not map to the config object."); + services.AddSingleton(config.Postgres); + services.AddSingleton(config.Sharkey); + services.AddSingleton(config.Worker); + services.AddSingleton(config.Reporters.SendGrid); + services.AddSingleton(config.Reporters.Console); + services.AddSingleton(config.Reporters.Native); + services.AddSingleton(config.Reporters.Post); + services.AddSingleton(config.Rules.FlaggedUser); + services.AddSingleton(config.Rules.FlaggedInstance); + services.AddSingleton(config.Rules.FlaggedNote); + + // Register all services. + + services.AddHttpClient(); + services.AddScoped(); + + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + + services.AddSingleton(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + + services.AddSingleton(); + services.AddScoped(); + services.AddScoped(); + services.AddHostedService(); + + return services; + } +} \ No newline at end of file diff --git a/ModShark/Program.cs b/ModShark/Program.cs index 99dfe0e..214ec30 100644 --- a/ModShark/Program.cs +++ b/ModShark/Program.cs @@ -1,9 +1,4 @@ using ModShark; -using ModShark.Reports; -using ModShark.Reports.Reporter; -using ModShark.Rules; -using ModShark.Services; -using SharkeyDB; var builder = Host.CreateApplicationBuilder(args); @@ -33,47 +28,7 @@ if (builder.Environment.IsDevelopment()) builder.Configuration.AddJsonFile("appsettings.Local.json", optional: true); -// Read config -var config = builder.Configuration - .GetSection("ModShark") - .Get() - ?? throw new ApplicationException("Configuration file is invalid: could not map to the config object."); - -builder.Services.UseSharkeyDB(); - -builder.Services.AddSingleton(config.Postgres); -builder.Services.AddSingleton(config.Sharkey); -builder.Services.AddSingleton(config.Worker); -builder.Services.AddSingleton(config.Reporters.SendGrid); -builder.Services.AddSingleton(config.Reporters.Console); -builder.Services.AddSingleton(config.Reporters.Native); -builder.Services.AddSingleton(config.Reporters.Post); -builder.Services.AddSingleton(config.Rules.FlaggedUser); -builder.Services.AddSingleton(config.Rules.FlaggedInstance); -builder.Services.AddSingleton(config.Rules.FlaggedNote); - -builder.Services.AddHttpClient(); -builder.Services.AddScoped(); - -builder.Services.AddSingleton(); -builder.Services.AddSingleton(); -builder.Services.AddSingleton(); - -builder.Services.AddSingleton(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); - -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); - -builder.Services.AddSingleton(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddHostedService(); +builder.Services.AddModShark(builder.Configuration); var host = builder.Build(); host.Run(); \ No newline at end of file diff --git a/SharkeyDB/SharkeyDBModule.cs b/SharkeyDB/SharkeyDBModule.cs index 1e36a9c..6c08d37 100644 --- a/SharkeyDB/SharkeyDBModule.cs +++ b/SharkeyDB/SharkeyDBModule.cs @@ -4,7 +4,7 @@ namespace SharkeyDB; public static class SharkeyDBModule { - public static T UseSharkeyDB(this T services) + public static T AddSharkeyDB(this T services) where T : IServiceCollection { services.AddDbContext();