229 lines
7.2 KiB
C#
229 lines
7.2 KiB
C#
using System;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using xExceptions.Constants;
|
|
using xCommons.Extensions;
|
|
using xDataService.Constants;
|
|
using xDataService.Db;
|
|
using xDataService.DI;
|
|
using xDataService.Models;
|
|
using xStringService.Helpers;
|
|
using xCommons.Helpers;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using xStringService.Interfaces.Dtos;
|
|
using xStringService.Providers.Dtos;
|
|
using xStringService.Interfaces.Entities;
|
|
using xStringService.Providers.Entities;
|
|
using xPushService.Helpers;
|
|
using xPushService.DI;
|
|
using xStringService.Interfaces;
|
|
using xStringService.Providers;
|
|
|
|
namespace xStringService.DI
|
|
{
|
|
public static class XDIHelperExtension
|
|
{
|
|
/// <summary>
|
|
/// Register Service ...
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="repositoryType"></param>
|
|
/// <param name="lifeTime"></param>
|
|
public static void AddXStringService(
|
|
this IServiceCollection services,
|
|
XRepositoryType repositoryType,
|
|
ServiceLifetime lifeTime = ServiceLifetime.Scoped
|
|
)
|
|
{
|
|
AddStringService(
|
|
contextType: null,
|
|
services: services,
|
|
lifeTime: lifeTime,
|
|
repositoryType: repositoryType
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register Service ...
|
|
/// </summary>
|
|
/// <typeparam name="TContext"></typeparam>
|
|
/// <param name="services"></param>
|
|
/// <param name="repositoryType"></param>
|
|
/// <param name="lifeTime"></param>
|
|
public static void AddXStringService<TContext>(
|
|
this IServiceCollection services,
|
|
XRepositoryType repositoryType,
|
|
ServiceLifetime lifeTime = ServiceLifetime.Scoped
|
|
)
|
|
where TContext : XDbContext
|
|
{
|
|
AddStringService(
|
|
services: services,
|
|
lifeTime: lifeTime,
|
|
contextType: typeof(TContext),
|
|
repositoryType: repositoryType
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Use XStringService Middleware ...
|
|
/// </summary>
|
|
/// <param name="app"></param>
|
|
/// <param name="isDevelopmentEnvironment"></param>
|
|
public static void UseXStringService(
|
|
this IApplicationBuilder app,
|
|
bool isDevelopmentEnvironment = false
|
|
)
|
|
{
|
|
//
|
|
#region Using XString Hubs ...
|
|
//
|
|
var pushHelper = new XPushServiceHelper();
|
|
|
|
//
|
|
pushHelper.AddHub<XStringDtoHub>("stringDto");
|
|
pushHelper.AddHub<XStringEntityHub>("stringEntity");
|
|
|
|
//
|
|
app.UseXPushService(pushHelper);
|
|
#endregion
|
|
|
|
//
|
|
// Using XString Repository Descriptor ...
|
|
if (!descriptor.IsNull())
|
|
{
|
|
//
|
|
app.UseXRepository(
|
|
descriptor: descriptor,
|
|
isDevelopmentEnvironment: isDevelopmentEnvironment
|
|
);
|
|
}
|
|
}
|
|
|
|
//
|
|
#region Private ...
|
|
private static XRepositoryDescriptor descriptor = null;
|
|
|
|
/// <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="repositoryType"></param>
|
|
/// <param name="contextType"></param>
|
|
/// <param name="lifeTime"></param>
|
|
private static void AddStringService(
|
|
IServiceCollection services,
|
|
XRepositoryType repositoryType,
|
|
Type contextType = null,
|
|
ServiceLifetime lifeTime = ServiceLifetime.Scoped
|
|
)
|
|
{
|
|
//
|
|
var exception = XException.InvalidArgs.ToException(); ;
|
|
|
|
//
|
|
// Services ...
|
|
var isServicesExists = !services.IsNull();
|
|
if (!isServicesExists)
|
|
{
|
|
//
|
|
Log("Service Registration failed, services not provided ...");
|
|
throw exception;
|
|
}
|
|
|
|
//
|
|
// Lifetime ...
|
|
var isLifetimeExists = !lifeTime.IsNull();
|
|
if (!isLifetimeExists)
|
|
{
|
|
//
|
|
Log("Service Registration failed, lifetime not provided ...");
|
|
throw exception;
|
|
}
|
|
|
|
//
|
|
// Validate Repository Type ...
|
|
var isRepositoryTypeValid =
|
|
(repositoryType == XRepositoryType.EF &&
|
|
!contextType.IsNull()) ||
|
|
((repositoryType == XRepositoryType.Mongo ||
|
|
repositoryType == XRepositoryType.InMemory) &&
|
|
contextType.IsNull());
|
|
if (!isRepositoryTypeValid)
|
|
{
|
|
//
|
|
Log("Service Registration failed, Invalid Repository Type and Context Type ...");
|
|
throw exception;
|
|
}
|
|
|
|
//
|
|
// Maske Instance of Repository Descriptor
|
|
// Based on Provided Type ...
|
|
switch(repositoryType)
|
|
{
|
|
//
|
|
case XRepositoryType.EF:
|
|
//
|
|
descriptor = typeof(XStringServiceHelper)
|
|
.InvokeGenericMethod<XRepositoryDescriptor>(
|
|
methodName: "GetXStringEFRepositoryDescriptor",
|
|
runtimeType: contextType,
|
|
args: null
|
|
);
|
|
break;
|
|
|
|
//
|
|
case XRepositoryType.Mongo:
|
|
descriptor = XStringServiceHelper.GetXStringMongoRepositoryDescriptor();
|
|
break;
|
|
|
|
//
|
|
case XRepositoryType.InMemory:
|
|
descriptor = XStringServiceHelper.GetXStringInMemoryRepositoryDescriptor();
|
|
break;
|
|
}
|
|
|
|
//
|
|
// Register XString Repository Descriptor ...
|
|
services.AddXRepository(
|
|
lifeTime: lifeTime,
|
|
descriptor: descriptor
|
|
);
|
|
|
|
//
|
|
// Register Dto Services ...
|
|
services.Add(
|
|
new ServiceDescriptor(typeof(IXStringServiceProvider), typeof(XStringServiceProvider), lifeTime)
|
|
);
|
|
services.Add(
|
|
new ServiceDescriptor(typeof(IXStringRepositoryService), typeof(XStringRepositoryService), lifeTime)
|
|
);
|
|
services.Add(
|
|
new ServiceDescriptor(typeof(IXStringRepositoryProvider), typeof(XStringRepositoryProvider), lifeTime)
|
|
);
|
|
|
|
//
|
|
// Register XString Provider ...
|
|
services.Add(
|
|
new ServiceDescriptor(typeof(IXStringProvider), typeof(XStringProvider), lifeTime)
|
|
);
|
|
|
|
//
|
|
Log("Service Regitration Succeed ...");
|
|
}
|
|
#endregion
|
|
}
|
|
} |