Add Some new Features for Enabling Repository Registration and Middlewares Usages separately ...

This commit is contained in:
2026-05-24 22:47:07 +03:30
parent e8f2f94f6e
commit 3d274c347f
2 changed files with 153 additions and 116 deletions
+16
View File
@@ -0,0 +1,16 @@
using xExceptions.Attributes;
namespace xDataService.Constants
{
public enum XRepositoryType
{
[StringValue("EF")]
EF,
[StringValue("Mongo")]
Mongo,
[StringValue("InMemory")]
InMemory
}
}
+75 -54
View File
@@ -1,7 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using GraphQL.Server.Ui.Playground;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
@@ -122,12 +121,12 @@ namespace xDataService.DI
/// <param name="source"></param> /// <param name="source"></param>
/// <param name="configuration"></param> /// <param name="configuration"></param>
/// <param name="descriptor"></param> /// <param name="descriptor"></param>
/// <param name="lifetime"></param> /// <param name="lifeTime"></param>
public static void AddXDatabase( public static void AddXDatabase(
this IServiceCollection source, this IServiceCollection source,
IConfiguration configuration, IConfiguration configuration,
XDatabaseDescriptor descriptor, XDatabaseDescriptor descriptor,
ServiceLifetime lifetime = ServiceLifetime.Scoped ServiceLifetime lifeTime = ServiceLifetime.Scoped
) )
{ {
// //
@@ -210,7 +209,7 @@ namespace xDataService.DI
args: [ args: [
source, source,
descriptor, descriptor,
lifetime lifeTime
] ]
); );
@@ -230,10 +229,25 @@ namespace xDataService.DI
{ {
// //
descriptor.Repositories.ToList().ForEach(r => descriptor.Repositories.ToList().ForEach(r =>
{
//
source.AddXRepository(
descriptor: r,
lifeTime: lifeTime
);
});
}
}
public static void AddXRepository(
this IServiceCollection source,
XRepositoryDescriptor descriptor,
ServiceLifetime lifeTime = ServiceLifetime.Scoped
)
{ {
// //
// Preparing Runtime Types ... // Preparing Runtime Types ...
Type[] runtimeTypes = r.GetType().GenericTypeArguments; Type[] runtimeTypes = descriptor.GetType().GenericTypeArguments;
// //
try try
@@ -244,8 +258,8 @@ namespace xDataService.DI
runtimeTypes: runtimeTypes, runtimeTypes: runtimeTypes,
args: [ args: [
source, source,
r, descriptor,
lifetime lifeTime
] ]
); );
@@ -256,8 +270,6 @@ namespace xDataService.DI
{ {
Log($"Repository of: {runtimeTypes[0]}, Registration failed due {ex.Message} ..."); Log($"Repository of: {runtimeTypes[0]}, Registration failed due {ex.Message} ...");
} }
});
}
} }
public static void UseXDatabase( public static void UseXDatabase(
@@ -278,28 +290,10 @@ namespace xDataService.DI
XException.InvalidArgs.Throw(); XException.InvalidArgs.Throw();
} }
//
var withPlayground = false;
var isGraphConfigured = false;
if (isDevelopmentEnvironment)
{
withPlayground = true;
}
// //
// Check Repositories Exists ... // Check Repositories Exists ...
isValid = descriptor.HasRepositories(); isValid = descriptor.HasRepositories();
if (isValid) if (isValid)
{
//
// Create an Scope for Services Acces ...
using (var scope = source.ApplicationServices.CreateScope())
{
//
// Inject XDataServiceConfiguration ForMake Sure DataService Registration ...
var dataServiceConfiguration = scope.ServiceProvider.GetService<XDataServiceConfiguration>();
isValid = !dataServiceConfiguration.IsNullOrDefault();
if (isValid)
{ {
// //
// Loop through Repositories ... // Loop through Repositories ...
@@ -308,21 +302,59 @@ namespace xDataService.DI
.ForEach(r => .ForEach(r =>
{ {
// //
Log($"Start Using Repository of: {r.Entity}"); source.UseXRepository(
descriptor: r,
isDevelopmentEnvironment: isDevelopmentEnvironment
);
});
}
}
/// <summary>
/// Use a Repository Middlewares by Providing it's Descriptor ...
/// </summary>
/// <param name="source"></param>
/// <param name="descriptor"></param>
/// <param name="isDevelopmentEnvironment"></param>
public static void UseXRepository(
this IApplicationBuilder source,
XRepositoryDescriptor descriptor,
bool isDevelopmentEnvironment = false
)
{
//
Log($"Start Using Repository of: {descriptor.Entity}");
// //
// Check Seeder Exists ... var withPlayground = false;
// Execute Seeding Data Task if Exists ... if (isDevelopmentEnvironment)
isValid = r.HasSeeder(); {
withPlayground = true;
}
//
// Create an Scope for Services Acces ...
using (var scope = source.ApplicationServices.CreateScope())
{
//
// Inject XDataServiceConfiguration ForMake Sure DataService Registration ...
var dataServiceConfiguration = scope.ServiceProvider.GetService<XDataServiceConfiguration>();
var isValid = !dataServiceConfiguration.IsNullOrDefault();
if (isValid) if (isValid)
{ {
// //
var seeder = (dynamic)scope.ServiceProvider.GetService(r.SeederService); // Check Seeder Exists ...
// Execute Seeding Data Task if Exists ...
isValid = descriptor.HasSeeder();
if (isValid)
{
//
var seeder = (dynamic)scope.ServiceProvider.GetService(descriptor.SeederService);
isValid = seeder != null; isValid = seeder != null;
if (isValid) if (isValid)
{ {
// //
Log($"Start Seeding {r.Entity} ..."); Log($"Start Seeding {descriptor.Entity} ...");
// //
// Seeding ... // Seeding ...
@@ -335,19 +367,16 @@ namespace xDataService.DI
// //
// Check GraphQL Support ... // Check GraphQL Support ...
isValid = r.HasGraphSupport(); isValid = descriptor.HasGraphSupport();
if (isValid) if (isValid)
{ {
// //
// Use WebSockets ... // Use WebSockets ...
if (!isGraphConfigured)
{
source.UseWebSockets(); source.UseWebSockets();
}
// //
// Inject GraphQL Type Helper ... // Inject GraphQL Type Helper ...
var helper = (dynamic)scope.ServiceProvider.GetService(r.GraphTypeHelperService); var helper = (dynamic)scope.ServiceProvider.GetService(descriptor.GraphTypeHelperService);
isValid = helper != null; isValid = helper != null;
if (isValid) if (isValid)
{ {
@@ -357,14 +386,14 @@ namespace xDataService.DI
typeof(XDataServiceHelper).InvokeGenericMethod( typeof(XDataServiceHelper).InvokeGenericMethod(
methodName: "UseXGraphQL", methodName: "UseXGraphQL",
runtimeTypes: [ runtimeTypes: [
r.Entity, descriptor.Entity,
r.Key, descriptor.Key,
r.GraphType, descriptor.GraphType,
r.GraphTypeKey, descriptor.GraphTypeKey,
r.GraphQuery, descriptor.GraphQuery,
r.GraphTypeHelperService, descriptor.GraphTypeHelperService,
r.GraphTypeHelperImplementation, descriptor.GraphTypeHelperImplementation,
r.GraphSchema descriptor.GraphSchema
], ],
args: [ args: [
source, source,
@@ -377,14 +406,6 @@ namespace xDataService.DI
// //
Log($"Register GraphQL: {graphPath}"); Log($"Register GraphQL: {graphPath}");
} }
//
if (!isGraphConfigured)
{
isGraphConfigured = true;
}
}
});
} }
} }
} }