From 3d274c347f6e1e441fde20096f727f044cb4a117 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Sun, 24 May 2026 22:47:07 +0330 Subject: [PATCH] Add Some new Features for Enabling Repository Registration and Middlewares Usages separately ... --- Constants/XRepositoryType.cs | 16 +++ DI/XDIHelperExtension.cs | 253 +++++++++++++++++++---------------- 2 files changed, 153 insertions(+), 116 deletions(-) create mode 100644 Constants/XRepositoryType.cs diff --git a/Constants/XRepositoryType.cs b/Constants/XRepositoryType.cs new file mode 100644 index 0000000..215ba72 --- /dev/null +++ b/Constants/XRepositoryType.cs @@ -0,0 +1,16 @@ +using xExceptions.Attributes; + +namespace xDataService.Constants +{ + public enum XRepositoryType + { + [StringValue("EF")] + EF, + + [StringValue("Mongo")] + Mongo, + + [StringValue("InMemory")] + InMemory + } +} \ No newline at end of file diff --git a/DI/XDIHelperExtension.cs b/DI/XDIHelperExtension.cs index 971dc35..2d9e142 100644 --- a/DI/XDIHelperExtension.cs +++ b/DI/XDIHelperExtension.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using GraphQL.Server.Ui.Playground; using Microsoft.AspNetCore.Builder; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.Extensions.Configuration; @@ -122,12 +121,12 @@ namespace xDataService.DI /// /// /// - /// + /// public static void AddXDatabase( this IServiceCollection source, IConfiguration configuration, XDatabaseDescriptor descriptor, - ServiceLifetime lifetime = ServiceLifetime.Scoped + ServiceLifetime lifeTime = ServiceLifetime.Scoped ) { // @@ -210,7 +209,7 @@ namespace xDataService.DI args: [ source, descriptor, - lifetime + lifeTime ] ); @@ -232,34 +231,47 @@ namespace xDataService.DI descriptor.Repositories.ToList().ForEach(r => { // - // Preparing Runtime Types ... - Type[] runtimeTypes = r.GetType().GenericTypeArguments; - - // - try - { - // - typeof(XDataServiceHelper).InvokeGenericMethod( - methodName: "AddXRepository", - runtimeTypes: runtimeTypes, - args: [ - source, - r, - lifetime - ] - ); - - // - Log($"Repository of: {runtimeTypes[0]}, Registered Successfully ..."); - } - catch (Exception ex) - { - Log($"Repository of: {runtimeTypes[0]}, Registration failed due {ex.Message} ..."); - } + source.AddXRepository( + descriptor: r, + lifeTime: lifeTime + ); }); } } + public static void AddXRepository( + this IServiceCollection source, + XRepositoryDescriptor descriptor, + ServiceLifetime lifeTime = ServiceLifetime.Scoped + ) + { + // + // Preparing Runtime Types ... + Type[] runtimeTypes = descriptor.GetType().GenericTypeArguments; + + // + try + { + // + typeof(XDataServiceHelper).InvokeGenericMethod( + methodName: "AddXRepository", + runtimeTypes: runtimeTypes, + args: [ + source, + descriptor, + lifeTime + ] + ); + + // + Log($"Repository of: {runtimeTypes[0]}, Registered Successfully ..."); + } + catch (Exception ex) + { + Log($"Repository of: {runtimeTypes[0]}, Registration failed due {ex.Message} ..."); + } + } + public static void UseXDatabase( this IApplicationBuilder source, XDatabaseDescriptor descriptor, @@ -278,113 +290,122 @@ namespace xDataService.DI XException.InvalidArgs.Throw(); } + // + // Check Repositories Exists ... + isValid = descriptor.HasRepositories(); + if (isValid) + { + // + // Loop through Repositories ... + descriptor.Repositories + .ToList() + .ForEach(r => + { + // + source.UseXRepository( + descriptor: r, + isDevelopmentEnvironment: isDevelopmentEnvironment + ); + }); + } + } + + /// + /// Use a Repository Middlewares by Providing it's Descriptor ... + /// + /// + /// + /// + public static void UseXRepository( + this IApplicationBuilder source, + XRepositoryDescriptor descriptor, + bool isDevelopmentEnvironment = false + ) + { + // + Log($"Start Using Repository of: {descriptor.Entity}"); + // var withPlayground = false; - var isGraphConfigured = false; if (isDevelopmentEnvironment) { withPlayground = true; } // - // Check Repositories Exists ... - isValid = descriptor.HasRepositories(); - if (isValid) + // Create an Scope for Services Acces ... + using (var scope = source.ApplicationServices.CreateScope()) { // - // Create an Scope for Services Acces ... - using (var scope = source.ApplicationServices.CreateScope()) + // Inject XDataServiceConfiguration ForMake Sure DataService Registration ... + var dataServiceConfiguration = scope.ServiceProvider.GetService(); + var isValid = !dataServiceConfiguration.IsNullOrDefault(); + if (isValid) { // - // Inject XDataServiceConfiguration ForMake Sure DataService Registration ... - var dataServiceConfiguration = scope.ServiceProvider.GetService(); - isValid = !dataServiceConfiguration.IsNullOrDefault(); + // Check Seeder Exists ... + // Execute Seeding Data Task if Exists ... + isValid = descriptor.HasSeeder(); if (isValid) { // - // Loop through Repositories ... - descriptor.Repositories - .ToList() - .ForEach(r => - { - // - Log($"Start Using Repository of: {r.Entity}"); + var seeder = (dynamic)scope.ServiceProvider.GetService(descriptor.SeederService); + isValid = seeder != null; + if (isValid) + { + // + Log($"Start Seeding {descriptor.Entity} ..."); - // - // Check Seeder Exists ... - // Execute Seeding Data Task if Exists ... - isValid = r.HasSeeder(); - if (isValid) - { - // - var seeder = (dynamic)scope.ServiceProvider.GetService(r.SeederService); - isValid = seeder != null; - if (isValid) - { - // - Log($"Start Seeding {r.Entity} ..."); + // + // Seeding ... + seeder + .Seed() + .GetAwaiter() + .GetResult(); + } + } - // - // Seeding ... - seeder - .Seed() - .GetAwaiter() - .GetResult(); - } - } + // + // Check GraphQL Support ... + isValid = descriptor.HasGraphSupport(); + if (isValid) + { + // + // Use WebSockets ... + source.UseWebSockets(); - // - // Check GraphQL Support ... - isValid = r.HasGraphSupport(); - if (isValid) - { - // - // Use WebSockets ... - if (!isGraphConfigured) - { - source.UseWebSockets(); - } + // + // Inject GraphQL Type Helper ... + var helper = (dynamic)scope.ServiceProvider.GetService(descriptor.GraphTypeHelperService); + isValid = helper != null; + if (isValid) + { + // + var graphPath = $"{helper.GetGraphQLPath()}"; + var graphBasePath = $"{dataServiceConfiguration.GraphQLBasePath}"; + typeof(XDataServiceHelper).InvokeGenericMethod( + methodName: "UseXGraphQL", + runtimeTypes: [ + descriptor.Entity, + descriptor.Key, + descriptor.GraphType, + descriptor.GraphTypeKey, + descriptor.GraphQuery, + descriptor.GraphTypeHelperService, + descriptor.GraphTypeHelperImplementation, + descriptor.GraphSchema + ], + args: [ + source, + graphBasePath, + graphPath, + withPlayground + ] + ); - // - // Inject GraphQL Type Helper ... - var helper = (dynamic)scope.ServiceProvider.GetService(r.GraphTypeHelperService); - isValid = helper != null; - if (isValid) - { - // - var graphPath = $"{helper.GetGraphQLPath()}"; - var graphBasePath = $"{dataServiceConfiguration.GraphQLBasePath}"; - typeof(XDataServiceHelper).InvokeGenericMethod( - methodName: "UseXGraphQL", - runtimeTypes: [ - r.Entity, - r.Key, - r.GraphType, - r.GraphTypeKey, - r.GraphQuery, - r.GraphTypeHelperService, - r.GraphTypeHelperImplementation, - r.GraphSchema - ], - args: [ - source, - graphBasePath, - graphPath, - withPlayground - ] - ); - - // - Log($"Register GraphQL: {graphPath}"); - } - - // - if (!isGraphConfigured) - { - isGraphConfigured = true; - } - } - }); + // + Log($"Register GraphQL: {graphPath}"); + } } } }