需要覆盖静态变量

时间:2014-11-26 23:45:43

标签: c# .net database orm derived

我创建了抽象类ORM,可以访问数据库和一些基本方法。有些是静态的,例如:

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;
  }
        ....

}

所以,我需要从Derived Class中调用这个方法

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

在Derived类中,我创建了静态构造函数

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

它完美无缺。但在另一种情况下,当我称这种方法为

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. 

TABLENAME未从"Users"更改为"Articles",因为它是一个类实例。

请帮助我找到我的决议的替代方案

0 个答案:

没有答案