Refactor configuration handling

Refactored `MessagingServiceApplicationConfiguration` to simplify the tracking of the LuckyPennySoft license key configuration by replacing the `_configured` tuple with a boolean. Removed email account configuration methods from this class.

Updated `MessagingServiceInfrastructureConfiguration` to introduce a new `_configured` tuple for tracking `RabbitMqConnectionFactory` and `EmailAccounts` configurations. Added methods for configuring email accounts and updated the `EnsureRabbitMqConnectionFactoryAdded` method to include email account checks.

Changed the namespace of `EmailAccountsOptions.cs` to `DigitalData.MessagingService.Infrastructure.Options`.

Modified `Program.cs` to include email account configuration in the infrastructure setup.

Added new files `DependencyInjectionExtensions.cs` and `MessagingServiceConfiguration.cs` as placeholders for future extensions, wrapped in `#if NET` directives.

Removed an `<ItemGroup>` from `DigitalData.MessagingService.Infrastructure.csproj` and updated `DigitalData.MessagingService.Client.csproj` for XML compliance.
This commit is contained in:
2026-11-02 15:23:21 +01:00
parent 8ff8a9e58b
commit 82be3c5a4f
8 changed files with 54 additions and 51 deletions

View File

@@ -1,34 +0,0 @@
#if NET
using DigitalData.MessagingService.Application.Common.Dto;
using DigitalData.MessagingService.Application.Common.Dto.EmailAccounts;
using DigitalData.MessagingService.Application.EmailAccounts.Commands;
using DigitalData.MessagingService.Domain.Entities;
namespace DigitalData.MessagingService.Application.Common.Options;
/// <summary>
/// Wrapper options class that holds a list of <see cref="Dto.EmailAccount"/> entries
/// bound from the <c>EmailAccounts</c> configuration section.
/// </summary>
public class EmailAccountsOptions
{
public const string SectionName = "EmailAccounts";
/// <summary>
/// The list of configured email accounts.
/// </summary>
public required List<EmailAccountModificationDto> Accounts { get; set; } = [];
/// <summary>
/// How often the IMAP sync worker polls for new emails, in seconds.
/// Defaults to 300 seconds (5 minutes).
/// </summary>
public int SyncIntervalSeconds { get; set; } = 300;
/// <summary>
/// The minimum interval, in seconds, between forced IMAP sync operations regardless of idle state.
/// Defaults to 30 seconds (0.5 minute).
/// </summary>
public int ForcedSyncIntervalSeconds { get; set; } = 30;
}
#endif

View File

@@ -1,5 +1,4 @@
#if NET
using DigitalData.MessagingService.Application.Common.Options;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
@@ -9,7 +8,7 @@ public partial class MessagingServiceApplicationConfiguration
{
private IServiceCollection Services { get; }
private (bool LuckyPennySoftLicenseKey, bool EmailAccounts) _configured = (LuckyPennySoftLicenseKey: false, EmailAccounts: false);
private bool _luckyPennySoftLicenseKeyAdded = false;
internal MessagingServiceApplicationConfiguration(IServiceCollection services)
{
@@ -18,14 +17,10 @@ public partial class MessagingServiceApplicationConfiguration
internal void EnsureConfigured()
{
if (!_configured.LuckyPennySoftLicenseKey)
if (!_luckyPennySoftLicenseKeyAdded)
{
throw new InvalidOperationException("LuckyPennySoftLicenseKey is not configured. Please call SetLuckyPennySoftLicenseKey() or SetLuckyPennySoftLicenseKey(IConfiguration) in DI configuration.");
}
if (!_configured.EmailAccounts)
{
throw new InvalidOperationException("EmailAccountsOptions is not configured. Please call ConfigureEmailAccountsOptions() or ConfigureEmailAccounts(IConfiguration) in DI configuration.");
}
}
#region LuckyPennySoftLicenseKey
@@ -37,7 +32,7 @@ public partial class MessagingServiceApplicationConfiguration
throw new InvalidOperationException("LuckyPennySoftLicenseKey is not configured. Please call SetLuckyPennySoftLicenseKey() or SetLuckyPennySoftLicenseKey(IConfiguration) in DI configuration.");
LuckyPennySoftLicenseKey = licenseKey;
_configured.LuckyPennySoftLicenseKey = true;
_luckyPennySoftLicenseKeyAdded = true;
return this;
}
@@ -46,33 +41,5 @@ public partial class MessagingServiceApplicationConfiguration
return SetLuckyPennySoftLicenseKey(configuration.GetValue<string>(key) ?? string.Empty);
}
#endregion LuckyPennySoftLicenseKey
#region EmailAccountsOptions
/// <summary>
/// Ignores whether EmailAccountsOptions is configured or not. Use this method if you just want to publish messages to the messaging service; not to send
/// emails. If you want to consume the messaging service to send emails, you must configure EmailAccountsOptions using ConfigureEmailAccountsOptions() or
/// ConfigureEmailAccounts(IConfiguration).
/// </summary>
/// <returns></returns>
public MessagingServiceApplicationConfiguration PublishOnly()
{
_configured.EmailAccounts = true;
return this;
}
public MessagingServiceApplicationConfiguration ConfigureEmailAccountsOptions(Action<EmailAccountsOptions> configureOptions)
{
Services.Configure(configureOptions);
_configured.EmailAccounts = true;
return this;
}
public MessagingServiceApplicationConfiguration ConfigureEmailAccounts(IConfiguration configuration, string sectionName = EmailAccountsOptions.SectionName)
{
Services.Configure<EmailAccountsOptions>(configuration.GetSection(sectionName));
_configured.EmailAccounts = true;
return this;
}
#endregion EmailAccountsOptions
}
#endif