last ...
This commit is contained in:
+135
-33
@@ -1,12 +1,20 @@
|
||||
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 xFileService.Interfaces;
|
||||
using xPushService.DI;
|
||||
using xPushService.Helpers;
|
||||
using xFileService.Helpers;
|
||||
using xFileService.Interfaces.Dtos;
|
||||
using xFileService.Interfaces.Entities;
|
||||
using xFileService.Providers;
|
||||
using xStorageService.Interfaces;
|
||||
using xTagService.Interfaces;
|
||||
using xFileService.Providers.Dtos;
|
||||
using xFileService.Providers.Entities;
|
||||
|
||||
namespace xFileService.DI
|
||||
{
|
||||
@@ -16,21 +24,83 @@ namespace xFileService.DI
|
||||
/// Register Service ...
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
/// <param name="config"></param>
|
||||
/// <param name="repositoryType"></param>
|
||||
/// <param name="lifeTime"></param>
|
||||
public static void AddXFileService(
|
||||
this IServiceCollection services,
|
||||
ServiceLifetime lifeTime
|
||||
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
|
||||
)
|
||||
{
|
||||
//
|
||||
AddFileService(
|
||||
services,
|
||||
lifeTime
|
||||
);
|
||||
#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>
|
||||
@@ -49,10 +119,14 @@ namespace xFileService.DI
|
||||
/// 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,
|
||||
ServiceLifetime lifeTime
|
||||
XRepositoryType repositoryType,
|
||||
Type contextType = null,
|
||||
ServiceLifetime lifeTime = ServiceLifetime.Scoped
|
||||
)
|
||||
{
|
||||
//
|
||||
@@ -64,7 +138,7 @@ namespace xFileService.DI
|
||||
if (!isServicesExists)
|
||||
{
|
||||
//
|
||||
Log("AddFileService failed, services not provided ...");
|
||||
Log("Service Registration failed, services not provided ...");
|
||||
throw exception;
|
||||
}
|
||||
|
||||
@@ -74,46 +148,74 @@ namespace xFileService.DI
|
||||
if (!isLifetimeExists)
|
||||
{
|
||||
//
|
||||
Log("AddFileService failed, lifetime not provided ...");
|
||||
Log("Service Registration failed, lifetime not provided ...");
|
||||
throw exception;
|
||||
}
|
||||
|
||||
//
|
||||
// Check Dependencies Exists ...
|
||||
var isDependenciesPassed = !services
|
||||
.GetRegisteredService<IXStorageProvider>()
|
||||
.IsNull() &&
|
||||
!services.GetRegisteredService<IXTagProvider>().IsNull();
|
||||
if (!isDependenciesPassed)
|
||||
// Validate Repository Type ...
|
||||
var isRepositoryTypeValid =
|
||||
(repositoryType == XRepositoryType.EF &&
|
||||
!contextType.IsNull()) ||
|
||||
((repositoryType == XRepositoryType.Mongo ||
|
||||
repositoryType == XRepositoryType.InMemory) &&
|
||||
contextType.IsNull());
|
||||
if (!isRepositoryTypeValid)
|
||||
{
|
||||
//
|
||||
Log("AddFileService failed due Dependencies Issues, IXTagProvider, IXStorageProvider not provided ...");
|
||||
Log("Service Registration failed, Invalid Repository Type and Context Type ...");
|
||||
throw exception;
|
||||
}
|
||||
|
||||
//
|
||||
// Check Repository Exists ...
|
||||
var isRepositoryExists = !services
|
||||
.GetRegisteredService<IXFileRepository>()
|
||||
.IsNull();
|
||||
if (!isRepositoryExists)
|
||||
// Maske Instance of Repository Descriptor
|
||||
// Based on Provided Type ...
|
||||
switch (repositoryType)
|
||||
{
|
||||
//
|
||||
Log("AddFileService failed, IXFileRepository not provided ...");
|
||||
throw exception;
|
||||
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 Tag Provider for Files ...
|
||||
services.Add(new ServiceDescriptor(typeof(IXFileTagProvider), typeof(XFileTagProvider), lifeTime));
|
||||
// Register XFile Repository Descriptor ...
|
||||
services.AddXRepository(
|
||||
lifeTime: lifeTime,
|
||||
descriptor: descriptor
|
||||
);
|
||||
|
||||
//
|
||||
// Register Main Provider ...
|
||||
services.Add(new ServiceDescriptor(typeof(IXFileProvider), typeof(XFileProvider), lifeTime));
|
||||
// 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)
|
||||
);
|
||||
|
||||
//
|
||||
Log("AddFileService Succeed ...");
|
||||
Log("Service Regitration Succeed ...");
|
||||
}
|
||||
#endregion
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user