using System;
using System.Linq;
using xExceptions.Models;
using xExceptions.Constants;
using Microsoft.Extensions.DependencyInjection;
namespace xCommons.Extensions
{
///
/// an Internal Class for Registering Keyed Services ...
///
///
internal sealed class XKeyedService
where TService : class
{
public string Key { get; }
public TService Instance { get; }
public XKeyedService(
string key,
TService instance
)
{
//
if (key.IsNullOrEmpty() || instance.IsNull())
{
XException.InvalidArgs.Throw();
}
//
Key = key;
Instance = instance;
}
}
///
/// Keyed Service Extensions Methods ...
///
public static class XDIKeyedServiceExtensions
{
///
/// Register Specified Keyed Service ...
///
///
///
///
///
///
public static void AddXKeyedService(
this IServiceCollection services,
string key,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
where TService : class
where TImplementation : TService
{
//
services.AddXKeyedService(
key: key,
lifetime: lifetime,
factory: sp => ActivatorUtilities.CreateInstance(sp)
);
}
///
/// Register Specified Keyed Service ...
///
///
///
///
///
public static void AddXKeyedService(
this IServiceCollection services,
string key,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
where TService : class
{
//
services.AddXKeyedService(
key: key,
lifetime: lifetime,
factory: sp => ActivatorUtilities.CreateInstance(sp)
);
}
///
/// Register Specified Keyed Service ...
///
///
///
///
///
///
public static void AddXKeyedService(
this IServiceCollection services,
string key,
Func factory,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
where TService : class
{
//
var erorr = new XError();
//
var isKeyValid = !key.IsNullOrEmpty();
var isFactoryValid = !factory.IsNull();
var isServicesValid = !services.IsNull();
var isServiceLifetimeValid = Enum.IsDefined(typeof(ServiceLifetime), lifetime);
//
if (!isKeyValid ||
!isFactoryValid ||
!isServicesValid ||
!isServiceLifetimeValid
)
{
//
if (!isKeyValid)
{
erorr.Message = $"Key not Provided ...";
}
//
if (!isFactoryValid)
{
erorr.Message = $"Service Factory not Provided ...";
}
//
if (!isServicesValid)
{
erorr.Message = $"Services not Provided ...";
}
//
if (!isServiceLifetimeValid)
{
erorr.Message = $"invalid Service Lifetime ...";
}
//
erorr.Id = (int)XException.InvalidArgs;
throw erorr.ToException();
}
//
services.Add(new ServiceDescriptor(
typeof(XKeyedService),
sp => new XKeyedService(key, factory(sp)),
lifetime));
}
///
/// Register Specified Keyed Service ...
///
///
///
///
///
///
public static void AddXKeyedService(
this IServiceCollection services,
string key,
TService instance,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
where TService : class
{
//
var erorr = new XError();
//
var isKeyValid = !key.IsNullOrEmpty();
var isInstanceValid = !instance.IsNull();
var isServicesValid = !services.IsNull();
var isServiceLifetimeValid = Enum.IsDefined(typeof(ServiceLifetime), lifetime);
//
if (!isKeyValid ||
!isInstanceValid ||
!isServicesValid ||
!isServiceLifetimeValid
)
{
//
if (!isKeyValid)
{
erorr.Message = $"Key not Provided ...";
}
//
if (!isInstanceValid)
{
erorr.Message = $"Instance not Provided ...";
}
//
if (!isServicesValid)
{
erorr.Message = $"Services not Provided ...";
}
//
if (!isServiceLifetimeValid)
{
erorr.Message = $"invalid Service Lifetime ...";
}
//
erorr.Id = (int)XException.InvalidArgs;
throw erorr.ToException();
}
//
services.Add(new ServiceDescriptor(
typeof(XKeyedService),
sp => new XKeyedService(key, instance),
lifetime));
}
///
/// Retrieve Registered XKeyedService ...
///
///
///
///
///
public static TService GetXKeyedService(
this IServiceProvider provider,
string key
)
where TService : class
{
//
if (provider.IsNull())
{
XException.InvalidArgs.Throw("Invalid Provider ...");
}
//
var result = provider
.GetServices>()
.LastOrDefault(x => x.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase))
?.Instance;
return result;
}
///
/// Retrieve Registered XKeyedService ...
///
///
///
///
///
public static TService GetXKeyedService(
this IServiceCollection provider,
string key
)
where TService : class
{
//
if (provider.IsNull())
{
XException.InvalidArgs.Throw("Invalid Provider ...");
}
//
var result = provider
.GetRegisteredServices>()
.LastOrDefault(x => x.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase))
?.Instance;
return result;
}
///
/// Retrieve Registered XKeyedService ...
///
///
///
///
///
public static TService GetRequiredXKeyedService(
this IServiceProvider provider,
string key
)
where TService : class
{
//
var service = provider.GetXKeyedService(key);
if (service == null)
{
//
XException.InvalidArgs.Throw(
$"Service '{typeof(TService).FullName}' " +
$"with key '{key}' is not registered.");
}
//
return service;
}
///
/// Retrieve Registered XKeyedService ...
///
///
///
///
///
public static TService GetRequiredXKeyedService(
this IServiceCollection provider,
string key
)
where TService : class
{
//
var service = provider.GetXKeyedService(key);
if (service == null)
{
//
XException.InvalidArgs.Throw(
$"Service '{typeof(TService).FullName}' " +
$"with key '{key}' is not registered.");
}
//
return service;
}
}
}