117 lines
3.3 KiB
C#
117 lines
3.3 KiB
C#
using System;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using xCommons.Extensions;
|
|
using xExceptions.Constants;
|
|
using xFileService.Interfaces;
|
|
using xFileService.Interfaces.Entities;
|
|
using xFileService.Providers;
|
|
using xStorageService.Interfaces;
|
|
|
|
namespace xFileService.DI
|
|
{
|
|
public static class XDIHelperExtension
|
|
{
|
|
/// <summary>
|
|
/// Register Service ...
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="config"></param>
|
|
public static void AddXFileService(
|
|
this IServiceCollection services,
|
|
ServiceLifetime lifeTime
|
|
)
|
|
{
|
|
//
|
|
AddFileService(
|
|
services,
|
|
lifeTime
|
|
);
|
|
}
|
|
|
|
//
|
|
#region Private ...
|
|
/// <summary>
|
|
/// a LogTag for Service ...
|
|
/// </summary>
|
|
private static string XLogTag = "XFileService";
|
|
|
|
/// <summary>
|
|
/// print a log in Console ...
|
|
/// </summary>
|
|
/// <param name="message"></param>
|
|
private static void Log(string message)
|
|
{
|
|
Console.WriteLine($"{XLogTag} => {message}");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register Service ...
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="lifeTime"></param>
|
|
private static void AddFileService(
|
|
IServiceCollection services,
|
|
ServiceLifetime lifeTime
|
|
)
|
|
{
|
|
//
|
|
var exception = XException.InvalidArgs.ToException(); ;
|
|
|
|
//
|
|
// Services ...
|
|
var isServicesExists = !services.IsNull();
|
|
if (!isServicesExists)
|
|
{
|
|
//
|
|
Log("AddFileService failed, services not provided ...");
|
|
throw exception;
|
|
}
|
|
|
|
//
|
|
// Lifetime ...
|
|
var isLifetimeExists = !lifeTime.IsNull();
|
|
if (!isLifetimeExists)
|
|
{
|
|
//
|
|
Log("AddFileService failed, lifetime not provided ...");
|
|
throw exception;
|
|
}
|
|
|
|
//
|
|
// Check Dependencies Exists ...
|
|
var isDependenciesPassed = !services
|
|
.GetRegisteredService<IXStorageProvider>()
|
|
.IsNull();
|
|
if (!isDependenciesPassed)
|
|
{
|
|
//
|
|
Log("AddFileService failed due Dependencies Issues, IXStorageProvider not provided ...");
|
|
throw exception;
|
|
}
|
|
|
|
//
|
|
// Check Repository Exists ...
|
|
var isRepositoryExists = !services
|
|
.GetRegisteredService<IXFileRepository>()
|
|
.IsNull();
|
|
if (!isRepositoryExists)
|
|
{
|
|
//
|
|
Log("AddFileService failed, IXFileRepository not provided ...");
|
|
throw exception;
|
|
}
|
|
|
|
//
|
|
// Register Tag Provider for Files ...
|
|
services.Add(new ServiceDescriptor(typeof(IXFileTagProvider), typeof(XFileTagProvider), lifeTime));
|
|
|
|
//
|
|
// Register Main Provider ...
|
|
services.Add(new ServiceDescriptor(typeof(IXFileProvider), typeof(XFileProvider), lifeTime));
|
|
|
|
//
|
|
Log("AddFileService Succeed ...");
|
|
}
|
|
#endregion
|
|
}
|
|
} |