I have created the following class, that works with a specific database table. How can I make this class a base class so that other classes can have the same properties and methods, but return the correct type and the only thing I have to do is to assign the correct tableName?
Thanks in advance, I hope my question is clear.
Here is the class:
public class AccountType
{
private static string tableName = "accountTypes";
private int id = -1;
public int Id
{
get
{
return id;
}
set
{
id = value;
}
}
private string name = "";
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
private static List<AccountType> accountTypes = new List<AccountType> ();
public static List<AccountType> AccountTypes
{
get
{
return accountTypes;
}
}
public AccountType ()
{
}
public AccountType Clone ()
{
AccountType o = (AccountType)this.MemberwiseClone ();
return o;
}
public static AccountType Fill (DataRow row)
{
int id = Convert.ToInt32 (row["id"].ToString ());
string name = row["name"].ToString ();
AccountType o = new AccountType ();
o.id = id;
o.name = name;
return o;
}
public static List<AccountType> FillAll (DataRowCollection rows)
{
List<AccountType> objs = new List<AccountType> ();
foreach (DataRow row in rows)
{
AccountType o = Fill (row);
if (o != null)
objs.Add (o);
}
return objs;
}
public static List<AccountType> GetAll ()
{
if (AccountType.accountTypes.Count > 0)
return AccountType.accountTypes;
List<AccountType> objs = new List<AccountType> ();
string query = "SELECT * \r\n" +
"FROM " + AccountType.tableName + " \r\n" +
"WHERE id > -1 \r\n" +
"ORDER BY name";
DataSet result = Global.Db.ExecuteQuery (query);
if (
(result == null)
|| (result.Tables[0] == null)
|| (result.Tables[0].Rows.Count < 1)
)
{
return objs;
}
objs = FillAll (result.Tables[0].Rows);
return objs;
}
public static AccountType GetById (int id)
{
foreach (AccountType at in AccountType.accountTypes)
{
if (at.id == id)
return at;
}
AccountType o = null;
string query = "SELECT * \r\n" +
"FROM " + AccountType.tableName + " \r\n" +
"WHERE id = " + id + " \r\n";
DataSet result = Global.Db.ExecuteQuery (query);
if (
(result == null)
|| (result.Tables[0] == null)
|| (result.Tables[0].Rows.Count < 1)
)
{
return o;
}
o = Fill (result.Tables[0].Rows[0]);
return o;
}
public static void Load ()
{
AccountType.accountTypes = AccountType.GetAll ();
}
public void Save ()
{
string tn = AccountType.tableName;
string query = "INSERT INTO " + tn + " (name) " +
"VALUES ( @name)";
SQLiteCommand command = new SQLiteCommand ();
command.CommandText = query;
command.CommandType = CommandType.Text;
command.Parameters.Add (new SQLiteParameter("@currencyPair", this.name));
Common.Global.Db.ExecuteNonQuery (command);
}
public void Update ()
{
string query = "UPDATE " + AccountType.tableName + " \r\n" +
"SET name = @name \r\n" +
"WHERE id = @id";
SQLiteCommand command = new SQLiteCommand ();
command.CommandText = query;
command.CommandType = CommandType.Text;
command.Parameters.Add (new SQLiteParameter("@id", this.id));
command.Parameters.Add (new SQLiteParameter("@name", this.name));
Common.Global.Db.ExecuteNonQuery (command);
}
}