59 lines
1.9 KiB
C#
Executable File
59 lines
1.9 KiB
C#
Executable File
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Threading;
|
|
|
|
namespace Opsoft.NetCore.O3App.Shared.Data
|
|
{
|
|
public class ConnectionFactory
|
|
{
|
|
private static ConnectionFactory _instance;
|
|
|
|
private static object _syncRoot = new object();
|
|
|
|
private IDictionary<string, string> _assmblyName;
|
|
|
|
public static ConnectionFactory Instance
|
|
{
|
|
get
|
|
{
|
|
ConnectionFactory instance = _instance;
|
|
if (instance == null)
|
|
{
|
|
lock (_syncRoot)
|
|
{
|
|
instance = Volatile.Read(ref _instance);
|
|
if (instance == null)
|
|
{
|
|
instance = new ConnectionFactory();
|
|
}
|
|
Volatile.Write(ref _instance, instance);
|
|
return instance;
|
|
}
|
|
}
|
|
return instance;
|
|
}
|
|
}
|
|
|
|
private ConnectionFactory()
|
|
{
|
|
_assmblyName = new Dictionary<string, string>
|
|
{
|
|
{ "System.Data.SqlClient", "Opsoft.Data.SqlServer" },
|
|
{ "MySql.Data.MySqlClient", "Opsoft.Data.MySql" },
|
|
{ "Npgsql", "Opsoft.Data.PostgreSql" }
|
|
};
|
|
}
|
|
|
|
public IConnectionFactory GetByProvider(string providerName)
|
|
{
|
|
if (!_assmblyName.TryGetValue(providerName, out var value))
|
|
{
|
|
//Log.Error("数据库驱动名称" + providerName + "尚未被支持");
|
|
throw new ArgumentException("数据库驱动名称" + providerName + "尚未被支持", nameof(providerName));
|
|
}
|
|
return ExtensionLoader.Instance.GetService<IConnectionFactory>(value);
|
|
}
|
|
}
|
|
}
|