using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using xCommons.Extensions;
using xEventService.Configuration;
using xEventService.Constants;
using xEventService.Extensions;
using xEventService.Interfaces;
using xEventService.Providers;
using xExceptions.Constants;
namespace xEventService.DI
{
public static partial class XDIHelperExtension
{
///
/// Extract EventService Configurations ...
///
///
///
public static XEventServiceConfiguration GetXEventServiceConfigurations(
this IConfiguration source
)
{
//
var configSection = source
.GetSection(ConfigurationNodeNames.EVENT_SERVICE_NODE_NAME);
var result = configSection.Get();
//
return result;
}
///
/// Register Event Service Configurations ...
///
///
///
public static void AddXEventServiceConfigurations(
this IServiceCollection services,
IConfiguration configuration
)
{
//
var config = configuration.GetXEventServiceConfigurations();
services.AddXEventServiceConfigurations(config);
}
///
/// Register Event Service Configurations ...
///
///
///
public static void AddXEventServiceConfigurations(
this IServiceCollection services,
XEventServiceConfiguration configuration
)
{
//
// Validate ...
if (!configuration.IsValid())
{
//
Log("Registration Failed, due Invalid Configuration ...");
XException.InvalidConfiguration.Throw();
}
//
// Register Configuration as Singleton ...
services.AddSingleton(configuration);
}
///
/// Register Service ...
///
///
///
///
public static void AddXEventService(
this IServiceCollection source,
XEventServiceConfiguration configuration,
ServiceLifetime lifeTime = ServiceLifetime.Scoped
)
{
//
source.AddEventService(
lifeTime: lifeTime,
configuration: configuration
);
}
//
#region Private ...
///
/// a LogTag for Service ...
///
private static string XLogTag = XEventServiceConstants.XEventServiceDILogTag;
///
/// print a log in Console ...
///
///
private static void Log(string message)
{
Console.WriteLine($"{XLogTag} => {message}");
}
///
/// Register Event Service ...
///
///
///
///
private static void AddEventService(
this IServiceCollection source,
XEventServiceConfiguration configuration,
ServiceLifetime lifeTime = ServiceLifetime.Scoped
)
{
//
// Validate ...
if (!configuration.IsValid())
{
//
Log("Initialization Failed due Invalid Configuration ...");
XException.InvalidConfiguration.Throw();
}
//
// Register based on Broker ...
switch (configuration.Broker)
{
//
case XEventBroker.XKafka:
source.Add(
new ServiceDescriptor(typeof(IXKafkaProducerService), typeof(XKafkaProducerService), lifeTime)
);
break;
//
case XEventBroker.XZeroMQ:
source.Add(
new ServiceDescriptor(typeof(IXZeroMQProducerService), typeof(XZeroMQProducerService), lifeTime)
);
break;
//
case XEventBroker.XRabbitMQ:
source.Add(
new ServiceDescriptor(typeof(IXRabbitMQProducerService), typeof(XRabbitMQProducerService), lifeTime)
);
break;
}
//
source.Add(
new ServiceDescriptor(typeof(IXEventServiceProvider), typeof(XEventServiceProvider), lifeTime)
);
//
Log($"Service Registered Sucessfull, Provider: {configuration.Broker.GetStringValue()}");
}
#endregion
}
}