40 lines
1.7 KiB
C#
40 lines
1.7 KiB
C#
using Microsoft.Extensions.DependencyInjection;
|
|
using xCommons.Extensions;
|
|
using xExceptions.Constants;
|
|
using xModels.Base;
|
|
using xModels.Interfaces;
|
|
|
|
namespace xModels.Extensions {
|
|
public static class DIExtensions {
|
|
/// <summary>
|
|
/// Register an Specific Store in DI ...
|
|
/// </summary>
|
|
/// <param name="services">DI service collection ...</param>
|
|
/// <param name="store">an implementation of inetrface of store ...</param>
|
|
/// <param name="lifeTime">service lifetime</param>
|
|
/// <typeparam name="TImplementation">implementation of TType ...</typeparam>
|
|
/// <typeparam name="TType">an inetrface which implements IXBaseStore<TStoreItemDto, TKey></typeparam>
|
|
/// <typeparam name="TStoreItemDto">type of an instance of XBaseStorable<TKey></typeparam>
|
|
/// <typeparam name="TKey">type of key of StorableItem</typeparam>
|
|
/// <returns></returns>
|
|
public static void AddXStore<TImplementation, TType, TStoreItemDto, TKey> (
|
|
this IServiceCollection services,
|
|
TImplementation store,
|
|
ServiceLifetime lifeTime = ServiceLifetime.Singleton
|
|
)
|
|
where TImplementation : TType
|
|
where TType : IXBaseStore<TStoreItemDto, TKey>
|
|
where TStoreItemDto : XBaseStorableDto<TKey> {
|
|
//
|
|
// Validate Args ...
|
|
if (store.IsNull ()) {
|
|
throw XException.InvalidArgs
|
|
.ToException ();
|
|
}
|
|
|
|
//
|
|
// Register Service in DI ...
|
|
services.Add (new ServiceDescriptor (typeof (TType), typeof (TImplementation), lifeTime));
|
|
}
|
|
}
|
|
} |