The way I see it, using the "new" keyword to create a database object should create a table in the database. So using the datareader in the constructor isn't an option because we are retrieving the object from the database, not adding one to it.
The static question is a bit tricker. I'm not 100% sure that it shouldn't be a a static method. It would eliminate a step. right now you have to do something like:
DBUser user = new DBUser();
string suery = user.SelectQuery + " WHERE id=?id";
Console.Write("Going to run: " + suery);
using (MySqlConnection conn = new MySqlConnection(Utility.connMy))
{
MySqlCommand cmd = new MySqlCommand(suery, conn);
cmd.Parameters.AddWithValue("?id", 1327);
try
{
conn.Open();
MySqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
user.FromReader(reader);
}
conn.Close();
}
catch (Exception ex)
{
Console.WriteLine("\n\nERROR:\n" + ex.Message);
}
}
Which means we are still calling new on the object (even though it's internally, it still violates the whole 'new should be to add a record' idea. If I changed .FromReader to be a static method, then we could simply do this:
DBUser user = null;
string suery = user.SelectQuery + " WHERE id=?id";
Console.Write("Going to run: " + suery);
using (MySqlConnection conn = new MySqlConnection(Utility.connMy))
{
MySqlCommand cmd = new MySqlCommand(suery, conn);
cmd.Parameters.AddWithValue("?id", 1327);
try
{
conn.Open();
MySqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
user = DBUser.FromReader(reader);
}
conn.Close();
}
catch (Exception ex)
{
Console.WriteLine("\n\nERROR:\n" + ex.Message);
}
}
It still takes the same number of lines of code, but it doesn't violate the "new keyword" rule.
I think I like the method being static. Mostly because it stays constant.