using System; using xPushService.DI; using xDataService.DI; using xDataService.Db; using xCommons.Helpers; using xDataService.Models; using xCommons.Extensions; using xPushService.Helpers; using xExceptions.Constants; using xDataService.Constants; using xContentService.Helpers; using xContentService.Constants; using xContentService.Providers; using xContentService.Interfaces; using Microsoft.AspNetCore.Builder; using xContentService.Providers.Dtos; using xContentService.Interfaces.Dtos; using xContentService.Providers.Entities; using xContentService.Interfaces.Entities; using xContentService.Providers.Enrichers; using xContentService.Interfaces.Enrichers; using Microsoft.Extensions.DependencyInjection; namespace xContentService.DI { /// /// Service Registration Extensions Classes ... /// public static partial class XDIHelperExtensions { /// /// Register Service ... /// /// /// /// public static void AddXContentService( this IServiceCollection services, XRepositoryType repositoryType, ServiceLifetime lifeTime = ServiceLifetime.Scoped ) { AddContentService( contextType: null, services: services, lifeTime: lifeTime, repositoryType: repositoryType ); } /// /// Register Service ... /// /// /// /// /// public static void AddXContentService( this IServiceCollection services, XRepositoryType repositoryType, ServiceLifetime lifeTime = ServiceLifetime.Scoped ) where TContext : XDbContext { AddContentService( services: services, lifeTime: lifeTime, contextType: typeof(TContext), repositoryType: repositoryType ); } /// /// Use xContentService Middleware ... /// /// /// public static void UseXContentService( this IApplicationBuilder app, bool isDevelopmentEnvironment = false ) { // #region Using XContent Hubs ... // var pushHelper = new XPushServiceHelper(); // pushHelper.AddHub(XContentServiceConstants.XContentDtoHub); pushHelper.AddHub(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; /// /// a LogTag for Service ... /// private static string XLogTag = XContentServiceConstants.XContentServiceDILogTag; /// /// print a log in Console ... /// /// private static void Log(string message) { Console.WriteLine($"{XLogTag} => {message}"); } /// /// Register Service ... /// /// /// /// /// 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( 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 ); // services.Add( new ServiceDescriptor(typeof(IXContentTitleResourceProvider), typeof(XContentTitleResourceProvider), lifeTime) ); services.Add( new ServiceDescriptor(typeof(IXContentDescriptionResourceProvider), typeof(XContentDescriptionResourceProvider), lifeTime) ); // services.Add( new ServiceDescriptor(typeof(IXContentEnricher), typeof(XContentEnricher), lifeTime) ); // // 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 } }