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
}
}
+137 -116
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
] ]
); );
@@ -232,34 +231,47 @@ namespace xDataService.DI
descriptor.Repositories.ToList().ForEach(r => descriptor.Repositories.ToList().ForEach(r =>
{ {
// //
// Preparing Runtime Types ... source.AddXRepository(
Type[] runtimeTypes = r.GetType().GenericTypeArguments; descriptor: r,
lifeTime: lifeTime
// );
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} ...");
}
}); });
} }
} }
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( public static void UseXDatabase(
this IApplicationBuilder source, this IApplicationBuilder source,
XDatabaseDescriptor descriptor, XDatabaseDescriptor descriptor,
@@ -278,113 +290,122 @@ namespace xDataService.DI
XException.InvalidArgs.Throw(); 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
);
});
}
}
/// <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}");
// //
var withPlayground = false; var withPlayground = false;
var isGraphConfigured = false;
if (isDevelopmentEnvironment) if (isDevelopmentEnvironment)
{ {
withPlayground = true; withPlayground = true;
} }
// //
// Check Repositories Exists ... // Create an Scope for Services Acces ...
isValid = descriptor.HasRepositories(); using (var scope = source.ApplicationServices.CreateScope())
if (isValid)
{ {
// //
// Create an Scope for Services Acces ... // Inject XDataServiceConfiguration ForMake Sure DataService Registration ...
using (var scope = source.ApplicationServices.CreateScope()) var dataServiceConfiguration = scope.ServiceProvider.GetService<XDataServiceConfiguration>();
var isValid = !dataServiceConfiguration.IsNullOrDefault();
if (isValid)
{ {
// //
// Inject XDataServiceConfiguration ForMake Sure DataService Registration ... // Check Seeder Exists ...
var dataServiceConfiguration = scope.ServiceProvider.GetService<XDataServiceConfiguration>(); // Execute Seeding Data Task if Exists ...
isValid = !dataServiceConfiguration.IsNullOrDefault(); isValid = descriptor.HasSeeder();
if (isValid) if (isValid)
{ {
// //
// Loop through Repositories ... var seeder = (dynamic)scope.ServiceProvider.GetService(descriptor.SeederService);
descriptor.Repositories isValid = seeder != null;
.ToList() if (isValid)
.ForEach(r => {
{ //
// Log($"Start Seeding {descriptor.Entity} ...");
Log($"Start Using Repository of: {r.Entity}");
// //
// Check Seeder Exists ... // Seeding ...
// Execute Seeding Data Task if Exists ... seeder
isValid = r.HasSeeder(); .Seed()
if (isValid) .GetAwaiter()
{ .GetResult();
// }
var seeder = (dynamic)scope.ServiceProvider.GetService(r.SeederService); }
isValid = seeder != null;
if (isValid)
{
//
Log($"Start Seeding {r.Entity} ...");
// //
// Seeding ... // Check GraphQL Support ...
seeder isValid = descriptor.HasGraphSupport();
.Seed() if (isValid)
.GetAwaiter() {
.GetResult(); //
} // Use WebSockets ...
} source.UseWebSockets();
// //
// Check GraphQL Support ... // Inject GraphQL Type Helper ...
isValid = r.HasGraphSupport(); var helper = (dynamic)scope.ServiceProvider.GetService(descriptor.GraphTypeHelperService);
if (isValid) isValid = helper != null;
{ if (isValid)
// {
// Use WebSockets ... //
if (!isGraphConfigured) var graphPath = $"{helper.GetGraphQLPath()}";
{ var graphBasePath = $"{dataServiceConfiguration.GraphQLBasePath}";
source.UseWebSockets(); 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 ... Log($"Register GraphQL: {graphPath}");
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;
}
}
});
} }
} }
} }