316 lines
9.9 KiB
C#
316 lines
9.9 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using xCommons.Extensions;
|
|
using xCommons.Helpers;
|
|
using xDataService.Configuration;
|
|
using xDataService.Constants;
|
|
using xDataService.Db;
|
|
using xDataService.Helpers;
|
|
using xDataService.Interfaces;
|
|
using xDataService.Models;
|
|
using xExceptions.Constants;
|
|
|
|
namespace xDataService.DI
|
|
{
|
|
public static partial class XDIHelperExtension
|
|
{
|
|
/// <summary>
|
|
/// Retrive XDataService Configurations
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="connectionName"></param>
|
|
/// <returns></returns>
|
|
public static XDataServiceConfiguration GetXDataServiceConfiguration(
|
|
this IConfiguration source
|
|
)
|
|
{
|
|
//
|
|
var xDataServiceConfigSection = source
|
|
.GetSection(ConfigurationNodeNames.DATA_SERVICE_NODE);
|
|
var result = xDataServiceConfigSection.Get<XDataServiceConfiguration>();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register XDataService Configuration ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="configuration"></param>
|
|
public static void AddXDataServiceConfiguration(
|
|
this IServiceCollection source,
|
|
IConfiguration configuration
|
|
)
|
|
{
|
|
//
|
|
var config = configuration.GetXDataServiceConfiguration();
|
|
if (config.IsNullOrDefault())
|
|
{
|
|
//
|
|
Log("Configurations Not Founded ...");
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
source.AddSingleton(config);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register XDataService Configuration ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="configuration"></param>
|
|
public static void AddXDataServiceConfiguration(
|
|
this IServiceCollection source,
|
|
XDataServiceConfiguration configuration
|
|
)
|
|
{
|
|
//
|
|
if (configuration.IsNullOrDefault())
|
|
{
|
|
//
|
|
Log("Configurations Not Founded ...");
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
source.AddSingleton(configuration);
|
|
}
|
|
|
|
public static void AddXDatabase(
|
|
this IServiceCollection source,
|
|
IConfiguration configuration,
|
|
XDatabaseDescriptor descriptor,
|
|
ServiceLifetime lifetime = ServiceLifetime.Scoped
|
|
)
|
|
{
|
|
//
|
|
// Validate Database Descriptor ...
|
|
var isValid =
|
|
!descriptor.IsNullOrDefault() &&
|
|
!descriptor.Name.IsNullOrEmpty();
|
|
if (!isValid)
|
|
{
|
|
//
|
|
Log("Invalid Database Descriptor ...");
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Check Service Collection Validation ...
|
|
isValid =
|
|
!source.IsNull() &&
|
|
!configuration.IsNull();
|
|
if (!isValid)
|
|
{
|
|
//
|
|
Log("Invalid Service Provider or Configuration ...");
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Get Registere Data Service Configuration ...
|
|
var xDataServiceConfiguration = source
|
|
.GetRegisteredService<XDataServiceConfiguration>();
|
|
isValid = !xDataServiceConfiguration.IsNullOrDefault();
|
|
if (!isValid)
|
|
{
|
|
//
|
|
// Extract Configuration from Configuration ...
|
|
xDataServiceConfiguration = configuration.GetXDataServiceConfiguration();
|
|
isValid = !xDataServiceConfiguration.IsNullOrDefault();
|
|
if (!isValid)
|
|
{
|
|
//
|
|
Log("DataService Configuration notProvided ...");
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Register Configuration in DI ...
|
|
source.AddSingleton(xDataServiceConfiguration);
|
|
}
|
|
|
|
//
|
|
// Check Data Service Configuration has Database ...
|
|
isValid =
|
|
!xDataServiceConfiguration.Databases.IsNullOrDefault() &&
|
|
xDataServiceConfiguration.Databases.HasChild() &&
|
|
xDataServiceConfiguration.Databases.Keys.Contains(descriptor.Name);
|
|
if (!isValid)
|
|
{
|
|
//
|
|
Log("Invalid Database Descriptor ...");
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Configure Database Descriptor ...
|
|
descriptor.Configure(source);
|
|
|
|
//
|
|
// Register Context ...
|
|
if (!descriptor.Context.IsNull() &&
|
|
descriptor.Provider != XDbProviders.None &&
|
|
!descriptor.ConnectionString.IsNullOrEmpty())
|
|
{
|
|
//
|
|
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
|
|
// );
|
|
}
|
|
|
|
//
|
|
// Register Repositories ...
|
|
if (!descriptor.Repositories.IsNull() &&
|
|
descriptor.Repositories.HasChild())
|
|
{
|
|
// source.AddXDataRepositories(
|
|
|
|
// );
|
|
}
|
|
}
|
|
|
|
//
|
|
#region Private ...
|
|
/// <summary>
|
|
/// a LogTag for xDataService ...
|
|
/// </summary>
|
|
private static string XLogTag = "xDataService";
|
|
|
|
/// <summary>
|
|
/// print a log in Console ...
|
|
/// </summary>
|
|
/// <param name="message"></param>
|
|
private static void Log(string message)
|
|
{
|
|
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>
|
|
/// <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
|
|
}
|
|
} |