97 lines
2.7 KiB
C#
97 lines
2.7 KiB
C#
using System;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using xExceptions.Constants;
|
|
using xCommons.Extensions;
|
|
using xStringService.Interfaces.Entities;
|
|
|
|
namespace xStringService.DI
|
|
{
|
|
public static class XDIHelperExtension
|
|
{
|
|
/// <summary>
|
|
/// Register Service ...
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="config"></param>
|
|
public static void AddXStringService(
|
|
this IServiceCollection services,
|
|
ServiceLifetime lifeTime
|
|
)
|
|
{
|
|
AddStringService(
|
|
services,
|
|
lifeTime
|
|
);
|
|
}
|
|
|
|
//
|
|
#region Private ...
|
|
/// <summary>
|
|
/// a LogTag for Service ...
|
|
/// </summary>
|
|
private static string XLogTag = "XStringService";
|
|
|
|
/// <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 AddStringService(
|
|
IServiceCollection services,
|
|
ServiceLifetime lifeTime
|
|
)
|
|
{
|
|
//
|
|
var exception = XException.InvalidArgs.ToException(); ;
|
|
|
|
//
|
|
// Services ...
|
|
var isServicesExists = !services.IsNull();
|
|
if (!isServicesExists)
|
|
{
|
|
//
|
|
Log("AddStringService failed, services not provided ...");
|
|
throw exception;
|
|
}
|
|
|
|
//
|
|
// Lifetime ...
|
|
var isLifetimeExists = !lifeTime.IsNull();
|
|
if (!isLifetimeExists)
|
|
{
|
|
//
|
|
Log("AddStringService failed, lifetime not provided ...");
|
|
throw exception;
|
|
}
|
|
|
|
//
|
|
// Check Repository Exists ...
|
|
var isRepositoryExists = !services
|
|
.GetRegisteredService<IXStringRepository>()
|
|
.IsNull();
|
|
if (!isRepositoryExists)
|
|
{
|
|
//
|
|
Log("AddStringService failed, IXStringRepository not provided ...");
|
|
throw exception;
|
|
}
|
|
|
|
// //
|
|
// // Main Provider ...
|
|
// services.Add(new ServiceDescriptor(typeof(IXStringProvider), typeof(XStringProvider), lifeTime));
|
|
|
|
//
|
|
Log("AddStringService Succeed ...");
|
|
}
|
|
#endregion
|
|
}
|
|
} |