Files
bnhtrade/src/bnhtrade.Core/Data/Database/Connection.cs
T

79 lines
3.5 KiB
C#

using FikaAmazonAPI.AmazonSpApiSDK.Models.Services;
using System;
using System.Configuration;
namespace bnhtrade.Core.Data.Database
{
/// this class needs a sort out. Ideally it shoud be called what it is, a connection string builder, and
/// it should expose a method to serve the connection string--rather than class inheritance and using a property setter
/// something to do once there aren't so many open git branches
///
public class Connection
{
private string _server;
private string _user;
private string _userPassword;
private string _database = "e2A";
private bool _persistSecurityInfo = true;
private bool _multipleActiveResultSets = true;
private bool _encrypt = true;
private uint _connectRetryInterval;
private uint _connectRetryCount;
private uint _connectTimeout;
internal string SqlConnectionString { get; private set; }
/// <summary>
///
/// </summary>
/// <param name="connectRetryInterval">Retry interval in seconds, must be 5-60</param>
/// <param name="connectTimeout">Timeout length in seconds (0 is indefinitely)</param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
/// <exception cref="Exception"></exception>
public Connection(uint connectRetryInterval = 10, uint connectRetryCount = 6, uint connectionTimeout = 60)
{
if (connectRetryInterval < 5 || connectRetryInterval > 60)
{
// these are limits set by the sql server
throw new ArgumentOutOfRangeException("ConnectRetryInterval must be from 5 to 60 seconds");
}
_connectRetryInterval = connectRetryInterval;
_connectRetryCount = connectRetryCount;
_connectTimeout = connectionTimeout;
// retrive credentials from app.local.config
var config = new Config().GetConfiguration();
try
{
_server = config.AppSettings.Settings["DbDataSource"].Value;
_user = config.AppSettings.Settings["DbUserId"].Value;
_userPassword = config.AppSettings.Settings["DbUserPassword"].Value;
// check
if (string.IsNullOrEmpty(_server))
{
throw new ArgumentException("Could not retrive 'DbDataSource' from config file");
}
else if (string.IsNullOrEmpty(_user))
{
throw new ArgumentException("Could not retrive 'DbUserId' from config file");
}
else if (string.IsNullOrEmpty(_userPassword))
{
throw new ArgumentException("Could not retrive 'DbUserPassword' from config file");
}
}
catch (Exception ex)
{
throw new Exception("Unable to retirve DB credentials: " + ex.Message);
}
// build connection string
SqlConnectionString = "Server=" + _server + ";Database=" + _database + ";PersistSecurityInfo=" + _persistSecurityInfo.ToString()
+ ";User=" + _user + ";Password=" + _userPassword + ";MultipleActiveResultSets=" + _multipleActiveResultSets.ToString()
+ ";ConnectRetryInterval=" + _connectRetryInterval + ";ConnectRetryCount=" + _connectRetryCount + ";Timeout=" + _connectTimeout
+ ";Encrypt=" + _encrypt.ToString() +";";
}
}
}