using MediatR;
using Microsoft.Extensions.DependencyInjection;
namespace Leanetec.EConnect.Client;
///
/// Provides a lazily initialized for accessing the EConnect client services,
/// including an instance for sending and publishing messages.
///
public static class Client
{
private static Action? _options = null;
///
/// Gets or sets the configuration options for the EConnect client.
/// Can only be set once; subsequent attempts to set will result in an exception.
///
public static Action 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.");
}
}
///
/// Lazily initializes the that registers and builds the EConnect client services.
///
private static readonly Lazy _serviceProvider = new(() =>
{
var services = new ServiceCollection();
services.AddEConnectClient(Options);
return services.BuildServiceProvider();
});
///
/// Gets the initialized that provides access to registered EConnect client services.
///
public static IServiceProvider Provider => _serviceProvider.Value;
///
/// Gets the instance used for sending commands, queries, and publishing events within the EConnect client.
///
public static IMediator Mediator => Provider.GetRequiredService();
}