last ...
This commit is contained in:
+144
-23
@@ -1,36 +1,108 @@
|
||||
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 xTagService.Interfaces;
|
||||
using xPushService.DI;
|
||||
using xPushService.Helpers;
|
||||
using xTagService.Helpers;
|
||||
using xTagService.Interfaces.Dtos;
|
||||
using xTagService.Interfaces.Entities;
|
||||
using xTagService.Providers;
|
||||
using xTagService.Providers.Dtos;
|
||||
using xTagService.Providers.Entities;
|
||||
|
||||
namespace xTagService.DI
|
||||
{
|
||||
public static class XDIHelperExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// Register Module Provided Service on DI
|
||||
/// Register Service ...
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
/// <param name="config"></param>
|
||||
/// <param name="repositoryType"></param>
|
||||
/// <param name="lifeTime"></param>
|
||||
public static void AddXTagService(
|
||||
this IServiceCollection services,
|
||||
ServiceLifetime lifeTime
|
||||
XRepositoryType repositoryType,
|
||||
ServiceLifetime lifeTime = ServiceLifetime.Scoped
|
||||
)
|
||||
{
|
||||
AddTagService(
|
||||
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 AddXTagService<TContext>(
|
||||
this IServiceCollection services,
|
||||
XRepositoryType repositoryType,
|
||||
ServiceLifetime lifeTime = ServiceLifetime.Scoped
|
||||
)
|
||||
where TContext : XDbContext
|
||||
{
|
||||
AddTagService(
|
||||
services: services,
|
||||
lifeTime: lifeTime,
|
||||
contextType: typeof(TContext),
|
||||
repositoryType: repositoryType
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Use XTagService Middleware ...
|
||||
/// </summary>
|
||||
/// <param name="app"></param>
|
||||
/// <param name="isDevelopmentEnvironment"></param>
|
||||
public static void UseXTagService(
|
||||
this IApplicationBuilder app,
|
||||
bool isDevelopmentEnvironment = false
|
||||
)
|
||||
{
|
||||
//
|
||||
AddTagService(
|
||||
services,
|
||||
lifeTime
|
||||
);
|
||||
#region Using XTag Hubs ...
|
||||
//
|
||||
var pushHelper = new XPushServiceHelper();
|
||||
|
||||
//
|
||||
pushHelper.AddHub<XTagDtoHub>("stringDto");
|
||||
pushHelper.AddHub<XTagEntityHub>("stringEntity");
|
||||
|
||||
//
|
||||
app.UseXPushService(pushHelper);
|
||||
#endregion
|
||||
|
||||
//
|
||||
// Using XTag Repository Descriptor ...
|
||||
if (!descriptor.IsNull())
|
||||
{
|
||||
//
|
||||
app.UseXRepository(
|
||||
descriptor: descriptor,
|
||||
isDevelopmentEnvironment: isDevelopmentEnvironment
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
#region Private ...
|
||||
private static XRepositoryDescriptor descriptor = null;
|
||||
|
||||
/// <summary>
|
||||
/// a LogTag for XTagService ...
|
||||
/// a LogTag for Service ...
|
||||
/// </summary>
|
||||
private static string XLogTag = "XTagService";
|
||||
|
||||
@@ -44,13 +116,17 @@ namespace xTagService.DI
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register Push Service ...
|
||||
/// Register Service ...
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
/// <param name="repositoryType"></param>
|
||||
/// <param name="contextType"></param>
|
||||
/// <param name="lifeTime"></param>
|
||||
private static void AddTagService(
|
||||
IServiceCollection services,
|
||||
ServiceLifetime lifeTime
|
||||
XRepositoryType repositoryType,
|
||||
Type contextType = null,
|
||||
ServiceLifetime lifeTime = ServiceLifetime.Scoped
|
||||
)
|
||||
{
|
||||
//
|
||||
@@ -62,7 +138,7 @@ namespace xTagService.DI
|
||||
if (!isServicesExists)
|
||||
{
|
||||
//
|
||||
Log("AddTagService failed, services not provided ...");
|
||||
Log("Service Registration failed, services not provided ...");
|
||||
throw exception;
|
||||
}
|
||||
|
||||
@@ -72,28 +148,73 @@ namespace xTagService.DI
|
||||
if (!isLifetimeExists)
|
||||
{
|
||||
//
|
||||
Log("AddTagService failed, lifetime not provided ...");
|
||||
Log("Service Registration failed, lifetime not provided ...");
|
||||
throw exception;
|
||||
}
|
||||
|
||||
//
|
||||
// Check String Repository Exists ...
|
||||
var isRepositoryExists = !services
|
||||
.GetRegisteredService<IXTagRepository>()
|
||||
.IsNull();
|
||||
if (!isRepositoryExists)
|
||||
// Validate Repository Type ...
|
||||
var isRepositoryTypeValid =
|
||||
(repositoryType == XRepositoryType.EF &&
|
||||
!contextType.IsNull()) ||
|
||||
((repositoryType == XRepositoryType.Mongo ||
|
||||
repositoryType == XRepositoryType.InMemory) &&
|
||||
contextType.IsNull());
|
||||
if (!isRepositoryTypeValid)
|
||||
{
|
||||
//
|
||||
Log("AddTagService failed, IXTagRepository not provided ...");
|
||||
Log("Service Registration failed, Invalid Repository Type and Context Type ...");
|
||||
throw exception;
|
||||
}
|
||||
|
||||
//
|
||||
// Register Main Provider ...
|
||||
services.Add(new ServiceDescriptor(typeof(IXTagProvider), typeof(XTagProvider), lifeTime));
|
||||
// Maske Instance of Repository Descriptor
|
||||
// Based on Provided Type ...
|
||||
switch (repositoryType)
|
||||
{
|
||||
//
|
||||
case XRepositoryType.EF:
|
||||
//
|
||||
descriptor = typeof(XTagServiceHelper)
|
||||
.InvokeGenericMethod<XRepositoryDescriptor>(
|
||||
methodName: "GetXTagEFRepositoryDescriptor",
|
||||
runtimeType: contextType,
|
||||
args: null
|
||||
);
|
||||
break;
|
||||
|
||||
//
|
||||
case XRepositoryType.Mongo:
|
||||
descriptor = XTagServiceHelper.GetXTagMongoRepositoryDescriptor();
|
||||
break;
|
||||
|
||||
//
|
||||
case XRepositoryType.InMemory:
|
||||
descriptor = XTagServiceHelper.GetXTagInMemoryRepositoryDescriptor();
|
||||
break;
|
||||
}
|
||||
|
||||
//
|
||||
Log("AddTagService Succeed ...");
|
||||
// Register XTag Repository Descriptor ...
|
||||
services.AddXRepository(
|
||||
lifeTime: lifeTime,
|
||||
descriptor: descriptor
|
||||
);
|
||||
|
||||
//
|
||||
// Register Dto Services ...
|
||||
services.Add(
|
||||
new ServiceDescriptor(typeof(IXTagServiceProvider), typeof(XTagServiceProvider), lifeTime)
|
||||
);
|
||||
services.Add(
|
||||
new ServiceDescriptor(typeof(IXTagRepositoryService), typeof(XTagRepositoryService), lifeTime)
|
||||
);
|
||||
services.Add(
|
||||
new ServiceDescriptor(typeof(IXTagRepositoryProvider), typeof(XTagRepositoryProvider), lifeTime)
|
||||
);
|
||||
|
||||
//
|
||||
Log("Service Regitration Succeed ...");
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user