add support for Db Seeder, Fix GraphQL Generic Usage ...
This commit is contained in:
+79
-83
@@ -1,7 +1,7 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
@@ -11,7 +11,6 @@ using xDataService.Configuration;
|
||||
using xDataService.Constants;
|
||||
using xDataService.Extensions;
|
||||
using xDataService.Helpers;
|
||||
using xDataService.Interfaces;
|
||||
using xDataService.Models;
|
||||
using xExceptions.Constants;
|
||||
|
||||
@@ -34,6 +33,25 @@ namespace xDataService.DI
|
||||
.GetSection(ConfigurationNodeNames.DATA_SERVICE_NODE);
|
||||
var result = xDataServiceConfigSection.Get<XDataServiceConfiguration>();
|
||||
|
||||
//
|
||||
// Reading Seeders ...
|
||||
if (!result.IsNull() &&
|
||||
result.Databases.HasChild())
|
||||
{
|
||||
//
|
||||
result
|
||||
.Databases
|
||||
.ToList()
|
||||
.ForEach(db =>
|
||||
{
|
||||
//
|
||||
// TODO: Fix this ...
|
||||
var sectionName = $"{ConfigurationNodeNames.DATA_SERVICE_NODE}:{nameof(XDataServiceConfiguration.Databases)}:{db.Key}:{nameof(XDataBaseConfiguration.SeedItems)}";
|
||||
// var seedItems = source.GetSectionAsDictionary(sectionName);
|
||||
// db.Value.SeedItems = seedItems;
|
||||
});
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
@@ -229,7 +247,8 @@ namespace xDataService.DI
|
||||
|
||||
public static void UseXDatabase(
|
||||
this IApplicationBuilder source,
|
||||
XDatabaseDescriptor descriptor
|
||||
XDatabaseDescriptor descriptor,
|
||||
bool isDevelopmentEnvironment = false
|
||||
)
|
||||
{
|
||||
//
|
||||
@@ -244,6 +263,14 @@ namespace xDataService.DI
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var withPlayground = false;
|
||||
if (isDevelopmentEnvironment)
|
||||
{
|
||||
//
|
||||
withPlayground = true;
|
||||
}
|
||||
|
||||
//
|
||||
// Check Repositories Exists ...
|
||||
isValid = descriptor.HasRepositories();
|
||||
@@ -265,61 +292,66 @@ namespace xDataService.DI
|
||||
.ToList()
|
||||
.ForEach(r =>
|
||||
{
|
||||
//
|
||||
Log($"Start Using Repository of: {r.Entity}");
|
||||
|
||||
//
|
||||
// Check Seeder 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()
|
||||
.RunTask();
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Check GraphQL Support ...
|
||||
isValid = r.HasGraphSupport();
|
||||
if (isValid)
|
||||
{
|
||||
//
|
||||
// Inject GraphQL Type Helper ...
|
||||
var helper = scope.ServiceProvider.GetService(r.GraphTypeHelperService);
|
||||
isValid = !helper.IsNullOrDefault();
|
||||
if (!isValid)
|
||||
{
|
||||
//
|
||||
Log($"unable to find GraphQLTypeHelper for Entiy: {r.Entity}");
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
// Use WebSockets ...
|
||||
source.UseWebSockets();
|
||||
|
||||
//
|
||||
var methods = source.GetType().GetMethods();
|
||||
isValid =
|
||||
!methods.IsNull() &&
|
||||
methods.HasChild();
|
||||
// Inject GraphQL Type Helper ...
|
||||
var helper = (dynamic)scope.ServiceProvider.GetService(r.GraphTypeHelperService);
|
||||
isValid = helper != null;
|
||||
if (isValid)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// UseGraphQL ...
|
||||
var method = methods.First(m =>
|
||||
m.IsGenericMethod &&
|
||||
m.Name == "UseGraphQL" &&
|
||||
m.GetGenericArguments().Length == 1 &&
|
||||
m.GetParameters().Length == 1
|
||||
);
|
||||
var genericMethod = method.MakeGenericMethod(r.GraphSchema);
|
||||
genericMethod.Invoke(null, ((dynamic)helper).GetGraphQLPath());
|
||||
|
||||
//
|
||||
// UseGraphQLWebSockets ...
|
||||
method = methods.First(m =>
|
||||
m.IsGenericMethod &&
|
||||
m.Name == "UseGraphQLWebSockets" &&
|
||||
m.GetGenericArguments().Length == 1 &&
|
||||
m.GetParameters().Length == 0
|
||||
);
|
||||
genericMethod = method.MakeGenericMethod(r.GraphSchema);
|
||||
genericMethod.Invoke(null, null);
|
||||
|
||||
//
|
||||
Log($"Register GraphQL Middleware for Entity {r.Entity} Successfully ...");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log($"Registration GraphQL Middleware for Entity {r.Entity} failed due {ex.Message} ...");
|
||||
}
|
||||
string graphPath = helper.GetGraphQLPath();
|
||||
typeof(XDataServiceHelper).InvokeGenericMethod(
|
||||
methodName: "UseXGraphQL",
|
||||
runtimeTypes: [
|
||||
r.Entity,
|
||||
r.Key,
|
||||
r.GraphType,
|
||||
r.GraphTypeKey,
|
||||
r.GraphQuery,
|
||||
r.GraphTypeHelperService,
|
||||
r.GraphTypeHelperImplementation,
|
||||
r.GraphSchema
|
||||
],
|
||||
args: [
|
||||
source,
|
||||
graphPath,
|
||||
withPlayground,
|
||||
null
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -343,42 +375,6 @@ namespace xDataService.DI
|
||||
{
|
||||
Console.WriteLine($"{XLogTag} => {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Do Seeding Initialization Data on DbContext
|
||||
/// </summary>
|
||||
/// <param name="app"></param>
|
||||
private static void SeedData(
|
||||
this IApplicationBuilder app
|
||||
)
|
||||
{
|
||||
//
|
||||
using (var scope = app.ApplicationServices.CreateScope())
|
||||
{
|
||||
//
|
||||
var dbSeeder = scope.ServiceProvider.GetService<IXDbSeeder>();
|
||||
Console.WriteLine($"XDataService: dbSeeder retrieved from DI is => {!dbSeeder.IsNull()}");
|
||||
|
||||
//
|
||||
if (!dbSeeder.IsNull())
|
||||
{
|
||||
try
|
||||
{
|
||||
//
|
||||
dbSeeder.Seed()
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
Console.WriteLine(" ");
|
||||
Console.WriteLine($"XDataService: Seeding Error: {ex.Message}");
|
||||
XException.InvalidConfiguration.Throw();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user