last ...
This commit is contained in:
+155
-85
@@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
@@ -10,7 +9,7 @@ using xCommons.Extensions;
|
||||
using xCommons.Helpers;
|
||||
using xDataService.Configuration;
|
||||
using xDataService.Constants;
|
||||
using xDataService.Db;
|
||||
using xDataService.Extensions;
|
||||
using xDataService.Helpers;
|
||||
using xDataService.Interfaces;
|
||||
using xDataService.Models;
|
||||
@@ -84,6 +83,13 @@ namespace xDataService.DI
|
||||
source.AddSingleton(configuration);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register a Data Base using Descriptor ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="configuration"></param>
|
||||
/// <param name="descriptor"></param>
|
||||
/// <param name="lifetime"></param>
|
||||
public static void AddXDatabase(
|
||||
this IServiceCollection source,
|
||||
IConfiguration configuration,
|
||||
@@ -162,35 +168,165 @@ namespace xDataService.DI
|
||||
!descriptor.ConnectionString.IsNullOrEmpty())
|
||||
{
|
||||
//
|
||||
typeof(XDataServiceHelper).InvokeGenericMethod(
|
||||
methodName: "AddXDbContext",
|
||||
runtimeType: descriptor.Context,
|
||||
args: [
|
||||
source,
|
||||
try
|
||||
{
|
||||
//
|
||||
typeof(XDataServiceHelper).InvokeGenericMethod(
|
||||
methodName: "AddXDbContext",
|
||||
runtimeType: descriptor.Context,
|
||||
args: [
|
||||
source,
|
||||
descriptor.Provider,
|
||||
descriptor.ConnectionString,
|
||||
lifetime,
|
||||
descriptor.OptionsBuilder
|
||||
]
|
||||
);
|
||||
]
|
||||
);
|
||||
|
||||
//
|
||||
// source.AddXDbContext<TContext>(
|
||||
// lifetime: lifetime,
|
||||
// provider: descriptor.Provider,
|
||||
// optionsBuilder: descriptor.OptionsBuilder,
|
||||
// connectionString: descriptor.ConnectionString
|
||||
// );
|
||||
//
|
||||
Log($"DbContext: {descriptor.Name} Registered Successfully ...");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log($"DbContext: {descriptor.Name} Registration failed due {ex.Message} ...");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// Register Repositories ...
|
||||
if (!descriptor.Repositories.IsNull() &&
|
||||
descriptor.Repositories.HasChild())
|
||||
{
|
||||
// source.AddXDataRepositories(
|
||||
//
|
||||
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} ...");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public static void UseXDatabase(
|
||||
this IApplicationBuilder source,
|
||||
XDatabaseDescriptor descriptor
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
var isValid =
|
||||
!source.IsNull() &&
|
||||
descriptor.IsValid();
|
||||
if (!isValid)
|
||||
{
|
||||
//
|
||||
Log("Invalid Database Configuration ...");
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Check Repositories Exists ...
|
||||
isValid = descriptor.HasRepositories();
|
||||
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 ...
|
||||
descriptor.Repositories
|
||||
.ToList()
|
||||
.ForEach(r =>
|
||||
{
|
||||
//
|
||||
// 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();
|
||||
}
|
||||
|
||||
//
|
||||
var methods = source.GetType().GetMethods();
|
||||
isValid =
|
||||
!methods.IsNull() &&
|
||||
methods.HasChild();
|
||||
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} ...");
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -210,72 +346,6 @@ namespace xDataService.DI
|
||||
Console.WriteLine($"{XLogTag} => {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register DbContext ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="provider"></param>
|
||||
/// <param name="connectionString"></param>
|
||||
/// <param name="lifetime"></param>
|
||||
/// <param name="optionsBuilder"></param>
|
||||
/// <typeparam name="TContext"></typeparam>
|
||||
private static void AddXDbContext<TContext>(
|
||||
this IServiceCollection source,
|
||||
XDbProviders provider,
|
||||
string connectionString,
|
||||
ServiceLifetime lifetime = ServiceLifetime.Scoped,
|
||||
Action<dynamic> optionsBuilder = null
|
||||
)
|
||||
where TContext : XDbContext
|
||||
{
|
||||
//
|
||||
if (provider == XDbProviders.None)
|
||||
{
|
||||
//
|
||||
Log("Invalid Db Provider ...");
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
switch (provider)
|
||||
{
|
||||
//
|
||||
// MySQL ...
|
||||
case XDbProviders.MySQL:
|
||||
//
|
||||
source.AddDbContext<TContext>(cfg =>
|
||||
{
|
||||
cfg.UseMySQL(connectionString, optionsBuilder);
|
||||
}, lifetime);
|
||||
break;
|
||||
|
||||
//
|
||||
// SQLite ...
|
||||
case XDbProviders.SQLite:
|
||||
//
|
||||
source.AddDbContext<TContext>(cfg =>
|
||||
{
|
||||
cfg.UseSqlite(connectionString, optionsBuilder);
|
||||
}, lifetime);
|
||||
break;
|
||||
|
||||
//
|
||||
// SQLServer ...
|
||||
case XDbProviders.SQLServer:
|
||||
//
|
||||
source.AddDbContext<TContext>(cfg =>
|
||||
{
|
||||
cfg.UseSqlServer(connectionString, optionsBuilder);
|
||||
}, lifetime);
|
||||
break;
|
||||
|
||||
//
|
||||
case XDbProviders.MongoDB:
|
||||
// There is notRequired to Register DBConext forMongo DB ...
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Do Seeding Initialization Data on DbContext
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user