159 lines
5.3 KiB
C#
159 lines
5.3 KiB
C#
using System;
|
|
using xDataService.Db;
|
|
using xDataService.DI;
|
|
using xCommons.Helpers;
|
|
using xCommons.Extensions;
|
|
using xDataService.Models;
|
|
using xExceptions.Constants;
|
|
using xBotService.Constants;
|
|
using xDataService.Constants;
|
|
using XBotUserService.Helpers;
|
|
using xHttpService.Interfaces;
|
|
using xBotService.Providers.Dtos;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using xBotService.Interfaces.Entities.Dtos;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace xBotService.DI
|
|
{
|
|
public static class XDIHelperExtension
|
|
{
|
|
/// <summary>
|
|
/// Register XBotService
|
|
/// </summary>
|
|
public static void AddXBotService(
|
|
this IServiceCollection services,
|
|
XRepositoryType repositoryType,
|
|
ServiceLifetime lifeTime = ServiceLifetime.Scoped
|
|
)
|
|
{
|
|
AddBotService(
|
|
contextType: null,
|
|
services: services,
|
|
lifeTime: lifeTime,
|
|
repositoryType: repositoryType
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register XBotService with Context
|
|
/// </summary>
|
|
public static void AddXBotService<TContext>(
|
|
this IServiceCollection services,
|
|
XRepositoryType repositoryType,
|
|
ServiceLifetime lifeTime = ServiceLifetime.Scoped
|
|
) where TContext : XDbContext
|
|
{
|
|
AddBotService(
|
|
services: services,
|
|
lifeTime: lifeTime,
|
|
contextType: typeof(TContext),
|
|
repositoryType: repositoryType
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Use XBotService Middleware
|
|
/// </summary>
|
|
public static void UseXBotService(
|
|
this IApplicationBuilder app,
|
|
bool isDevelopmentEnvironment = false
|
|
)
|
|
{
|
|
// Using XBot Repository Descriptor
|
|
if (!descriptor.IsNull())
|
|
{
|
|
app.UseXRepository(
|
|
descriptor: descriptor,
|
|
isDevelopmentEnvironment: isDevelopmentEnvironment
|
|
);
|
|
}
|
|
}
|
|
|
|
//
|
|
#region Private ...
|
|
private static XRepositoryDescriptor descriptor = null;
|
|
private static string XLogTag = XBotServiceConstants.XBotServiceDILogTag;
|
|
|
|
private static void Log(string message)
|
|
{
|
|
Console.WriteLine($"{XLogTag} => {message}");
|
|
}
|
|
|
|
private static void AddBotService(
|
|
IServiceCollection services,
|
|
XRepositoryType repositoryType,
|
|
Type contextType = null,
|
|
ServiceLifetime lifeTime = ServiceLifetime.Scoped
|
|
)
|
|
{
|
|
//
|
|
var exception = XException.InvalidArgs.ToException();
|
|
|
|
//
|
|
// Validate Services
|
|
if (services.IsNull())
|
|
{
|
|
//
|
|
Log("Service Registration failed, services not provided ...");
|
|
throw exception;
|
|
}
|
|
|
|
//
|
|
// Check Requirements ...
|
|
var httpService = services.GetRegisteredService<IXHttpProvider>();
|
|
if (httpService.IsNull())
|
|
{
|
|
//
|
|
Log("Service Registration failed, HttpService 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;
|
|
}
|
|
|
|
//
|
|
// Make Instance of Repository Descriptor
|
|
switch (repositoryType)
|
|
{
|
|
case XRepositoryType.EF:
|
|
descriptor = typeof(XBotServiceHelper)
|
|
.InvokeGenericMethod<XRepositoryDescriptor>(
|
|
args: null,
|
|
runtimeType: contextType,
|
|
methodName: XBotServiceConstants.GetXBotUserEFRepositoryDescriptor
|
|
);
|
|
break;
|
|
case XRepositoryType.Mongo:
|
|
// descriptor = XBotServiceHelper.GetXBotUserMongoRepositoryDescriptor();
|
|
Log("MongoDB support not implemented yet for XBotService.");
|
|
break;
|
|
case XRepositoryType.InMemory:
|
|
// descriptor = XBotServiceHelper.GetXBotUserInMemoryRepositoryDescriptor();
|
|
Log("InMemory support not implemented yet for XBotService.");
|
|
break;
|
|
}
|
|
|
|
//
|
|
// Register XBotUser Repository Descriptor
|
|
if (!descriptor.IsNull())
|
|
{
|
|
services.AddXRepository(lifeTime: lifeTime, descriptor: descriptor);
|
|
}
|
|
|
|
//
|
|
// Register Dto Services
|
|
services.Add(new ServiceDescriptor(typeof(IXBotUserRepositoryService), typeof(XBotUserRepositoryService), lifeTime));
|
|
Log("XBotService Registration Succeed ...");
|
|
}
|
|
#endregion
|
|
}
|
|
} |