CQRS_Simple/CQRS_Simple.Infrastructure/Dapper/SqlConnectionFactory.cs
2022-04-07 14:00:58 +08:00

44 lines
1.3 KiB
C#

using System;
using System.Data;
using System.Data.SqlClient;
using MySqlConnector;
namespace CQRS_Simple.Infrastructure.Dapper
{
public class SqlConnectionFactory : ISqlConnectionFactory, IDisposable
{
private readonly string _connectionString;
private IDbConnection _connection;
public SqlConnectionFactory(string connectionString)
{
this._connectionString = connectionString;
}
public IDbConnection GetOpenConnection()
{
if (this._connection == null || this._connection.State != ConnectionState.Open)
{
//this._connection = new SqlConnection(_connectionString);
this._connection = new MySqlConnection(_connectionString)
{
Site = null,
ProvideClientCertificatesCallback = null,
ProvidePasswordCallback = null,
RemoteCertificateValidationCallback = null
};
this._connection.Open();
}
return this._connection;
}
public void Dispose()
{
if (this._connection != null && this._connection.State == ConnectionState.Open)
{
this._connection.Dispose();
}
}
}
}