429 lines
16 KiB
C#
429 lines
16 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using xAiModels.Providers.Entities;
|
|
using xDataService.Constants;
|
|
using xDataService.Db;
|
|
using xPushService.Helpers;
|
|
|
|
namespace xAiModels.DI
|
|
{
|
|
public static class XDIHelperExtension
|
|
{
|
|
/// <summary>
|
|
/// Register Service ...
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="repositoryType"></param>
|
|
/// <param name="lifeTime"></param>
|
|
public static void AddXAiDataServiceService(
|
|
this IServiceCollection services,
|
|
XRepositoryType repositoryType,
|
|
ServiceLifetime lifeTime = ServiceLifetime.Scoped
|
|
)
|
|
{
|
|
AddAiDataServiceService(
|
|
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 AddXAiDataServiceService<TContext>(
|
|
this IServiceCollection services,
|
|
XRepositoryType repositoryType,
|
|
ServiceLifetime lifeTime = ServiceLifetime.Scoped
|
|
)
|
|
where TContext : XDbContext
|
|
{
|
|
AddAiDataServiceService(
|
|
services: services,
|
|
lifeTime: lifeTime,
|
|
contextType: typeof(TContext),
|
|
repositoryType: repositoryType
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Use XSubCategoryService Middleware ...
|
|
/// </summary>
|
|
/// <param name="app"></param>
|
|
/// <param name="isDevelopmentEnvironment"></param>
|
|
public static void UseXAiDataServiceService(
|
|
this IApplicationBuilder app,
|
|
bool isDevelopmentEnvironment = false
|
|
)
|
|
{
|
|
//
|
|
#region Using Hubs ...
|
|
//
|
|
var pushHelper = new XPushServiceHelper();
|
|
|
|
//
|
|
// XAiConversation ...
|
|
pushHelper.AddHub<XAiConversationEntityHub>("categoryDto");
|
|
pushHelper.AddHub<XAiDataServiceEntityHub>("categoryEntity");
|
|
|
|
//
|
|
// SubCategory ...
|
|
pushHelper.AddHub<XSubCategoryDtoHub>("subCategoryDto");
|
|
pushHelper.AddHub<XSubCategoryEntityHub>("subCategoryEntity");
|
|
|
|
//
|
|
app.UseXPushService(pushHelper);
|
|
#endregion
|
|
|
|
//
|
|
// Using XAiDataService Repository Descriptor ...
|
|
if (!categoryDescriptor.IsNull())
|
|
{
|
|
//
|
|
app.UseXRepository(
|
|
descriptor: categoryDescriptor,
|
|
isDevelopmentEnvironment: isDevelopmentEnvironment
|
|
);
|
|
}
|
|
|
|
//
|
|
// Using XSubCategory Repository Descriptor ...
|
|
if (!subCategoryDescriptor.IsNull())
|
|
{
|
|
//
|
|
app.UseXRepository(
|
|
descriptor: subCategoryDescriptor,
|
|
isDevelopmentEnvironment: isDevelopmentEnvironment
|
|
);
|
|
}
|
|
|
|
//
|
|
// Create an Scope for Services Acces ...
|
|
// Handle Seeding Category/SubCategory Resources ...
|
|
using (var scope = app.ApplicationServices.CreateScope())
|
|
{
|
|
//
|
|
// Retrieve Configuratuions ...
|
|
var categoryProvider = scope.ServiceProvider.GetService<IXAiDataServiceProvider>();
|
|
var subCategoryProvider = scope.ServiceProvider.GetService<IXSubCategoryProvider>();
|
|
var configuration = scope.ServiceProvider.GetService<XAiDataServiceServiceConfiguration>();
|
|
if (!configuration.IsNullOrDefault() &&
|
|
!categoryProvider.IsNullOrDefault() &&
|
|
!subCategoryDescriptor.IsNullOrDefault()
|
|
)
|
|
{
|
|
//
|
|
// Logging ...
|
|
Log("Start Seeding ...");
|
|
|
|
//
|
|
// Count Data in Category ...
|
|
var hasCategory = (categoryProvider.Count().GetAwaiter().GetResult()) > 0;
|
|
var hasSubCategory = (subCategoryProvider.Count().GetAwaiter().GetResult()) > 0;
|
|
|
|
//
|
|
// Seed Category Resources ...
|
|
var canSeed =
|
|
!hasCategory &&
|
|
configuration.Categories.HasChild();
|
|
if (canSeed)
|
|
{
|
|
//
|
|
// Loop through Provided items ...
|
|
foreach (var cat in configuration.Categories)
|
|
{
|
|
//
|
|
try
|
|
{
|
|
//
|
|
Log($"Start Seeding Category: {cat.Titles.Locales.ToArray()[0].Value} ...");
|
|
|
|
//
|
|
categoryProvider.Refresh(
|
|
resource: cat,
|
|
userInfo: null,
|
|
connectionId: null,
|
|
cancellationToken: default
|
|
)
|
|
.GetAwaiter()
|
|
.GetResult();
|
|
|
|
//
|
|
Log($"Seeding Category: {cat.Titles.Locales.ToArray()[0].Value} Finished Successfully ...");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log($"Seeding Category: {cat.Titles.Locales.ToArray()[0].Value} Failed due: {ex.Message} ...");
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
// Seed SubCategory Resources ...
|
|
canSeed =
|
|
!hasSubCategory &&
|
|
configuration.SubCategories.HasChild();
|
|
if (canSeed)
|
|
{
|
|
//
|
|
// Loop through Provided items ...
|
|
foreach (var subcat in configuration.SubCategories)
|
|
{
|
|
//
|
|
try
|
|
{
|
|
//
|
|
Log($"Start Seeding SubCategory: {subcat.Titles.Locales.ToArray()[0].Value} ...");
|
|
|
|
//
|
|
subCategoryProvider.Refresh(
|
|
userInfo: null,
|
|
resource: subcat,
|
|
connectionId: null,
|
|
cancellationToken: default
|
|
)
|
|
.GetAwaiter()
|
|
.GetResult();
|
|
|
|
//
|
|
Log($"Seeding SubCategory: {subcat.Titles.Locales.ToArray()[0].Value} Finished Successfully ...");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log($"Seeding SubCategory: {subcat.Titles.Locales.ToArray()[0].Value} Failed due: {ex.Message} ...");
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
Log("Finish Seeding ...");
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
#region Private ...
|
|
private static XRepositoryDescriptor categoryDescriptor = null;
|
|
private static XRepositoryDescriptor subCategoryDescriptor = null;
|
|
|
|
/// <summary>
|
|
/// a LogTag for Service ...
|
|
/// </summary>
|
|
private static string XLogTag = "XAiDataServiceService";
|
|
|
|
/// <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 AddCategoryService(
|
|
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:
|
|
//
|
|
// Category ...
|
|
categoryDescriptor = typeof(XAiDataServiceServiceHelper)
|
|
.InvokeGenericMethod<XRepositoryDescriptor>(
|
|
methodName: "GetXAiDataServiceEFRepositoryDescriptor",
|
|
runtimeType: contextType,
|
|
args: null
|
|
);
|
|
|
|
//
|
|
// SubCategory ...
|
|
subCategoryDescriptor = typeof(XAiDataServiceServiceHelper)
|
|
.InvokeGenericMethod<XRepositoryDescriptor>(
|
|
methodName: "GetXSubCategoryEFRepositoryDescriptor",
|
|
runtimeType: contextType,
|
|
args: null
|
|
);
|
|
break;
|
|
|
|
//
|
|
case XRepositoryType.Mongo:
|
|
//
|
|
// Category ...
|
|
categoryDescriptor = XAiDataServiceServiceHelper.GetXAiDataServiceMongoRepositoryDescriptor();
|
|
|
|
//
|
|
// SubCategory ...
|
|
subCategoryDescriptor = XAiDataServiceServiceHelper.GetXSubCategoryMongoRepositoryDescriptor();
|
|
break;
|
|
|
|
//
|
|
case XRepositoryType.InMemory:
|
|
//
|
|
// Category ...
|
|
categoryDescriptor = XAiDataServiceServiceHelper.GetXAiDataServiceInMemoryRepositoryDescriptor();
|
|
|
|
//
|
|
// SubCategory ...
|
|
subCategoryDescriptor = XAiDataServiceServiceHelper.GetXSubCategoryInMemoryRepositoryDescriptor();
|
|
break;
|
|
}
|
|
|
|
//
|
|
// Register Category Repository Descriptor ...
|
|
if (!categoryDescriptor.IsNull())
|
|
{
|
|
//
|
|
services.AddXRepository(
|
|
lifeTime: lifeTime,
|
|
descriptor: categoryDescriptor
|
|
);
|
|
}
|
|
|
|
//
|
|
// Register SubCategory Repository Descriptor ...
|
|
if (!subCategoryDescriptor.IsNull())
|
|
{
|
|
//
|
|
services.AddXRepository(
|
|
lifeTime: lifeTime,
|
|
descriptor: subCategoryDescriptor
|
|
);
|
|
}
|
|
|
|
//
|
|
#region Register Services ...
|
|
//
|
|
#region Category ...
|
|
//
|
|
// Models ...
|
|
services.Add(
|
|
new ServiceDescriptor(typeof(IXAiDataServiceServiceProvider), typeof(XAiDataServiceServiceProvider), lifeTime)
|
|
);
|
|
services.Add(
|
|
new ServiceDescriptor(typeof(IXAiDataServiceRepositoryService), typeof(XAiDataServiceRepositoryService), lifeTime)
|
|
);
|
|
services.Add(
|
|
new ServiceDescriptor(typeof(IXAiDataServiceRepositoryProvider), typeof(XAiDataServiceRepositoryProvider), lifeTime)
|
|
);
|
|
|
|
//
|
|
// Resource Providers ...
|
|
services.Add(
|
|
new ServiceDescriptor(typeof(IXAiDataServiceTitleResourceProvider), typeof(XAiDataServiceTitleResourceProvider), lifeTime)
|
|
);
|
|
services.Add(
|
|
new ServiceDescriptor(typeof(IXAiDataServiceDescriptionResourceProvider), typeof(XAiDataServiceDescriptionResourceProvider), lifeTime)
|
|
);
|
|
|
|
//
|
|
// Reference Providers ...
|
|
services.Add(
|
|
new ServiceDescriptor(typeof(IXAiDataServiceSubCategoryReferenceProvider), typeof(XAiDataServiceSubCategoryReferenceProvider), lifeTime)
|
|
);
|
|
|
|
//
|
|
// Provider ...
|
|
services.Add(
|
|
new ServiceDescriptor(typeof(IXAiDataServiceProvider), typeof(XAiDataServiceProvider), lifeTime)
|
|
);
|
|
#endregion
|
|
|
|
//
|
|
#region SubCategory ...
|
|
//
|
|
// Models ...
|
|
services.Add(
|
|
new ServiceDescriptor(typeof(IXSubCategoryServiceProvider), typeof(XSubCategoryServiceProvider), lifeTime)
|
|
);
|
|
services.Add(
|
|
new ServiceDescriptor(typeof(IXSubCategoryRepositoryService), typeof(XSubCategoryRepositoryService), lifeTime)
|
|
);
|
|
services.Add(
|
|
new ServiceDescriptor(typeof(IXSubCategoryRepositoryProvider), typeof(XSubCategoryRepositoryProvider), lifeTime)
|
|
);
|
|
|
|
//
|
|
// Resource Providers ...
|
|
services.Add(
|
|
new ServiceDescriptor(typeof(IXSubCategoryTitleResourceProvider), typeof(XSubCategoryTitleResourceProvider), lifeTime)
|
|
);
|
|
services.Add(
|
|
new ServiceDescriptor(typeof(IXSubCategoryDescriptionResourceProvider), typeof(XSubCategoryDescriptionResourceProvider), lifeTime)
|
|
);
|
|
|
|
//
|
|
// Provider ...
|
|
services.Add(
|
|
new ServiceDescriptor(typeof(IXSubCategoryProvider), typeof(XSubCategoryProvider), lifeTime)
|
|
);
|
|
#endregion
|
|
#endregion
|
|
|
|
//
|
|
Log("Service Regitration Succeed ...");
|
|
}
|
|
#endregion
|
|
}
|
|
} |