168 lines
6.0 KiB
C#
168 lines
6.0 KiB
C#
using System;
|
|
using System.Reflection;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using MySql.EntityFrameworkCore.Infrastructure;
|
|
using xCommons.Extensions;
|
|
using xIdentityModels.Configurations;
|
|
using xIds.Configurations;
|
|
using xIds.Constants;
|
|
using xIds.Models;
|
|
|
|
namespace xIds.Extensions
|
|
{
|
|
public static class DbExtensions
|
|
{
|
|
/// <summary>
|
|
/// Retrieve Data Provider Type from Configurations
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static XDbProviders GetXDbProviderType(this IConfiguration source)
|
|
{
|
|
//
|
|
var dbProvider = (source[$"{ConfigurationNodeNames.PROVIDER_NODE_NAME}"])
|
|
.ToNormalString();
|
|
|
|
//
|
|
return dbProvider == ProviderType.SQLServer.ToNormalString() ?
|
|
XDbProviders.SQLServer :
|
|
dbProvider == ProviderType.SQLite.ToNormalString() ?
|
|
XDbProviders.SQLite :
|
|
dbProvider == ProviderType.MySQL.ToNormalString() ?
|
|
XDbProviders.MySQL :
|
|
XDbProviders.None;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve AspNet Identity Configurations
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static XIdentityConfiguration GetXIdentityConfiguration(this IConfiguration source)
|
|
{
|
|
//
|
|
var xIdentityConfigSection = source
|
|
.GetSection(ConfigurationNodeNames.IDENTITY_NODE_NAME);
|
|
return xIdentityConfigSection.Get<XIdentityConfiguration>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve XIdentityResource Configuration from AppSettings
|
|
/// </summary>
|
|
/// <param name="configuration"></param>
|
|
/// <returns></returns>
|
|
public static XIdentityResourceConfiguration GetXIdentityResourceConfiguration(this IConfiguration configuration)
|
|
{
|
|
//
|
|
var xIdentityResourceSection = configuration.GetSection(ConfigurationNodeNames.IDENTITY_RESOURCE_NODE_NAME);
|
|
return xIdentityResourceSection.Get<XIdentityResourceConfiguration>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register XIdentityResourceConfiguration
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="configuration"></param>
|
|
public static void AddXIdentityResourceConfiguration(
|
|
this IServiceCollection services,
|
|
IConfiguration configuration
|
|
)
|
|
{
|
|
//
|
|
var xIdentityResourceConfiguration = configuration.GetXIdentityResourceConfiguration();
|
|
if (!xIdentityResourceConfiguration.IsNull())
|
|
{
|
|
services.AddSingleton<XIdentityResourceConfiguration>(xIdentityResourceConfiguration);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts DbInfo Options Builder for MySql Usage
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static Action<MySQLDbContextOptionsBuilder> GetMySqlOptionsBuilder(this XDbInfo source)
|
|
{
|
|
return ((Action<MySQLDbContextOptionsBuilder>)source.OptionsBuilder);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts DbInfo Options Builder for SQLite Usage
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static Action<SqliteDbContextOptionsBuilder> GetSQLiteOptionsBuilder(this XDbInfo source)
|
|
{
|
|
return ((Action<SqliteDbContextOptionsBuilder>)source.OptionsBuilder);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts DbInfo Options Builder for SQLServer Usage
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static Action<SqlServerDbContextOptionsBuilder> GetSQLServerOptionsBuilder(this XDbInfo source)
|
|
{
|
|
return ((Action<SqlServerDbContextOptionsBuilder>)source.OptionsBuilder);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve Required Informations to Register DbContexts into Di as XDbInfo instance
|
|
/// </summary>
|
|
/// <param name="configuration"></param>
|
|
/// <returns></returns>
|
|
public static XDbInfo GetXDbInfo(this IConfiguration configuration)
|
|
{
|
|
return new XDbInfo
|
|
{
|
|
ProviderType = configuration.GetXDbProviderType(),
|
|
MigrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Prepare DbContextOptionsBuilder with Propper data to Support Configured DbProvider
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="dbInfo"></param>
|
|
/// <param name="connectionString"></param>
|
|
public static void PrepareXDbContextOptionsBuilder(
|
|
this DbContextOptionsBuilder source,
|
|
XDbInfo dbInfo,
|
|
string connectionString
|
|
)
|
|
{
|
|
//
|
|
if (dbInfo.ProviderType == XDbProviders.MySQL)
|
|
{
|
|
//
|
|
// Add Support for MySql ...
|
|
source.UseMySQL(
|
|
connectionString,
|
|
dbInfo.GetMySqlOptionsBuilder()
|
|
);
|
|
}
|
|
else if (dbInfo.ProviderType == XDbProviders.SQLite)
|
|
{
|
|
//
|
|
// Add Support for SQLite ...
|
|
source.UseSqlite(
|
|
connectionString,
|
|
dbInfo.GetSQLiteOptionsBuilder()
|
|
);
|
|
}
|
|
else if (dbInfo.ProviderType == XDbProviders.SQLServer)
|
|
{
|
|
//
|
|
// Add Support for SQLServer ...
|
|
source.UseSqlServer(
|
|
connectionString,
|
|
dbInfo.GetSQLServerOptionsBuilder()
|
|
);
|
|
}
|
|
}
|
|
}
|
|
} |