196 lines
6.3 KiB
C#
196 lines
6.3 KiB
C#
using System;
|
|
using System.Data;
|
|
using Ease.Core.Utility;
|
|
using Ease.Core.DataAccess;
|
|
using System.Collections.Generic;
|
|
using Microsoft.Extensions.Configuration;
|
|
using System.IO;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
namespace Ease.Core.DataAccess
|
|
{
|
|
#region DataAccess: Connection Facory
|
|
|
|
public sealed class DBConnections
|
|
{
|
|
private static DbConfig _allConnections = null;
|
|
private static string _defaultConnection = null;
|
|
private static readonly object _lock = new object();
|
|
public static IConfigurationRoot _configuration;
|
|
|
|
DBConnections()
|
|
{
|
|
}
|
|
|
|
public static DbConfig Connections
|
|
{
|
|
get
|
|
{
|
|
if (_allConnections == null)
|
|
{
|
|
lock (_lock)
|
|
{
|
|
// create the instance only if the instance is null
|
|
if (_allConnections == null)
|
|
{
|
|
//string str[] = Microsoft.Extensions.Configuration.ConfigurationSection
|
|
_allConnections = DbConfig.GetConfig();
|
|
}
|
|
}
|
|
}
|
|
|
|
// Otherwise return the already existing instance
|
|
return _allConnections;
|
|
}
|
|
}
|
|
|
|
public static string DefaultConnection
|
|
{
|
|
get
|
|
{
|
|
//if (_defaultConnection == null || _defaultConnection == "")
|
|
//{
|
|
// lock (_lock)
|
|
// {
|
|
// // create the instance only if the instance is null
|
|
// if (_defaultConnection == null)
|
|
// {
|
|
// _defaultConnection = _allConnections["Default"].ConnectionString;
|
|
// }
|
|
// }
|
|
//}
|
|
_defaultConnection = "Default";
|
|
// Otherwise return the already existing instance
|
|
return _defaultConnection;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public abstract class ConnectionFactory
|
|
{
|
|
#region Variable declaration
|
|
|
|
private static int _connectionIdx = 0;
|
|
private static List<string> _dataSources;
|
|
|
|
private static DbConfig.ConnectionNode _cn;
|
|
//private static DbConfig _dbCfg = DBConnections.Connections;
|
|
|
|
#endregion
|
|
|
|
#region Properties
|
|
|
|
internal SQLSyntax Syntax
|
|
{
|
|
get { return _cn.Syntax; }
|
|
}
|
|
|
|
internal static Provider Provider
|
|
{
|
|
get { return _cn.Provider; }
|
|
}
|
|
|
|
internal static List<string> DataSources
|
|
{
|
|
get { return _dataSources; }
|
|
}
|
|
|
|
internal static int ConnectionCount
|
|
{
|
|
get { return DBConnections.Connections.Names.Length; }
|
|
}
|
|
|
|
internal DbConfig.ConnectionNode ConnectionNode
|
|
{
|
|
get { return _cn; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// This property is used to point the default database
|
|
/// </summary>
|
|
public static ConnectionFactory Default
|
|
{
|
|
get
|
|
{
|
|
//_dataSources = new List<string>();
|
|
//If no setting have been found
|
|
//if (string.IsNullOrEmpty(DBConnections.DefaultConnection))
|
|
// throw new ArgumentException(string.Format("Either serviceHandler key in app setting is missing.{0}Or{0}type key in serviceConfiguration is missing.", Environment.NewLine));
|
|
////Preparing Datasources
|
|
//_dataSources.AddRange(DBConnections.DefaultConnection.Split('~'));
|
|
//if (_dataSources == null || _dataSources.Count <= 0)
|
|
// throw new Exception("Data source is not specified in defaultDB.");
|
|
|
|
//Findout actual ConnectionName
|
|
// if (DBConnections.Connections != null)
|
|
_cn = DBConnections.Connections[DBConnections.DefaultConnection];
|
|
|
|
if (_cn == null)
|
|
throw new Exception(string.Format("Invalid use of configuration setting: {0}",
|
|
DBConnections.DefaultConnection));
|
|
return _cn.Factory;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Functions
|
|
|
|
internal static void SetConnectionIndex(int connectionIdx)
|
|
{
|
|
_connectionIdx = connectionIdx;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create an instatnce of connection factory using valid connectioncontext.
|
|
/// </summary>
|
|
/// <param name="context">Valid connection context.</param>
|
|
/// <returns>Return an instatnce of connection factory.</returns>
|
|
public static ConnectionFactory ContextConnection(ConnectionContext context)
|
|
{
|
|
if (context == null || context.ContextKey.Length <= 0)
|
|
throw new Exception("Invalid use of configuration setting");
|
|
|
|
//string conKey = ConfigUtility.GetAppSettings(context.ContextKey);
|
|
|
|
////If no setting have been found
|
|
//if (string.IsNullOrEmpty(conKey))
|
|
// throw new ArgumentException(string.Format("Please use serviceHandler key: {0} in app setting.", context.ContextKey));
|
|
|
|
if (_cn != null && _cn.Name.Equals(context.ContextKey))
|
|
return _cn.Factory;
|
|
|
|
if (DBConnections.Connections != null)
|
|
_cn = DBConnections.Connections[context.ContextKey];
|
|
|
|
if (_cn == null)
|
|
throw new Exception("Invalid use of configuration setting");
|
|
|
|
return _cn.Factory;
|
|
}
|
|
|
|
|
|
public static ConnectionFactory CreateConnection(Provider provider, SQLSyntax syntax, string connectionString)
|
|
{
|
|
if (_cn == null || _cn.ConnectionString.ToLower() != connectionString.ToLower())
|
|
{
|
|
_cn = new DbConfig.ConnectionNode();
|
|
_cn.CreateNode(provider, syntax, connectionString);
|
|
}
|
|
|
|
if (_cn == null)
|
|
throw new Exception("Invalid use of configuration setting");
|
|
|
|
return _cn.Factory;
|
|
}
|
|
|
|
public abstract IDbConnection CreateConnection();
|
|
public abstract IDbConnection CreateConnection(ConnectionContext context);
|
|
|
|
#endregion
|
|
}
|
|
|
|
#endregion
|
|
} |