From 30b3e80a8d397c60494cacb2e561c74117cbbab5 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Sun, 24 May 2026 22:46:25 +0330 Subject: [PATCH] Prepare xStringService except Controllers ... --- {Base => Constants}/.gitkeep | 0 Constants/XStringHubAction.cs | 5 - Constants/xStringServiceConstants.cs | 3 - DI/XDIHelperExtension.cs | 159 ++++++++-- Extensions/xStringServiceExtensions.cs | 3 - Helpers/XStringServiceHelper.cs | 281 ++++++++++++++++++ Interfaces/IXStringSeeder.cs | 8 + Providers/Entities/XStringEFRepository.cs | 4 +- ...sitory.cs => XStringInMemoryRepository.cs} | 4 +- 9 files changed, 433 insertions(+), 34 deletions(-) rename {Base => Constants}/.gitkeep (100%) delete mode 100644 Constants/XStringHubAction.cs delete mode 100644 Constants/xStringServiceConstants.cs delete mode 100644 Extensions/xStringServiceExtensions.cs create mode 100644 Helpers/XStringServiceHelper.cs create mode 100644 Interfaces/IXStringSeeder.cs rename Providers/Entities/{XStringInMemoeryRepository.cs => XStringInMemoryRepository.cs} (77%) diff --git a/Base/.gitkeep b/Constants/.gitkeep similarity index 100% rename from Base/.gitkeep rename to Constants/.gitkeep diff --git a/Constants/XStringHubAction.cs b/Constants/XStringHubAction.cs deleted file mode 100644 index 793f7ce..0000000 --- a/Constants/XStringHubAction.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace xStringService.Constants -{ - public enum XStringHubAction - { } -} \ No newline at end of file diff --git a/Constants/xStringServiceConstants.cs b/Constants/xStringServiceConstants.cs deleted file mode 100644 index 80b04f1..0000000 --- a/Constants/xStringServiceConstants.cs +++ /dev/null @@ -1,3 +0,0 @@ -namespace xStringService.Constants { - public class xStringServiceConstants { } -} \ No newline at end of file diff --git a/DI/XDIHelperExtension.cs b/DI/XDIHelperExtension.cs index f0f5f30..e454303 100644 --- a/DI/XDIHelperExtension.cs +++ b/DI/XDIHelperExtension.cs @@ -2,7 +2,19 @@ using System; using Microsoft.Extensions.DependencyInjection; using xExceptions.Constants; using xCommons.Extensions; +using xDataService.Constants; +using xDataService.Db; +using xDataService.DI; +using xDataService.Models; +using xStringService.Helpers; +using xCommons.Helpers; +using Microsoft.AspNetCore.Builder; +using xStringService.Interfaces.Dtos; +using xStringService.Providers.Dtos; using xStringService.Interfaces.Entities; +using xStringService.Providers.Entities; +using xPushService.Helpers; +using xPushService.DI; namespace xStringService.DI { @@ -12,20 +24,83 @@ namespace xStringService.DI /// Register Service ... /// /// - /// + /// + /// public static void AddXStringService( this IServiceCollection services, - ServiceLifetime lifeTime + XRepositoryType repositoryType, + ServiceLifetime lifeTime = ServiceLifetime.Scoped ) { AddStringService( - services, - lifeTime + contextType: null, + services: services, + lifeTime: lifeTime, + repositoryType: repositoryType ); } + /// + /// Register Service ... + /// + /// + /// + /// + /// + public static void AddXStringService( + this IServiceCollection services, + XRepositoryType repositoryType, + ServiceLifetime lifeTime = ServiceLifetime.Scoped + ) + where TContext : XDbContext + { + AddStringService( + services: services, + lifeTime: lifeTime, + contextType: typeof(TContext), + repositoryType: repositoryType + ); + } + + /// + /// Use XStringService Middleware ... + /// + /// + /// + public static void UseXStringService( + this IApplicationBuilder app, + bool isDevelopmentEnvironment = false + ) + { + // + #region Using XString Hubs ... + // + var pushHelper = new XPushServiceHelper(); + + // + pushHelper.AddHub("stringDto"); + pushHelper.AddHub("stringEntity"); + + // + app.UseXPushService(pushHelper); + #endregion + + // + // Using XString Repository Descriptor ... + if (!descriptor.IsNull()) + { + // + app.UseXRepository( + descriptor: descriptor, + isDevelopmentEnvironment: isDevelopmentEnvironment + ); + } + } + // #region Private ... + private static XRepositoryDescriptor descriptor = null; + /// /// a LogTag for Service ... /// @@ -44,10 +119,14 @@ namespace xStringService.DI /// Register Service ... /// /// + /// + /// /// private static void AddStringService( IServiceCollection services, - ServiceLifetime lifeTime + XRepositoryType repositoryType, + Type contextType = null, + ServiceLifetime lifeTime = ServiceLifetime.Scoped ) { // @@ -59,7 +138,7 @@ namespace xStringService.DI if (!isServicesExists) { // - Log("AddStringService failed, services not provided ..."); + Log("Service Registration failed, services not provided ..."); throw exception; } @@ -69,28 +148,70 @@ namespace xStringService.DI if (!isLifetimeExists) { // - Log("AddStringService failed, lifetime not provided ..."); + Log("Service Registration failed, lifetime not provided ..."); throw exception; } // - // Check Repository Exists ... - var isRepositoryExists = !services - .GetRegisteredService() - .IsNull(); - if (!isRepositoryExists) + // Validate Repository Type ... + var isRepositoryTypeValid = + (repositoryType == XRepositoryType.EF && + !contextType.IsNull()) || + ((repositoryType == XRepositoryType.Mongo || + repositoryType == XRepositoryType.InMemory) && + contextType.IsNull()); + if (!isRepositoryTypeValid) { // - Log("AddStringService failed, IXStringRepository not provided ..."); - throw exception; + Log("Service Registration failed, Invalid Repository Type and Context Type ..."); + throw exception; } - // // - // // Main Provider ... - // services.Add(new ServiceDescriptor(typeof(IXStringProvider), typeof(XStringProvider), lifeTime)); - // - Log("AddStringService Succeed ..."); + switch(repositoryType) + { + // + case XRepositoryType.EF: + // + descriptor = typeof(XStringServiceHelper) + .InvokeGenericMethod( + methodName: "GetXStringEfRepositoryDescriptor", + runtimeType: contextType, + args: null + ); + break; + + // + case XRepositoryType.Mongo: + descriptor = XStringServiceHelper.GetXStringMongoRepositoryDescriptor(); + break; + + // + case XRepositoryType.InMemory: + descriptor = XStringServiceHelper.GetXStringInMemoryRepositoryDescriptor(); + break; + } + + // + services.AddXRepository( + lifeTime: lifeTime, + descriptor: descriptor + ); + + // + // Register Dto Services ... + services.Add( + new ServiceDescriptor(typeof(IXStringServiceProvider), typeof(XStringServiceProvider), lifeTime) + ); + services.Add( + new ServiceDescriptor(typeof(IXStringRepositoryService), typeof(XStringRepositoryService), lifeTime) + ); + services.Add( + new ServiceDescriptor(typeof(IXStringRepositoryProvider), typeof(XStringRepositoryProvider), lifeTime) + ); + + // + Log("Service Regitration Succeed ..."); } #endregion } diff --git a/Extensions/xStringServiceExtensions.cs b/Extensions/xStringServiceExtensions.cs deleted file mode 100644 index 8b3bc09..0000000 --- a/Extensions/xStringServiceExtensions.cs +++ /dev/null @@ -1,3 +0,0 @@ -namespace xStringService.Extensions { - public static class xStringServiceExtensions { } -} \ No newline at end of file diff --git a/Helpers/XStringServiceHelper.cs b/Helpers/XStringServiceHelper.cs new file mode 100644 index 0000000..1614812 --- /dev/null +++ b/Helpers/XStringServiceHelper.cs @@ -0,0 +1,281 @@ +using GraphQL.Types; +using MongoDB.Bson.Serialization.Serializers; +using xDataHelper.Providers.GraphQL; +using xDataService.Db; +using xDataService.Models; +using xDataService.Providers; +using xStringService.Interfaces; +using xStringService.Interfaces.Entities; +using xStringService.Models.Entities; +using xStringService.Providers.Entities; +using xStringService.Providers.GraphQL; + +namespace xStringService.Helpers +{ + public static class XStringServiceHelper + { + // + #region EF Repository Descriptor Provider(s) ... + public static XEFRepositoryDescriptor< + XString, + int, + IXStringKeyGenerator, + XStringKeyGenerator, + IXStringRepositoryEvents, + XStringRepositoryEvents, + XStringGraphType, + IntGraphType, + XStringGraphQuery, + IXStringGraphQLTypeHelper, + XStringGraphQLTypeHelper, + XStringGraphSchema, + IXStringRepository, + XStringEFRepository, + TContext + > GetXStringEfRepositoryDescriptor() + where TContext : XDbContext + { + // + var result = new XEFRepositoryDescriptor< + XString, + int, + IXStringKeyGenerator, + XStringKeyGenerator, + IXStringRepositoryEvents, + XStringRepositoryEvents, + XStringGraphType, + IntGraphType, + XStringGraphQuery, + IXStringGraphQLTypeHelper, + XStringGraphQLTypeHelper, + XStringGraphSchema, + IXStringRepository, + XStringEFRepository, + TContext + >(); + + // + return result; + } + + public static XEFRepositoryDescriptor< + XString, + int, + IXStringKeyGenerator, + XStringKeyGenerator, + IXStringRepositoryEvents, + XStringRepositoryEvents, + XStringGraphType, + IntGraphType, + XStringGraphQuery, + IXStringGraphQLTypeHelper, + XStringGraphQLTypeHelper, + XStringGraphSchema, + IXStringRepository, + XStringEFRepository, + TContext, + IXStringSeeder, + TSeederImplementation + > GetXStringEfRepositoryDescriptor() + where TContext : XDbContext + where TSeederImplementation : XBaseDbSeeder + { + // + var result = new XEFRepositoryDescriptor< + XString, + int, + IXStringKeyGenerator, + XStringKeyGenerator, + IXStringRepositoryEvents, + XStringRepositoryEvents, + XStringGraphType, + IntGraphType, + XStringGraphQuery, + IXStringGraphQLTypeHelper, + XStringGraphQLTypeHelper, + XStringGraphSchema, + IXStringRepository, + XStringEFRepository, + TContext, + IXStringSeeder, + TSeederImplementation + >(); + + // + return result; + } + #endregion + + // + #region Mongo Repository Descriptor Provider(s) ... + public static XMongoRepositoryDescriptor< + XString, + int, + IXStringKeyGenerator, + XStringKeyGenerator, + IXStringRepositoryEvents, + XStringRepositoryEvents, + XStringGraphType, + IntGraphType, + XStringGraphQuery, + IXStringGraphQLTypeHelper, + XStringGraphQLTypeHelper, + XStringGraphSchema, + IXStringRepository, + XStringMongoRepository + > GetXStringMongoRepositoryDescriptor() + { + // + var result = new XMongoRepositoryDescriptor< + XString, + int, + IXStringKeyGenerator, + XStringKeyGenerator, + IXStringRepositoryEvents, + XStringRepositoryEvents, + XStringGraphType, + IntGraphType, + XStringGraphQuery, + IXStringGraphQLTypeHelper, + XStringGraphQLTypeHelper, + XStringGraphSchema, + IXStringRepository, + XStringMongoRepository + >(); + + // + return result; + } + + public static XMongoRepositoryDescriptor< + XString, + int, + IXStringKeyGenerator, + XStringKeyGenerator, + IXStringRepositoryEvents, + XStringRepositoryEvents, + XStringGraphType, + IntGraphType, + XStringGraphQuery, + IXStringGraphQLTypeHelper, + XStringGraphQLTypeHelper, + XStringGraphSchema, + IXStringRepository, + XStringMongoRepository, + IXStringSeeder, + TSeederImplementation + > GetXStringMongoRepositoryDescriptor() + where TSeederImplementation : XBaseDbSeeder + { + // + var result = new XMongoRepositoryDescriptor< + XString, + int, + IXStringKeyGenerator, + XStringKeyGenerator, + IXStringRepositoryEvents, + XStringRepositoryEvents, + XStringGraphType, + IntGraphType, + XStringGraphQuery, + IXStringGraphQLTypeHelper, + XStringGraphQLTypeHelper, + XStringGraphSchema, + IXStringRepository, + XStringMongoRepository, + IXStringSeeder, + TSeederImplementation + >(); + + // + return result; + } + #endregion + + // + #region InMemory Repository Descriptor Provider(s) ... + public static XInMemoryRepositoryDescriptor< + XString, + int, + IXStringKeyGenerator, + XStringKeyGenerator, + IXStringRepositoryEvents, + XStringRepositoryEvents, + XStringGraphType, + IntGraphType, + XStringGraphQuery, + IXStringGraphQLTypeHelper, + XStringGraphQLTypeHelper, + XStringGraphSchema, + IXStringRepository, + XStringInMemoryRepository + > GetXStringInMemoryRepositoryDescriptor() + { + // + var result = new XInMemoryRepositoryDescriptor< + XString, + int, + IXStringKeyGenerator, + XStringKeyGenerator, + IXStringRepositoryEvents, + XStringRepositoryEvents, + XStringGraphType, + IntGraphType, + XStringGraphQuery, + IXStringGraphQLTypeHelper, + XStringGraphQLTypeHelper, + XStringGraphSchema, + IXStringRepository, + XStringInMemoryRepository + >(); + + // + return result; + } + + public static XInMemoryRepositoryDescriptor< + XString, + int, + IXStringKeyGenerator, + XStringKeyGenerator, + IXStringRepositoryEvents, + XStringRepositoryEvents, + XStringGraphType, + IntGraphType, + XStringGraphQuery, + IXStringGraphQLTypeHelper, + XStringGraphQLTypeHelper, + XStringGraphSchema, + IXStringRepository, + XStringInMemoryRepository, + IXStringSeeder, + TSeederImplementation + > GetXStringInMemoryRepositoryDescriptor() + where TSeederImplementation : XBaseDbSeeder + { + // + var result = new XInMemoryRepositoryDescriptor< + XString, + int, + IXStringKeyGenerator, + XStringKeyGenerator, + IXStringRepositoryEvents, + XStringRepositoryEvents, + XStringGraphType, + IntGraphType, + XStringGraphQuery, + IXStringGraphQLTypeHelper, + XStringGraphQLTypeHelper, + XStringGraphSchema, + IXStringRepository, + XStringInMemoryRepository, + IXStringSeeder, + TSeederImplementation + >(); + + // + return result; + } + #endregion + } +} \ No newline at end of file diff --git a/Interfaces/IXStringSeeder.cs b/Interfaces/IXStringSeeder.cs new file mode 100644 index 0000000..56c5915 --- /dev/null +++ b/Interfaces/IXStringSeeder.cs @@ -0,0 +1,8 @@ +using xDataService.Interfaces; +using xStringService.Models.Entities; + +namespace xStringService.Interfaces +{ + public interface IXStringSeeder : IXBaseDbSeeder + {} +} \ No newline at end of file diff --git a/Providers/Entities/XStringEFRepository.cs b/Providers/Entities/XStringEFRepository.cs index 57b6f56..6e177ea 100644 --- a/Providers/Entities/XStringEFRepository.cs +++ b/Providers/Entities/XStringEFRepository.cs @@ -7,10 +7,10 @@ using xStringService.Models.Entities; namespace xStringService.Providers.Entities { - public class XStringEFRepository : XBaseEFRepository + public class XStringEFRepository : XBaseEFRepository, IXStringRepository where TContext : XDbContext { - protected XStringEFRepository( + public XStringEFRepository( IXUnitOfWorks unitOfWorks, XDataServiceConfiguration configuration, IXStringKeyGenerator keyGenerator = null, diff --git a/Providers/Entities/XStringInMemoeryRepository.cs b/Providers/Entities/XStringInMemoryRepository.cs similarity index 77% rename from Providers/Entities/XStringInMemoeryRepository.cs rename to Providers/Entities/XStringInMemoryRepository.cs index cda6a7b..96bc57b 100644 --- a/Providers/Entities/XStringInMemoeryRepository.cs +++ b/Providers/Entities/XStringInMemoryRepository.cs @@ -5,9 +5,9 @@ using xStringService.Models.Entities; namespace xStringService.Providers.Entities { - public class XStringInMemoeryRepository : XBaseInMemoryRepository, IXStringRepository + public class XStringInMemoryRepository : XBaseInMemoryRepository, IXStringRepository { - public XStringInMemoeryRepository( + public XStringInMemoryRepository( XDataServiceConfiguration configuration, IXStringKeyGenerator keyGenerator = null, IXStringRepositoryEvents baseRepositoryEvents = null