I'm trying to implement the Strategy Pattern into my current code base. I would like to know if i'm going in the right direction on this?
IOracleDB.cs:
/// <summary>
/// Strategy
/// </summary>
interface IOracleDB
{
DataSet DatabaseQuery(string query, OracleConnection oracleConnection);
DataSet DatabaseQuery(string procedure, string parameters, OracleConnection oracleConnection);
}
OracleDBContext.cs:
/// <summary>
/// Context
/// </summary>
class OracleDBContext
{
private readonly string _EmailConnectionString = ConfigurationManager
.ConnectionStrings["EmailConnectionString"].ConnectionString;
private readonly string _PhConnectionString = ConfigurationManager
.ConnectionStrings["PhConnectionString"].ConnectionString;
private readonly IOracleDB _oracleDB;
private readonly OracleConnection _oracleConnection;
public OracleDBContext(IOracleDB oracleDB, string table)
{
_oracleDB = oracleDB;
switch (table.ToUpper())
{
case "Email":
_oracleConnection = new OracleConnection(_EmailConnectionString);
return;
case "Ph":
_oracleConnection = new OracleConnection(_PhConnectionString);
return;
default:
return;
}
}
public void ConnectToDatabase()
{
_oracleConnection.Open();
}
public void DisconnectFromDatabase()
{
_oracleConnection.Close();
_oracleConnection.Dispose();
}
public string ConnectionStatus()
{
if (_oracleConnection != null)
{
return _oracleConnection.State.ToString();
}
return "OracleConnection is null.";
}
public DataSet DatabaseQuery(string query)
{
return _oracleDB.DatabaseQuery(query, _oracleConnection);
}
public DataSet DatabaseQuery(string procedure, string parameters)
{
return _oracleDB.DatabaseQuery(procedure, parameters, _oracleConnection);
}
}
EmailTableClass.cs:
/// <summary>
/// Concrete Strategy
/// </summary>
class EmailTableClass : IOracleDB
{
public DataSet DatabaseQuery(string query, OracleConnection oracleConnection)
{
DataSet dataSet = new DataSet();
OracleCommand cmd = new OracleCommand(query);
cmd.CommandType = CommandType.Text;
cmd.Connection = oracleConnection;
using (OracleDataAdapter dataAdapter = new OracleDataAdapter())
{
dataAdapter.SelectCommand = cmd;
dataAdapter.Fill(dataSet);
}
return dataSet;
}
public DataSet DatabaseQuery(string procedure, string parameters, OracleConnection oracleConnection)
{
throw new NotImplementedException();
}
}
PhTableClass.cs:
/// <summary>
/// Concrete Strategy
/// </summary>
class PhTableClass : IOracleDB
{
public DataSet DatabaseQuery(string query, OracleConnection oracleConnection)
{
throw new NotImplementedException();
}
public DataSet DatabaseQuery(string procedure, string parameters, OracleConnection oracleConnection)
{
DataSet dataSet = new DataSet();
OracleCommand cmd = new OracleCommand
{
CommandText = procedure,
CommandType = CommandType.StoredProcedure,
Connection = oracleConnection,
};
OracleParameter oracleParameter = new OracleParameter
{
ParameterName = parameters
};
using (OracleDataAdapter dataAdapter = new OracleDataAdapter())
{
dataAdapter.SelectCommand = cmd;
dataAdapter.Fill(dataSet);
}
return dataSet;
}
}