Files
xFileService/DI/XDIHelperExtension.cs
T
2026-05-28 11:39:34 +03:30

235 lines
7.4 KiB
C#

using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using xCommons.Extensions;
using xCommons.Helpers;
using xDataService.Constants;
using xDataService.Db;
using xDataService.DI;
using xDataService.Models;
using xExceptions.Constants;
using xPushService.DI;
using xPushService.Helpers;
using xFileService.Helpers;
using xFileService.Interfaces.Dtos;
using xFileService.Interfaces.Entities;
using xFileService.Providers.Dtos;
using xFileService.Providers.Entities;
using xFileService.Interfaces;
using xFileService.Providers;
namespace xFileService.DI
{
public static class XDIHelperExtension
{
/// <summary>
/// Register Service ...
/// </summary>
/// <param name="services"></param>
/// <param name="repositoryType"></param>
/// <param name="lifeTime"></param>
public static void AddXFileService(
this IServiceCollection services,
XRepositoryType repositoryType,
ServiceLifetime lifeTime = ServiceLifetime.Scoped
)
{
AddFileService(
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 AddXFileService<TContext>(
this IServiceCollection services,
XRepositoryType repositoryType,
ServiceLifetime lifeTime = ServiceLifetime.Scoped
)
where TContext : XDbContext
{
AddFileService(
services: services,
lifeTime: lifeTime,
contextType: typeof(TContext),
repositoryType: repositoryType
);
}
/// <summary>
/// Use XFileService Middleware ...
/// </summary>
/// <param name="app"></param>
/// <param name="isDevelopmentEnvironment"></param>
public static void UseXFileService(
this IApplicationBuilder app,
bool isDevelopmentEnvironment = false
)
{
//
#region Using XFile Hubs ...
//
var pushHelper = new XPushServiceHelper();
//
pushHelper.AddHub<XFileDtoHub>("fileDto");
pushHelper.AddHub<XFileEntityHub>("fileEntity");
//
app.UseXPushService(pushHelper);
#endregion
//
// Using XFile 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 = "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="repositoryType"></param>
/// <param name="contextType"></param>
/// <param name="lifeTime"></param>
private static void AddFileService(
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(XFileServiceHelper)
.InvokeGenericMethod<XRepositoryDescriptor>(
methodName: "GetXFileEFRepositoryDescriptor",
runtimeType: contextType,
args: null
);
break;
//
case XRepositoryType.Mongo:
descriptor = XFileServiceHelper.GetXFileMongoRepositoryDescriptor();
break;
//
case XRepositoryType.InMemory:
descriptor = XFileServiceHelper.GetXFileInMemoryRepositoryDescriptor();
break;
}
//
// Register XFile Repository Descriptor ...
services.AddXRepository(
lifeTime: lifeTime,
descriptor: descriptor
);
//
// Register Dto Services ...
services.Add(
new ServiceDescriptor(typeof(IXFileServiceProvider), typeof(XFileServiceProvider), lifeTime)
);
services.Add(
new ServiceDescriptor(typeof(IXFileRepositoryService), typeof(XFileRepositoryService), lifeTime)
);
services.Add(
new ServiceDescriptor(typeof(IXFileRepositoryProvider), typeof(XFileRepositoryProvider), lifeTime)
);
//
// Register Additional Service ...
services.Add(
new ServiceDescriptor(typeof(IXFileTagProvider), typeof(XFileTagProvider), lifeTime)
);
services.Add(
new ServiceDescriptor(typeof(IXFileTagProvided), typeof(XFileTagProvided), lifeTime)
);
services.Add(
new ServiceDescriptor(typeof(IXFileProvider), typeof(XFileProvider), lifeTime)
);
//
Log("Service Regitration Succeed ...");
}
#endregion
}
}