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 xWebinarService.Constants; using xWebinarService.Helpers; using xWebinarService.Interfaces.Dtos; using xWebinarService.Interfaces.Entities; using xWebinarService.Providers.Dtos; using xWebinarService.Providers.Entities; namespace xWebinarService.DI { public static class XDIHelperExtension { /// /// Register Service ... /// /// /// /// public static void AddXWebinarService( this IServiceCollection services, XRepositoryType repositoryType, ServiceLifetime lifeTime = ServiceLifetime.Scoped ) { AddWebinarService( contextType: null, services: services, lifeTime: lifeTime, repositoryType: repositoryType ); } /// /// Register Service ... /// /// /// /// /// public static void AddXWebinarService( this IServiceCollection services, XRepositoryType repositoryType, ServiceLifetime lifeTime = ServiceLifetime.Scoped ) where TContext : XDbContext { AddWebinarService( services: services, lifeTime: lifeTime, contextType: typeof(TContext), repositoryType: repositoryType ); } /// /// Use XWebinarService Middleware ... /// /// /// public static void UsexWebinarService( this IApplicationBuilder app, bool isDevelopmentEnvironment = false ) { // #region Using XWebinar Hubs ... // var pushHelper = new XPushServiceHelper(); // pushHelper.AddHub(XWebinarServiceConstants.XWebinarDtoHub); pushHelper.AddHub(XWebinarServiceConstants.XWebinarEntityHub); // pushHelper.AddHub(XWebinarServiceConstants.XWebinarSubscriberDtoHub); pushHelper.AddHub(XWebinarServiceConstants.XWebinarSubscriberEntityHub); // app.UseXPushService(pushHelper); #endregion // // Using XWebinar Repository Descriptor ... if (!webinarDescriptor.IsNull()) { // app.UseXRepository( descriptor: webinarDescriptor, isDevelopmentEnvironment: isDevelopmentEnvironment ); } // // Using XWebinarSubscriber Repository Descriptor ... if (!webinarSubscriberDescriptor.IsNull()) { // app.UseXRepository( descriptor: webinarSubscriberDescriptor, isDevelopmentEnvironment: isDevelopmentEnvironment ); } } // #region Private ... private static XRepositoryDescriptor webinarDescriptor = null; private static XRepositoryDescriptor webinarSubscriberDescriptor = null; /// /// a LogTag for Service ... /// private static string XLogTag = XWebinarServiceConstants.XWebinarServiceDILogTag; /// /// print a log in Console ... /// /// private static void Log(string message) { Console.WriteLine($"{XLogTag} => {message}"); } /// /// Register Service ... /// /// /// /// /// private static void AddWebinarService( 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: // webinarDescriptor = typeof(XWebinarServiceHelper) .InvokeGenericMethod( args: null, runtimeType: contextType, methodName: XWebinarServiceConstants.GetXWebinarEFRepositoryDescriptor ); // webinarSubscriberDescriptor = typeof(XWebinarServiceHelper) .InvokeGenericMethod( args: null, runtimeType: contextType, methodName: XWebinarServiceConstants.GetXWebinarSubscriberEFRepositoryDescriptor ); break; // case XRepositoryType.Mongo: webinarDescriptor = XWebinarServiceHelper.GetXWebinarMongoRepositoryDescriptor(); webinarSubscriberDescriptor = XWebinarServiceHelper.GetXWebinarSubscriberMongoRepositoryDescriptor(); break; // case XRepositoryType.InMemory: webinarDescriptor = XWebinarServiceHelper.GetXWebinarInMemoryRepositoryDescriptor(); webinarSubscriberDescriptor = XWebinarServiceHelper.GetXWebinarSubscriberInMemoryRepositoryDescriptor(); break; } // // Register xWebinar Repository Descriptor ... services.AddXRepository( lifeTime: lifeTime, descriptor: webinarDescriptor ); services.AddXRepository( lifeTime: lifeTime, descriptor: webinarSubscriberDescriptor ); // // Register Dto Services ... services.Add( new ServiceDescriptor(typeof(IXWebinarServiceProvider), typeof(XWebinarServiceProvider), lifeTime) ); services.Add( new ServiceDescriptor(typeof(IXWebinarRepositoryService), typeof(XWebinarRepositoryService), lifeTime) ); services.Add( new ServiceDescriptor(typeof(IXWebinarRepositoryProvider), typeof(XWebinarRepositoryProvider), lifeTime) ); services.Add( new ServiceDescriptor(typeof(IXWebinarSubscriberServiceProvider), typeof(XWebinarSubscriberServiceProvider), lifeTime) ); services.Add( new ServiceDescriptor(typeof(IXWebinarSubscriberRepositoryService), typeof(XWebinarSubscriberRepositoryService), lifeTime) ); services.Add( new ServiceDescriptor(typeof(IXWebinarSubscriberRepositoryProvider), typeof(XWebinarSubscriberRepositoryProvider), lifeTime) ); // // services.Add( // new ServiceDescriptor(typeof(IXWebinarProvider), typeof(XWebinarProvider), lifeTime) // ); // Log("Service Regitration Succeed ..."); } #endregion } }