locked
Need help on modifying the existing DBHelper.cs RRS feed

  • Question

  • User1094269964 posted

    Hi Friends,

    We are developing a small application and we got the requirement that the application will have multiple connection string.

    One connection string is specific to the application on which we are developing and the rest of the connection string are third parties.

    Lets say our application connection string Connection1 in that we are having a table which will have list of thirdparties names

    For example a Connection1 have Database1 and which as table called AppName

    Table: AppName below records

    AppName_COL    |  TableName_Col
    ThirdParty1            table1
    ThirdParty1            table5
    ThirdParty2            table7
    ThirdParty3            table2
    ThirdParty1            table2
    ThirdParty4            table50

    when the application runs the users will be given options to select AppName_COL.

    Base on the user selection we need to pick the corresponding connection from web.config and issue select * from corresponding table

    How can we do this with the below code. How can i inject the connection string int the DBHelper method

    namespace ConsoleApp1
    {
         public sealed class DBHelper
    {
    static readonly string ORACLE_CONN_STR = string.Empty;
    static DBHelper()
    {
    string environment = Convert.ToString(ConfigurationManager.AppSettings["CurrentEnvironment"]);
    switch (environment)
    {
    case "DEVEnvironment": ORACLE_CONN_STR = Convert.ToString(ConfigurationManager.ConnectionStrings["DEV_CON"]); break;
    case "UATEnvironment": ORACLE_CONN_STR = Convert.ToString(ConfigurationManager.ConnectionStrings["UAT_CON"]); break;
    case "PRODEnvironment": ORACLE_CONN_STR = Convert.ToString(ConfigurationManager.ConnectionStrings["PROD_CON"]); break;
    default:
    ORACLE_CONN_STR = string.Empty;
    break;
    }
    }
    public static int DoInsert(string commandText, CommandType commandType, OracleParameter[] parameters)
    {
    int result;
    using (OracleConnection connection = new OracleConnection(ORACLE_CONN_STR))
    {
    using (var Command = new OracleCommand(commandText, connection))
    {
    Command.CommandType = commandType;
    if (parameters != null)
    {
    foreach (var parameter in parameters)
    {
    Command.Parameters.Add(parameter);
    }
    }
    try
    {
    result = Command.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
    result = -1;
    }
    finally
    {
    connection.Close();
    }
    }
    }
    return result;
    }

    public static DataSet GetDataSet(string commandText, CommandType commandType, OracleParameter[] parameters)
    {
    using (OracleConnection connection = new OracleConnection(ORACLE_CONN_STR))
    {
    using (var command = new OracleCommand(commandText, connection))
    {
    command.CommandType = commandType;
    if (parameters != null)
    {
    foreach (var parameter in parameters)
    {
    command.Parameters.Add(parameter);
    }
    }
    var dataset = new DataSet();
    var dataAdapter = new OracleDataAdapter(command);
    dataAdapter.Fill(dataset);
    return dataset;
    }
    }
    }
    }

    public class AdminDAL
    {
    public static int AddUser(string userid, string username)
    {
    OracleParameter[] param = null;
    param = new OracleParameter[]
    {
    new OracleParameter("USER_ID",userid),
    new OracleParameter("USER_NAME",username)
    };
    return DBHelper.DoInsert("INSERTPROC", CommandType.StoredProcedure, param);
    }

    public static DataSet GetTablesByAppName(string appName)
    {
    OracleParameter[] param = null;
    param = new OracleParameter[]
    {
    new OracleParameter("APP_NAME",appName)
    };
    return DBHelper.GetDataSet("GetProc", CommandType.StoredProcedure, param);
    }

    public static DataSet GetThirdPartyDataByName(string thirdPartyDBName, string tableName)
    {
    var commandText = "SELECT * FROM " + tableName;
    var connectionString = Convert.ToString(ConfigurationManager.ConnectionStrings[thirdPartyDBName]);
    //Here is the problem the existing code of DataHelper is reading the connection string which is specific to whole application
    //But there ThirdParty connection gives some data to the application
    return DBHelper.GetDataSet("commandText", CommandType.Text, null);
    }
    }
    class Program
    {
    static void Main(string[] args)
    {
    string appUserinput = Console.ReadLine();
    string tableName = Convert.ToString(AdminDAL.GetTablesByAppName(appUserinput).Tables[0].Rows[0]["TableName"]);
    DataSet ds=AdminDAL.GetThirdPartyDataByName(appUserinput, tableName);


    }
    }
    }

    App.config

    <configuration>
    <appSettings>
    <add key="CurrentEnvironment" value="DEVEnvironment"/>
    </appSettings>
    <connectionStrings>
    <add name="DEV_CON" connectionString="Dev Connection"/>
    <add name="UAT_CON" connectionString="Uat Connection"/>
    <add name="PROD_CON" connectionString="Prod Connection"/>

    <!-- These are application specific connection-->
    <add name="ThirdParty1" connectionString="ThirdParty1 Connection"/>
    <add name="ThirdParty2" connectionString="ThirdParty2 Connection"/>
    <add name="ThirdParty3" connectionString="ThirdParty3 Connection"/>
    <add name="ThirdParty4" connectionString="ThirdParty4 Connection"/>

    </connectionStrings>
    </configuration>

    Friday, July 17, 2020 5:43 PM

All replies

  • User475983607 posted

    The current DBHelper design does not support the new requirement.  DBHelper has hard coded dependencies on a configuration file plus all the methods are static. 

    Change the DBHelper so it is an instance type.  Then you can use a constructor to pass a dynamic connection string or interface to the DBHelper instance.

    If you want to stick with static then you need to pass the connection string to the method.

    Friday, July 17, 2020 6:31 PM