adding Data Requirement on XContent Service ...
This commit is contained in:
@@ -0,0 +1,237 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using xCommons.Extensions;
|
||||
using xContentService.Constants;
|
||||
using xContentService.Providers.Dtos;
|
||||
using xContentService.Providers.Entities;
|
||||
using xDataService.Constants;
|
||||
using xDataService.Models;
|
||||
using xPushService.DI;
|
||||
using xDataService.DI;
|
||||
using xPushService.Helpers;
|
||||
using System;
|
||||
using xExceptions.Constants;
|
||||
using xContentService.Helpers;
|
||||
using xCommons.Helpers;
|
||||
using xContentService.Interfaces.Dtos;
|
||||
using xContentService.Interfaces.Entities;
|
||||
using xDataService.Db;
|
||||
|
||||
namespace xContentService.DI
|
||||
{
|
||||
/// <summary>
|
||||
/// Service Registration Extensions Classes ...
|
||||
/// </summary>
|
||||
public static partial class XDIHelperExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Register Service ...
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
/// <param name="repositoryType"></param>
|
||||
/// <param name="lifeTime"></param>
|
||||
public static void AddXContentService(
|
||||
this IServiceCollection services,
|
||||
XRepositoryType repositoryType,
|
||||
ServiceLifetime lifeTime = ServiceLifetime.Scoped
|
||||
)
|
||||
{
|
||||
AddContentService(
|
||||
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 AddXContentService<TContext>(
|
||||
this IServiceCollection services,
|
||||
XRepositoryType repositoryType,
|
||||
ServiceLifetime lifeTime = ServiceLifetime.Scoped
|
||||
)
|
||||
where TContext : XDbContext
|
||||
{
|
||||
AddContentService(
|
||||
services: services,
|
||||
lifeTime: lifeTime,
|
||||
contextType: typeof(TContext),
|
||||
repositoryType: repositoryType
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Use xContentService Middleware ...
|
||||
/// </summary>
|
||||
/// <param name="app"></param>
|
||||
/// <param name="isDevelopmentEnvironment"></param>
|
||||
public static void UseXContentService(
|
||||
this IApplicationBuilder app,
|
||||
bool isDevelopmentEnvironment = false
|
||||
)
|
||||
{
|
||||
//
|
||||
#region Using XContent Hubs ...
|
||||
//
|
||||
var pushHelper = new XPushServiceHelper();
|
||||
|
||||
//
|
||||
pushHelper.AddHub<XContentDtoHub>(XContentServiceConstants.XContentDtoHub);
|
||||
pushHelper.AddHub<XContentEntityHub>(XContentServiceConstants.XContentEntityHub);
|
||||
|
||||
//
|
||||
app.UseXPushService(pushHelper);
|
||||
#endregion
|
||||
|
||||
//
|
||||
// Using XContent 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 = XContentServiceConstants.XContentServiceDILogTag;
|
||||
|
||||
/// <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 AddContentService(
|
||||
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(XContentServiceHelper)
|
||||
.InvokeGenericMethod<XRepositoryDescriptor>(
|
||||
args: null,
|
||||
runtimeType: contextType,
|
||||
methodName: XContentServiceConstants.GetXContentEFRepositoryDescriptor
|
||||
);
|
||||
break;
|
||||
|
||||
//
|
||||
case XRepositoryType.Mongo:
|
||||
descriptor = XContentServiceHelper.GetXContentMongoRepositoryDescriptor();
|
||||
break;
|
||||
|
||||
//
|
||||
case XRepositoryType.InMemory:
|
||||
descriptor = XContentServiceHelper.GetXContentInMemoryRepositoryDescriptor();
|
||||
break;
|
||||
}
|
||||
|
||||
//
|
||||
// Register XContent Repository Descriptor ...
|
||||
services.AddXRepository(
|
||||
lifeTime: lifeTime,
|
||||
descriptor: descriptor
|
||||
);
|
||||
|
||||
//
|
||||
// Register Dto Services ...
|
||||
services.Add(
|
||||
new ServiceDescriptor(typeof(IXContentServiceProvider), typeof(XContentServiceProvider), lifeTime)
|
||||
);
|
||||
services.Add(
|
||||
new ServiceDescriptor(typeof(IXContentRepositoryService), typeof(XContentRepositoryService), lifeTime)
|
||||
);
|
||||
services.Add(
|
||||
new ServiceDescriptor(typeof(IXContentRepositoryProvider), typeof(XContentRepositoryProvider), lifeTime)
|
||||
);
|
||||
|
||||
//
|
||||
// Register Additional Service ...
|
||||
// services.Add(
|
||||
// new ServiceDescriptor(typeof(IXContentTagProvider), typeof(XContentTagProvider), lifeTime)
|
||||
// );
|
||||
// services.Add(
|
||||
// new ServiceDescriptor(typeof(IXContentTagProvided), typeof(XContentTagProvided), lifeTime)
|
||||
// );
|
||||
// services.Add(
|
||||
// new ServiceDescriptor(typeof(IXContentProvider), typeof(XContentProvider), lifeTime)
|
||||
// );
|
||||
|
||||
//
|
||||
Log("Service Regitration Succeed ...");
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user