First of all sorry for the confusing title, I could not find the exact words to describe the situation. But it's easy to understand with an example.
We have a static class that holds the table names as a catalog like this:
public static class Tables
{
public const string Employees= "DBEmploy1";
public const string Sales = "Sale1";
// (...etc)
}
And use them in our code like this:
string sql = "select name, surname from " + Tables.Employees + " where code='1'"
But sometimes we need to prefix the database connection or another prefix/suffix to the tables. The current solution is to declare a second Table catalog:
public static class CurrentDB1Prefix = "[databasex].dbo."
public static class Tables
{
public const string Employees = "DBEmploy1";
public const string Sales = "Sale1";
// (...etc)
}
public static class TablesP
{
public static readonly string Employees = CurrentDB1Prefix + Employees;
public static readonly string Sales = CurrentDB1Prefix + Sales;
// (...etc)
}
And use them in our code like:
string sql = "select name, surname from " + TablesP.Employees + " where code='1'"
To save effort maintaining two table lists we would like to do something like this:
public static class CurrentDB1Prefix = "[databasex].dbo."
public static class Tables
{
public const string Employees= "DBEmploy1";
public const string Sales = "Sale1";
// (...etc)
}
public static class TablesP
{
//this would return the above Table class variables with the prefixes somehow
return CurrentDB1Prefix + Table.TableVariablex;
}
How can this be done? Or some approximation as usable as this?
Don't use static
and don't use const
. Make the values runtime-changable by converting the fields to properties. Like this:
public class Tables
{
public string CurrentPrefix = ...;
public string Employees { get { return CurrentPrefix + "DBEmploy1" };
//(...etc)
}