Need to override static variable

330 Views Asked by At

I've created abstract class ORM with access to database and few basic methods. Some of there are static, such as:

abstract class ORM {
  public static string TABLENAME;
  public static Connect db_connection = new Connect();

  public static List<List<object>> where(query)
  {
    string query = "SELECT * FROM `" + TABLENAME + "` WHERE "  " " + query + ";";

    List<List<Object>> objects = new List<List<Object>>();
    if(ORM.connect.Open())
    {
        MySqlCommand command = new MySqlCommand(query, ORM.connect.connection);
        MySqlDataReader dataReader = command.ExecuteReader();

        int count = dataReader.FieldCount;
        while (dataReader.Read())
        {
            List<Object> args = new List<Object>();
            for (int i = 0; i < count; i++)
            {
                args.Add(dataReader.GetValue(i));
            }

            objects.Add(args);
        }                
    }

    ORM.connect.Close();
    return objects;
  }
        ....

}

So, i need to call this method from Derived Class

List<List<object>> current_user_instance = User.where("name LIKE 'Josh'");

And in Derived class I've created static constructor

static User()
{
  TABLENAME = "Users";
}

And it works perfectly. But in another case, when I call this method like

List<List<object>> current_user_instance = User.where("name LIKE 'Josh'");
List<List<object>> current_user_articles_instance = Article.where("user_id = 20"); 
// in my Article class static constructor is present too. 

the TABLENAME is not changing from "Users" to "Articles", because it is one class instance.

Please, help me to find alternatives to my resolution

0

There are 0 best solutions below