last works on Keyed Services ...
This commit is contained in:
@@ -151,6 +151,74 @@ namespace xCommons.Extensions
|
|||||||
lifetime));
|
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>
|
/// <summary>
|
||||||
/// Retrieve Registered XKeyedService ...
|
/// Retrieve Registered XKeyedService ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user