last works on Keyed Services ...

This commit is contained in:
2026-11-24 08:25:21 +03:30
parent d5da835b71
commit e2de55e8ef
+68
View File
@@ -151,6 +151,74 @@ namespace xCommons.Extensions
lifetime));
}
/// <summary>
/// Register Specified Keyed Service ...
/// </summary>
/// <typeparam name="TService"></typeparam>
/// <param name="services"></param>
/// <param name="key"></param>
/// <param name="instance"></param>
/// <param name="lifetime"></param>
public static void AddXKeyedService<TService>(
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<TService>),
sp => new XKeyedService<TService>(key, instance),
lifetime));
}
/// <summary>
/// Retrieve Registered XKeyedService ...
/// </summary>