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:
@@ -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
|
||||
@@ -38,8 +38,4 @@
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Messaging\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using DigitalData.MessagingService.RabbitMQ;
|
||||
using DigitalData.MessagingService.Infrastructure.Options;
|
||||
using DigitalData.MessagingService.RabbitMQ;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
@@ -8,7 +9,7 @@ public class MessagingServiceInfrastructureConfiguration
|
||||
{
|
||||
internal IServiceCollection Services { get; }
|
||||
|
||||
private bool _rabbitMqConnectionFactoryAdded = false;
|
||||
private (bool RabbitMqConnectionFactory, bool EmailAccounts) _configured = (false, false);
|
||||
|
||||
internal MessagingServiceInfrastructureConfiguration(IServiceCollection services)
|
||||
{
|
||||
@@ -17,24 +18,44 @@ public class MessagingServiceInfrastructureConfiguration
|
||||
|
||||
public void EnsureRabbitMqConnectionFactoryAdded()
|
||||
{
|
||||
if (!_rabbitMqConnectionFactoryAdded)
|
||||
if (!_configured.RabbitMqConnectionFactory)
|
||||
{
|
||||
throw new InvalidOperationException("RabbitMQ connection factory has not been added. Please call AddRabbitMqConnectionFactory() before using this configuration.");
|
||||
}
|
||||
if(!_configured.EmailAccounts)
|
||||
{
|
||||
throw new InvalidOperationException("Email accounts options have not been configured. Please call ConfigureEmailAccountsOptions() or ConfigureEmailAccounts() before using this configuration.");
|
||||
}
|
||||
}
|
||||
|
||||
public MessagingServiceInfrastructureConfiguration AddRabbitMqConnectionFactory(Action<RabbitMqConfiguration> configure)
|
||||
{
|
||||
Services.AddRabbitMqConnectionFactory(configure);
|
||||
_rabbitMqConnectionFactoryAdded = true;
|
||||
_configured.RabbitMqConnectionFactory = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public MessagingServiceInfrastructureConfiguration AddRabbitMqConnectionFactory(IConfiguration configuration, string sectionName = RabbitMqConfiguration.SectionName)
|
||||
{
|
||||
Services.AddRabbitMqConnectionFactory(configuration, sectionName);
|
||||
_rabbitMqConnectionFactoryAdded = true;
|
||||
_configured.RabbitMqConnectionFactory = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
#region EmailAccountsOptions
|
||||
public MessagingServiceInfrastructureConfiguration ConfigureEmailAccountsOptions(Action<EmailAccountsOptions> configureOptions)
|
||||
{
|
||||
Services.Configure(configureOptions);
|
||||
_configured.EmailAccounts = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public MessagingServiceInfrastructureConfiguration ConfigureEmailAccounts(IConfiguration configuration, string sectionName = EmailAccountsOptions.SectionName)
|
||||
{
|
||||
Services.Configure<EmailAccountsOptions>(configuration.GetSection(sectionName));
|
||||
_configured.EmailAccounts = true;
|
||||
return this;
|
||||
}
|
||||
#endregion EmailAccountsOptions
|
||||
|
||||
}
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
#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;
|
||||
namespace DigitalData.MessagingService.Infrastructure.Options;
|
||||
|
||||
/// <summary>
|
||||
/// Wrapper options class that holds a list of <see cref="Dto.EmailAccount"/> entries
|
||||
@@ -68,7 +68,9 @@ try
|
||||
builder.Services.AddMessagingServiceApplication(builder.Configuration);
|
||||
|
||||
// Register Infrastructure layer (RabbitMQ, Repositories, etc.)
|
||||
builder.Services.AddInfrastructure(cfg => cfg.AddRabbitMqConnectionFactory(builder.Configuration));
|
||||
builder.Services.AddInfrastructure(cfg => cfg
|
||||
.AddRabbitMqConnectionFactory(builder.Configuration)
|
||||
.ConfigureEmailAccounts(builder.Configuration));
|
||||
|
||||
// Health checks — each check corresponds to a singleton registered in the Infrastructure layer
|
||||
builder.Services.AddHealthChecks()
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
#if NET
|
||||
namespace DigitalData.MessagingService.Client.DependencyInjection;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public static class DependencyInjectionExtensions
|
||||
{
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,10 @@
|
||||
#if NET
|
||||
namespace DigitalData.MessagingService.Client.DependencyInjection;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class MessagingServiceConfiguration
|
||||
{
|
||||
}
|
||||
#endif
|
||||
@@ -61,7 +61,7 @@
|
||||
|
||||
<Target Name="EmbedProjectReferences" DependsOnTargets="ResolveReferences">
|
||||
<ItemGroup>
|
||||
<BuildOutputInPackage Include="@(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget','ProjectReference'))" />
|
||||
<BuildOutputInPackage Include="@(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget','ProjectReference'))" />
|
||||
</ItemGroup>
|
||||
</Target>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user