262 lines
7.9 KiB
C#
262 lines
7.9 KiB
C#
using System;
|
|
using System.Linq;
|
|
using xExceptions.Models;
|
|
using xExceptions.Constants;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace xCommons.Extensions
|
|
{
|
|
/// <summary>
|
|
/// an Internal Class for Registering Keyed Services ...
|
|
/// </summary>
|
|
/// <typeparam name="TService"></typeparam>
|
|
internal sealed class XKeyedService<TService>
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Keyed Service Extensions Methods ...
|
|
/// </summary>
|
|
public static class XDIKeyedServiceExtensions
|
|
{
|
|
/// <summary>
|
|
/// Register Specified Keyed Service ...
|
|
/// </summary>
|
|
/// <typeparam name="TService"></typeparam>
|
|
/// <typeparam name="TImplementation"></typeparam>
|
|
/// <param name="services"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="lifetime"></param>
|
|
public static void AddXKeyedService<TService, TImplementation>(
|
|
this IServiceCollection services,
|
|
string key,
|
|
ServiceLifetime lifetime = ServiceLifetime.Scoped
|
|
)
|
|
where TService : class
|
|
where TImplementation : TService
|
|
{
|
|
//
|
|
services.AddXKeyedService<TService>(
|
|
key: key,
|
|
lifetime: lifetime,
|
|
factory: sp => ActivatorUtilities.CreateInstance<TImplementation>(sp)
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register Specified Keyed Service ...
|
|
/// </summary>
|
|
/// <typeparam name="TService"></typeparam>
|
|
/// <param name="services"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="lifetime"></param>
|
|
public static void AddXKeyedService<TService>(
|
|
this IServiceCollection services,
|
|
string key,
|
|
ServiceLifetime lifetime = ServiceLifetime.Scoped
|
|
)
|
|
where TService : class
|
|
{
|
|
//
|
|
services.AddXKeyedService<TService>(
|
|
key: key,
|
|
lifetime: lifetime,
|
|
factory: sp => ActivatorUtilities.CreateInstance<TService>(sp)
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register Specified Keyed Service ...
|
|
/// </summary>
|
|
/// <typeparam name="TService"></typeparam>
|
|
/// <param name="services"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="factory"></param>
|
|
/// <param name="lifetime"></param>
|
|
public static void AddXKeyedService<TService>(
|
|
this IServiceCollection services,
|
|
string key,
|
|
Func<IServiceProvider, TService> 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<TService>),
|
|
sp => new XKeyedService<TService>(key, factory(sp)),
|
|
lifetime));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve Registered XKeyedService ...
|
|
/// </summary>
|
|
/// <typeparam name="TService"></typeparam>
|
|
/// <param name="provider"></param>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
public static TService GetXKeyedService<TService>(
|
|
this IServiceProvider provider,
|
|
string key
|
|
)
|
|
where TService : class
|
|
{
|
|
//
|
|
if (provider.IsNull())
|
|
{
|
|
XException.InvalidArgs.Throw("Invalid Provider ...");
|
|
}
|
|
|
|
//
|
|
var result = provider
|
|
.GetServices<XKeyedService<TService>>()
|
|
.LastOrDefault(x => x.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase))
|
|
?.Instance;
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve Registered XKeyedService ...
|
|
/// </summary>
|
|
/// <typeparam name="TService"></typeparam>
|
|
/// <param name="provider"></param>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
public static TService GetXKeyedService<TService>(
|
|
this IServiceCollection provider,
|
|
string key
|
|
)
|
|
where TService : class
|
|
{
|
|
//
|
|
if (provider.IsNull())
|
|
{
|
|
XException.InvalidArgs.Throw("Invalid Provider ...");
|
|
}
|
|
|
|
//
|
|
var result = provider
|
|
.GetRegisteredServices<XKeyedService<TService>>()
|
|
.LastOrDefault(x => x.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase))
|
|
?.Instance;
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve Registered XKeyedService ...
|
|
/// </summary>
|
|
/// <typeparam name="TService"></typeparam>
|
|
/// <param name="provider"></param>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
public static TService GetRequiredXKeyedService<TService>(
|
|
this IServiceProvider provider,
|
|
string key
|
|
)
|
|
where TService : class
|
|
{
|
|
//
|
|
var service = provider.GetXKeyedService<TService>(key);
|
|
if (service == null)
|
|
{
|
|
//
|
|
XException.InvalidArgs.Throw(
|
|
$"Service '{typeof(TService).FullName}' " +
|
|
$"with key '{key}' is not registered.");
|
|
}
|
|
|
|
//
|
|
return service;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve Registered XKeyedService ...
|
|
/// </summary>
|
|
/// <typeparam name="TService"></typeparam>
|
|
/// <param name="provider"></param>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
public static TService GetRequiredXKeyedService<TService>(
|
|
this IServiceCollection provider,
|
|
string key
|
|
)
|
|
where TService : class
|
|
{
|
|
//
|
|
var service = provider.GetXKeyedService<TService>(key);
|
|
if (service == null)
|
|
{
|
|
//
|
|
XException.InvalidArgs.Throw(
|
|
$"Service '{typeof(TService).FullName}' " +
|
|
$"with key '{key}' is not registered.");
|
|
}
|
|
|
|
//
|
|
return service;
|
|
}
|
|
}
|
|
} |