Skip to content

Commit

Permalink
refactor configuration logic
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidEggenberger committed Oct 26, 2024
1 parent dceac27 commit 7f45343
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Modules.Subscriptions.Features.Infrastructure.StripePayments;
using Shared.Features.Modules.Configuration;
using Shared.Features.Configuration;
using Shared.Kernel.BuildingBlocks.Auth;

namespace Modules.Subscriptions.Features.Infrastructure.Configuration
{
public class SubscriptionsConfiguration : IModuleConfiguration
public class SubscriptionsConfiguration : ConfigurationObject
{
public string StripeAPIKey { get; set; }
public string StripeEndpointSecret { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using Microsoft.Extensions.Options;
using Shared.Features.Configuration;

namespace Modules.Subscriptions.Features.Infrastructure.Configuration
{
public class SubscriptionsConfigurationValidator : IValidateOptions<SubscriptionsConfiguration>
public class SubscriptionsConfigurationValidator : ConfigurationObjectValidator<SubscriptionsConfiguration>
{
public ValidateOptionsResult Validate(string name, SubscriptionsConfiguration options)
public override ValidateOptionsResult Validate(string name, SubscriptionsConfiguration options)
{
if (string.IsNullOrEmpty(options.StripeProfessionalPlanPriceId))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
using Microsoft.Extensions.Hosting;
using Modules.Subscriptions.Features.Infrastructure.Configuration;
using Modules.Subscriptions.Features.Infrastructure.EFCore;
using Shared.Features.Configuration;
using Shared.Features.EFCore;
using Shared.Features.Modules;
using Shared.Features.Modules.Configuration;
using Stripe;

namespace Modules.Subscriptions.Server
Expand All @@ -16,7 +16,7 @@ public class SubscriptionsModuleStartup : IModuleStartup
public void ConfigureServices(IServiceCollection services, IConfiguration config)
{
services.RegisterDbContext<SubscriptionsDbContext>();
services.RegisterModuleConfiguration<SubscriptionsConfiguration, SubscriptionsConfigurationValidator>(config);
services.RegisterConfiguration<SubscriptionsConfiguration, SubscriptionsConfigurationValidator>(config);

StripeConfiguration.ApiKey = services.BuildServiceProvider().GetRequiredService<SubscriptionsConfiguration>().StripeAPIKey;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Shared.Features.Modules.Configuration;
using Shared.Features.Configuration;

namespace Modules.TenantIdentity.Features.Infrastructure.Configuration
{
public class TenantIdentityConfiguration : IModuleConfiguration
public class TenantIdentityConfiguration : ConfigurationObject
{
public string GoogleClientId { get; set; }
public string GoogleClientSecret { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using Microsoft.Extensions.Options;
using Shared.Features.Configuration;

namespace Modules.TenantIdentity.Features.Infrastructure.Configuration
{
public class TenantIdentityConfigurationValidator : IValidateOptions<TenantIdentityConfiguration>
public class TenantIdentityConfigurationValidator : ConfigurationObjectValidator<TenantIdentityConfiguration>
{
public ValidateOptionsResult Validate(string name, TenantIdentityConfiguration tenantIdentityConfiguration)
public override ValidateOptionsResult Validate(string name, TenantIdentityConfiguration tenantIdentityConfiguration)
{
if (string.IsNullOrEmpty(tenantIdentityConfiguration.GoogleClientId))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
using Modules.TenantIdentity.Features.Infrastructure.Configuration;
using Modules.TenantIdentity.Features.DomainFeatures.UserAggregate;
using Shared.Features.EFCore;
using Shared.Features.Modules.Configuration;
using Shared.Features.Configuration;

namespace Modules.TenantIdentity.Server
{
Expand All @@ -28,7 +28,7 @@ public void ConfigureServices(IServiceCollection services, IConfiguration config
services.AddSingleton<OpenIdConnectPostConfigureOptions>();
services.AddScoped<ContextUserClaimsPrincipalFactory<ApplicationUser>>();

services.RegisterModuleConfiguration<TenantIdentityConfiguration, TenantIdentityConfigurationValidator>(configuration);
services.RegisterConfiguration<TenantIdentityConfiguration, TenantIdentityConfigurationValidator>(configuration);

services.Configure<SecurityStampValidatorOptions>(options =>
{
Expand Down
6 changes: 6 additions & 0 deletions Source/Shared/Features/Configuration/ConfigurationObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Shared.Features.Configuration
{
public class ConfigurationObject
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Microsoft.Extensions.Options;

namespace Shared.Features.Configuration
{
public abstract class ConfigurationObjectValidator<TConfiguration> : IValidateOptions<TConfiguration> where TConfiguration : ConfigurationObject
{
public abstract ValidateOptionsResult Validate(string name, TConfiguration options);
}
}
38 changes: 38 additions & 0 deletions Source/Shared/Features/Configuration/Registrator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace Shared.Features.Configuration
{
public static class Registrator
{
public static IServiceCollection RegisterConfiguration<TConfiguration>(this IServiceCollection services, IConfiguration configuration) where TConfiguration : ConfigurationObject, new()
{
services.AddScoped(sp =>
{
TConfiguration tc = new TConfiguration();
configuration.GetSection(typeof(TConfiguration).Name).Bind(tc);
return tc;
});
services.Configure<TConfiguration>(configuration.GetSection(nameof(TConfiguration)));

return services;
}

public static IServiceCollection RegisterConfiguration<TConfiguration, TConfigurationValidator>(this IServiceCollection services, IConfiguration configuration)
where TConfiguration : ConfigurationObject, new()
where TConfigurationValidator : ConfigurationObjectValidator<TConfiguration>, new()
{
services.AddScoped(sp =>
{
TConfiguration tc = new TConfiguration();
configuration.GetSection(typeof(TConfiguration).Name).Bind(tc);
return tc;
});
services.Configure<TConfiguration>(configuration.GetSection(nameof(TConfiguration)));
services.AddSingleton<IValidateOptions<TConfiguration>, TConfigurationValidator>();

return services;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Shared.Features.Modules.Configuration;
using Shared.Features.Configuration;

namespace Shared.Features.EFCore.Configuration
{
public class EFCoreConfiguration : IModuleConfiguration
public class EFCoreConfiguration : ConfigurationObject
{
public string SQLServerConnectionString_Dev { get; set; }
public string SQLServerConnectionString_Prod { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using Microsoft.Extensions.Options;
using Shared.Features.Configuration;

namespace Shared.Features.EFCore.Configuration
{
internal class EFCoreConfigurationValidator : IValidateOptions<EFCoreConfiguration>
internal class EFCoreConfigurationValidator : ConfigurationObjectValidator<EFCoreConfiguration>
{
public ValidateOptionsResult Validate(string name, EFCoreConfiguration efCoreConfiguration)
public override ValidateOptionsResult Validate(string name, EFCoreConfiguration efCoreConfiguration)
{
if (string.IsNullOrEmpty(efCoreConfiguration.SQLServerConnectionString_Dev))
{
Expand Down
4 changes: 2 additions & 2 deletions Source/Shared/Features/EFCore/Registrator.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Shared.Features.Configuration;
using Shared.Features.EFCore.Configuration;
using Shared.Features.EFCore.DbUp;
using Shared.Features.Modules.Configuration;

namespace Shared.Features.EFCore
{
public static class Registrator
{
public static IServiceCollection AddEFCore(this IServiceCollection services, IConfiguration configuration)
{
services.RegisterModuleConfiguration<EFCoreConfiguration, EFCoreConfigurationValidator>(configuration);
services.RegisterConfiguration<EFCoreConfiguration, EFCoreConfigurationValidator>(configuration);
services.AddScoped<TransactionScopeMiddleware>();
services.AddDbUpMigration();

Check warning on line 16 in Source/Shared/Features/EFCore/Registrator.cs

View workflow job for this annotation

GitHub Actions / build

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

Expand Down

This file was deleted.

37 changes: 0 additions & 37 deletions Source/Shared/Features/Modules/Configuration/Registrator.cs

This file was deleted.

0 comments on commit 7f45343

Please sign in to comment.