DBObject rev .02

Ok, here is the code with the static modifications.
I like this better, but I don’t like the whole “(DBUser)DBObject.FromReader(reader, typeof(DBUser));” (or “(DBUser)DBUser.FromReader(reader, typeof(DBUser));”) thing. It just seems like I’m having to type to much. Then again, the child class could encapsulate some of this passing by type.

DBObject

   ///
   /// This is the baseclass for DB Object created by Chris Richards   /// 

   [Serializable]   public class DBObject   {       //Holds all the properties in the object       protected Hashtable _properties = new Hashtable();

       protected DBObject() { }

       ///
       /// This will populate all of the properties from the table by the column 'id'       /// 

       ///       ///        public object ByID(int id)       {           return new DBObject();       }

       ///
       /// Gets the SELECT ... FROM query for this object       /// 

       static protected string SelectQuery(Type who)       {           //Generate the Select Query           string _select = "SELECT";

           //Now Set all the select columns           foreach (PropertyInfo properity in who.GetProperties())           {               foreach (object attribute in properity.GetCustomAttributes(false))               {                   if (attribute is DBColumn)                   {                       _select += " " + ((DBColumn)attribute).Column + ",";                   }               }           }

           //Remove the last Comma           _select = _select.Substring(0, _select.Length - 1);

           //Get the Table Name for this Object           foreach (object attr in who.GetCustomAttributes(false))           {               if (attr is DBTable)               {                   //Add the Table to our List                   _select += " FROM " + ((DBTable)attr).Table;               }           }           //Now Return it           return _select;       }

       ///
       /// This will populate the object from the reader.       /// It will not advance the reader.       /// 

       ///       ///        static protected DBObject FromReader(MySqlDataReader reader, Type who_type)       {           DBObject who = (DBObject)Activator.CreateInstance(who_type, true);           //Look for all the properities           foreach (PropertyInfo properity in who_type.GetProperties())           {               if (properity.CanWrite)               {                   object[] attributes = properity.GetCustomAttributes(false);                   foreach (object attribute in attributes)                   {                       if (attribute is DBColumn)                       {                           try                           {                               properity.SetValue(who, reader[((DBColumn)attribute).Column], null);                           }                           catch (IndexOutOfRangeException)                           {                               //Just Skip it                           }                           catch (Exception ex)                           {                               //ErrorLog.Log(-1, this.GetType().Name + " reader Error", ex.Message);                               string junk = ex.Message;                           }                       }                   }               }               else               {                   ErrorLog.Log(-1, "Can't Write " + who.GetType().Name + "'s Properity", "Properity: " + properity.Name);               }           }           return who;       }   }

Test Code

And here is the test example

            string suery = DBObject.SelectQuery(typeof(DBUser)) + " 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())                    {                        DBUser user = (DBUser)DBObject.FromReader(reader, typeof(DBUser));                        Console.WriteLine("nGot: " + user.FirstName);                    }

                    conn.Close();                }                catch (Exception ex)                {                    Console.WriteLine("nnERROR:n" + ex.Message);                }            }
Share

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CommentLuv Enabled