using System; using System.Linq; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using xCategoryService.Configurations; using xCategoryService.Constants; using xCategoryService.Helpers; using xCategoryService.Interfaces; using xCategoryService.Interfaces.Dtos; using xCategoryService.Interfaces.Entities; using xCategoryService.Providers; using xCategoryService.Providers.Dtos; using xCategoryService.Providers.Entities; 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; namespace xCategoryService.DI { public static class XDIHelperExtension { /// /// Extract Service Configurations ... /// /// /// public static XCategoryServiceConfiguration GetXCategoryServiceConfigurations( this IConfiguration source ) { // XCategoryServiceConfiguration result = null; // var section = source.GetSection(Constants.ConfigurationNodeNames.CATEGORY_SERVICE_NODE); var sectionJson = source.GetSectionAsJson(Constants.ConfigurationNodeNames.CATEGORY_SERVICE_NODE); if (!section.IsNull()) { result = sectionJson.FromJSON(); } // return result; } /// /// Register Service Configuration ... /// /// /// public static void AddXCategoryServiceConfiguration( this IServiceCollection source, IConfiguration configuration ) { // // Extract Configuration Class ... var config = configuration.GetXCategoryServiceConfigurations(); // // Register Class as Service ... source.AddXCategoryServiceConfiguration(config); } /// /// Register Service Configuration ... /// /// /// public static void AddXCategoryServiceConfiguration( this IServiceCollection source, XCategoryServiceConfiguration configuration ) { // // Normalize Configuration Class ... if (configuration.IsNull()) { configuration = new XCategoryServiceConfiguration(); } // // Register ... source.AddSingleton(configuration); } /// /// Register Service ... /// /// /// /// public static void AddXCategoryService( this IServiceCollection services, XRepositoryType repositoryType, ServiceLifetime lifeTime = ServiceLifetime.Scoped ) { AddCategoryService( contextType: null, services: services, lifeTime: lifeTime, repositoryType: repositoryType ); } /// /// Register Service ... /// /// /// /// /// public static void AddXCategoryService( this IServiceCollection services, XRepositoryType repositoryType, ServiceLifetime lifeTime = ServiceLifetime.Scoped ) where TContext : XDbContext { AddCategoryService( services: services, lifeTime: lifeTime, contextType: typeof(TContext), repositoryType: repositoryType ); } /// /// Use XSubCategoryService Middleware ... /// /// /// public static void UseXCategoryService( this IApplicationBuilder app, bool isDevelopmentEnvironment = false ) { // #region Using Hubs ... // var pushHelper = new XPushServiceHelper(); // // Category ... pushHelper.AddHub(XCategoryServiceConstants.XCategoryDtoHub); pushHelper.AddHub(XCategoryServiceConstants.XCategoryEntityHub); // // SubCategory ... pushHelper.AddHub(XCategoryServiceConstants.XSubCategoryDtoHub); pushHelper.AddHub(XCategoryServiceConstants.XSubCategoryEntityHub); // app.UseXPushService(pushHelper); #endregion // // Using XCategory 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(); var subCategoryProvider = scope.ServiceProvider.GetService(); var configuration = scope.ServiceProvider.GetService(); 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; /// /// a LogTag for Service ... /// private static string XLogTag = XCategoryServiceConstants.XCategoryServiceDILogTag; /// /// print a log in Console ... /// /// private static void Log(string message) { Console.WriteLine($"{XLogTag} => {message}"); } /// /// Register Service ... /// /// /// /// /// 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(XCategoryServiceHelper) .InvokeGenericMethod( args: null, runtimeType: contextType, methodName: XCategoryServiceConstants.GetXCategoryEFRepositoryDescriptor ); // // SubCategory ... subCategoryDescriptor = typeof(XCategoryServiceHelper) .InvokeGenericMethod( args: null, runtimeType: contextType, methodName: XCategoryServiceConstants.GetXSubCategoryEFRepositoryDescriptor ); break; // case XRepositoryType.Mongo: // // Category ... categoryDescriptor = XCategoryServiceHelper.GetXCategoryMongoRepositoryDescriptor(); // // SubCategory ... subCategoryDescriptor = XCategoryServiceHelper.GetXSubCategoryMongoRepositoryDescriptor(); break; // case XRepositoryType.InMemory: // // Category ... categoryDescriptor = XCategoryServiceHelper.GetXCategoryInMemoryRepositoryDescriptor(); // // SubCategory ... subCategoryDescriptor = XCategoryServiceHelper.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(IXCategoryServiceProvider), typeof(XCategoryServiceProvider), lifeTime) ); services.Add( new ServiceDescriptor(typeof(IXCategoryRepositoryService), typeof(XCategoryRepositoryService), lifeTime) ); services.Add( new ServiceDescriptor(typeof(IXCategoryRepositoryProvider), typeof(XCategoryRepositoryProvider), lifeTime) ); // // Resource Providers ... services.Add( new ServiceDescriptor(typeof(IXCategoryTitleResourceProvider), typeof(XCategoryTitleResourceProvider), lifeTime) ); services.Add( new ServiceDescriptor(typeof(IXCategoryDescriptionResourceProvider), typeof(XCategoryDescriptionResourceProvider), lifeTime) ); // // Reference Providers ... services.Add( new ServiceDescriptor(typeof(IXCategorySubCategoryReferenceProvider), typeof(XCategorySubCategoryReferenceProvider), lifeTime) ); // // Provider ... services.Add( new ServiceDescriptor(typeof(IXCategoryProvider), typeof(XCategoryProvider), 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 // // Register Service Main Provider ... services.Add( new ServiceDescriptor(typeof(IXCategoryService), typeof(XCategoryService), lifeTime) ); // Log("Service Regitration Succeed ..."); } #endregion } }