Files
Leanetec.EConnect/src/Leanetec.EConnect.Client/Client.cs

49 lines
2.0 KiB
C#

using MediatR;
using Microsoft.Extensions.DependencyInjection;
namespace Leanetec.EConnect.Client;
/// <summary>
/// Provides a lazily initialized <see cref="IServiceProvider"/> for accessing the EConnect client services,
/// including an <see cref="IMediator"/> instance for sending and publishing messages.
/// </summary>
public static class Client
{
private static Action<ClientOptions>? _options = null;
/// <summary>
/// Gets or sets the configuration options for the EConnect client.
/// Can only be set once; subsequent attempts to set will result in an exception.
/// </summary>
public static Action<ClientOptions> Options
{
get => _options ?? throw new InvalidOperationException("EConnect Client options have not been configured. Please set the 'Client.Options' property before accessing client services.");
set
{
if (_options is null)
_options = value;
else
throw new InvalidOperationException("EConnect Client options have already been configured. Reassignment is not allowed.");
}
}
/// <summary>
/// Lazily initializes the <see cref="IServiceProvider"/> that registers and builds the EConnect client services.
/// </summary>
private static readonly Lazy<IServiceProvider> _serviceProvider = new(() =>
{
var services = new ServiceCollection();
services.AddEConnectClient(Options);
return services.BuildServiceProvider();
});
/// <summary>
/// Gets the initialized <see cref="IServiceProvider"/> that provides access to registered EConnect client services.
/// </summary>
public static IServiceProvider Provider => _serviceProvider.Value;
/// <summary>
/// Gets the <see cref="IMediator"/> instance used for sending commands, queries, and publishing events within the EConnect client.
/// </summary>
public static IMediator Mediator => Provider.GetRequiredService<IMediator>();
}