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
{
///
/// Retrieve Data Provider Type from Configurations
///
///
///
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;
}
///
/// Retrieve AspNet Identity Configurations
///
///
///
public static XIdentityConfiguration GetXIdentityConfiguration(this IConfiguration source)
{
//
var xIdentityConfigSection = source
.GetSection(ConfigurationNodeNames.IDENTITY_NODE_NAME);
return xIdentityConfigSection.Get();
}
///
/// Retrieve XIdentityResource Configuration from AppSettings
///
///
///
public static XIdentityResourceConfiguration GetXIdentityResourceConfiguration(this IConfiguration configuration)
{
//
var xIdentityResourceSection = configuration.GetSection(ConfigurationNodeNames.IDENTITY_RESOURCE_NODE_NAME);
return xIdentityResourceSection.Get();
}
///
/// Register XIdentityResourceConfiguration
///
///
///
public static void AddXIdentityResourceConfiguration(
this IServiceCollection services,
IConfiguration configuration
)
{
//
var xIdentityResourceConfiguration = configuration.GetXIdentityResourceConfiguration();
if (!xIdentityResourceConfiguration.IsNull())
{
services.AddSingleton(xIdentityResourceConfiguration);
}
}
///
/// Converts DbInfo Options Builder for MySql Usage
///
///
///
public static Action GetMySqlOptionsBuilder(this XDbInfo source)
{
return ((Action)source.OptionsBuilder);
}
///
/// Converts DbInfo Options Builder for SQLite Usage
///
///
///
public static Action GetSQLiteOptionsBuilder(this XDbInfo source)
{
return ((Action)source.OptionsBuilder);
}
///
/// Converts DbInfo Options Builder for SQLServer Usage
///
///
///
public static Action GetSQLServerOptionsBuilder(this XDbInfo source)
{
return ((Action)source.OptionsBuilder);
}
///
/// Retrieve Required Informations to Register DbContexts into Di as XDbInfo instance
///
///
///
public static XDbInfo GetXDbInfo(this IConfiguration configuration)
{
return new XDbInfo
{
ProviderType = configuration.GetXDbProviderType(),
MigrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name
};
}
///
/// Prepare DbContextOptionsBuilder with Propper data to Support Configured DbProvider
///
///
///
///
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()
);
}
}
}
}