From d5da835b7151b8f9464807594f4303f78a95c322 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Mon, 23 Nov 2026 16:33:11 +0330 Subject: [PATCH] add Support for XKeyedService ... --- Extensions/DIExtensions.cs | 13 ++ Extensions/XDIKeyedServiceExtensions.cs | 262 ++++++++++++++++++++++++ 2 files changed, 275 insertions(+) create mode 100644 Extensions/XDIKeyedServiceExtensions.cs diff --git a/Extensions/DIExtensions.cs b/Extensions/DIExtensions.cs index 0842d45..bd86375 100644 --- a/Extensions/DIExtensions.cs +++ b/Extensions/DIExtensions.cs @@ -33,6 +33,19 @@ namespace xCommons.Extensions return serviceProvider.GetService(); } + /// + /// Inject Specific Registered Services from IServiceCollection + /// + /// + /// + /// + public static IEnumerable GetRegisteredServices(this IServiceCollection source) + { + // + var serviceProvider = source.BuildServiceProvider(); + return serviceProvider.GetServices(); + } + /// /// Register App Configuration as a Service /// diff --git a/Extensions/XDIKeyedServiceExtensions.cs b/Extensions/XDIKeyedServiceExtensions.cs new file mode 100644 index 0000000..a05a3f3 --- /dev/null +++ b/Extensions/XDIKeyedServiceExtensions.cs @@ -0,0 +1,262 @@ +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)); + } + + /// + /// 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; + } + } +} \ No newline at end of file