342 lines
11 KiB
C#
342 lines
11 KiB
C#
using System;
|
|
using xDataService.Db;
|
|
using xDataService.DI;
|
|
using xPushService.DI;
|
|
using xCommons.Helpers;
|
|
using xCommons.Extensions;
|
|
using xDataService.Models;
|
|
using xPushService.Helpers;
|
|
using xExceptions.Constants;
|
|
using xBotService.Constants;
|
|
using xBotService.Providers;
|
|
using xDataService.Constants;
|
|
using xBotService.Interfaces;
|
|
using XBotUserService.Helpers;
|
|
using xHttpService.Interfaces;
|
|
using xBotService.Configuration;
|
|
using xBotService.Providers.Dtos;
|
|
using xBotService.Interfaces.Dtos;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using xBotService.Providers.Entities;
|
|
using xBotService.Interfaces.Entities;
|
|
using Microsoft.Extensions.Configuration;
|
|
using xBotService.Interfaces.Entities.Dtos;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using ConfigurationNodeNames = xBotService.Constants.ConfigurationNodeNames;
|
|
|
|
namespace xBotService.DI
|
|
{
|
|
public static class XDIHelperExtension
|
|
{
|
|
/// <summary>
|
|
/// Retrieve Bot Service Configuration ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static XBotServiceConfiguration GetXBotServiceConfiguration(
|
|
this IConfiguration source
|
|
)
|
|
{
|
|
//
|
|
var section = source
|
|
.GetSection(ConfigurationNodeNames.BOT_SERVICE_NODE);
|
|
|
|
//
|
|
var result = section.Get<XBotServiceConfiguration>();
|
|
if (result.IsNull())
|
|
{
|
|
result = new();
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extract Service Configurations and Register it ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="configuration"></param>
|
|
public static void AddXBotServiceConfiguration(
|
|
this IServiceCollection source,
|
|
IConfiguration configuration
|
|
)
|
|
{
|
|
//
|
|
var config = configuration.GetXBotServiceConfiguration();
|
|
source.AddXBotServiceConfiguration(config);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register Service Configuration ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="configuration"></param>
|
|
public static void AddXBotServiceConfiguration(
|
|
this IServiceCollection source,
|
|
XBotServiceConfiguration configuration
|
|
)
|
|
{
|
|
//
|
|
if (configuration.IsNullOrDefault())
|
|
{
|
|
//
|
|
Log("Service Registration faild, Invalid Configuration ...");
|
|
XException.InvalidConfiguration.Throw();
|
|
}
|
|
|
|
//
|
|
source.AddSingleton(configuration);
|
|
Log("Service Configuration, Registered ...");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register Service ...
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="configuration"></param>
|
|
/// <param name="repositoryType"></param>
|
|
/// <param name="lifeTime"></param>
|
|
public static void AddXBotService(
|
|
this IServiceCollection services,
|
|
IConfiguration configuration,
|
|
XRepositoryType repositoryType,
|
|
ServiceLifetime lifeTime = ServiceLifetime.Scoped
|
|
)
|
|
{
|
|
//
|
|
var config = configuration.GetXBotServiceConfiguration();
|
|
|
|
//
|
|
AddBotService(
|
|
contextType: null,
|
|
services: services,
|
|
lifeTime: lifeTime,
|
|
configuration: config,
|
|
repositoryType: repositoryType
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register Service with Context ...
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="configuration"></param>
|
|
/// <param name="repositoryType"></param>
|
|
/// <param name="lifeTime"></param>
|
|
/// <typeparam name="TContext"></typeparam>
|
|
public static void AddXBotService<TContext>(
|
|
this IServiceCollection services,
|
|
IConfiguration configuration,
|
|
XRepositoryType repositoryType,
|
|
ServiceLifetime lifeTime = ServiceLifetime.Scoped
|
|
) where TContext : XDbContext
|
|
{
|
|
//
|
|
var config = configuration.GetXBotServiceConfiguration();
|
|
|
|
//
|
|
AddBotService(
|
|
services: services,
|
|
lifeTime: lifeTime,
|
|
configuration: config,
|
|
contextType: typeof(TContext),
|
|
repositoryType: repositoryType
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register Service ...
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="configuration"></param>
|
|
/// <param name="repositoryType"></param>
|
|
/// <param name="lifeTime"></param>
|
|
public static void AddXBotService(
|
|
this IServiceCollection services,
|
|
XBotServiceConfiguration configuration,
|
|
XRepositoryType repositoryType,
|
|
ServiceLifetime lifeTime = ServiceLifetime.Scoped
|
|
)
|
|
{
|
|
//
|
|
AddBotService(
|
|
contextType: null,
|
|
services: services,
|
|
lifeTime: lifeTime,
|
|
configuration: configuration,
|
|
repositoryType: repositoryType
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register Service with Context ...
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="configuration"></param>
|
|
/// <param name="repositoryType"></param>
|
|
/// <param name="lifeTime"></param>
|
|
/// <typeparam name="TContext"></typeparam>
|
|
public static void AddXBotService<TContext>(
|
|
this IServiceCollection services,
|
|
XBotServiceConfiguration configuration,
|
|
XRepositoryType repositoryType,
|
|
ServiceLifetime lifeTime = ServiceLifetime.Scoped
|
|
) where TContext : XDbContext
|
|
{
|
|
//
|
|
AddBotService(
|
|
services: services,
|
|
lifeTime: lifeTime,
|
|
configuration: configuration,
|
|
contextType: typeof(TContext),
|
|
repositoryType: repositoryType
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Use XBotService Middleware ...
|
|
/// </summary>
|
|
/// <param name="app"></param>
|
|
/// <param name="isDevelopmentEnvironment"></param>
|
|
public static void UseXBotService(
|
|
this IApplicationBuilder app,
|
|
bool isDevelopmentEnvironment = false
|
|
)
|
|
{
|
|
//
|
|
#region Using XTag Hubs ...
|
|
//
|
|
var pushHelper = new XPushServiceHelper();
|
|
|
|
//
|
|
pushHelper.AddHub<XBotUserDtoHub>(XBotServiceConstants.XBotUserDtoHub);
|
|
pushHelper.AddHub<XBotUserEntityHub>(XBotServiceConstants.XBotUserEntityHub);
|
|
|
|
//
|
|
app.UseXPushService(pushHelper);
|
|
#endregion
|
|
|
|
//
|
|
// Using XBotUser Repository Descriptor ...
|
|
if (!botUserDescriptor.IsNull())
|
|
{
|
|
//
|
|
app.UseXRepository(
|
|
descriptor: botUserDescriptor,
|
|
isDevelopmentEnvironment: isDevelopmentEnvironment
|
|
);
|
|
}
|
|
}
|
|
|
|
//
|
|
#region Private ...
|
|
private static XRepositoryDescriptor botUserDescriptor = null;
|
|
private static string XLogTag = XBotServiceConstants.XBotServiceDILogTag;
|
|
|
|
private static void Log(string message)
|
|
{
|
|
Console.WriteLine($"{XLogTag} => {message}");
|
|
}
|
|
|
|
private static void AddBotService(
|
|
IServiceCollection services,
|
|
XBotServiceConfiguration configuration,
|
|
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 Configuration ...
|
|
services.AddXBotServiceConfiguration(configuration);
|
|
|
|
//
|
|
// 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:
|
|
botUserDescriptor = typeof(XBotServiceHelper)
|
|
.InvokeGenericMethod<XRepositoryDescriptor>(
|
|
args: null,
|
|
runtimeType: contextType,
|
|
methodName: XBotServiceConstants.GetXBotUserEFRepositoryDescriptor
|
|
);
|
|
break;
|
|
|
|
//
|
|
case XRepositoryType.Mongo:
|
|
botUserDescriptor = XBotServiceHelper.GetXBotUserMongoRepositoryDescriptor();
|
|
break;
|
|
|
|
//
|
|
case XRepositoryType.InMemory:
|
|
botUserDescriptor = XBotServiceHelper.GetXBotUserInMemoryRepositoryDescriptor();
|
|
break;
|
|
}
|
|
|
|
//
|
|
// Register XBotUser Repository Descriptor ...
|
|
services.AddXRepository(
|
|
lifeTime: lifeTime,
|
|
descriptor: botUserDescriptor
|
|
);
|
|
|
|
//
|
|
// Register Dto Services ...
|
|
services.Add(
|
|
new ServiceDescriptor(typeof(IXBotUserServiceProvider), typeof(XBotUserServiceProvider), lifeTime)
|
|
);
|
|
services.Add(
|
|
new ServiceDescriptor(typeof(IXBotUserRepositoryService), typeof(XBotUserRepositoryService), lifeTime)
|
|
);
|
|
services.Add(
|
|
new ServiceDescriptor(typeof(IXBotUserRepositoryProvider), typeof(XBotUserRepositoryProvider), lifeTime)
|
|
);
|
|
|
|
//
|
|
services.Add(
|
|
new ServiceDescriptor(typeof(IXBotProvider), typeof(XBotProvider), lifeTime)
|
|
);
|
|
|
|
//
|
|
Log("Service Registration Succeed ...");
|
|
}
|
|
#endregion
|
|
}
|
|
} |