Refactoring DBObject
DBObject has come a long way recently and there is a lot of duplicated code. So I think now is a good time to refactor.
Let’s start with the most obvious items. There are only 11 methods in the DBObject, 7 of them take string connection, 4 take string index_column, and another 4 take string table. These values should almost always be the same for the entire life of the object. It would make secence to define these somewhere and just use them instead of having to constantly pass them.
But where should we define them? I can see 3 options. Using properties on the object that the child can set. Using SetProperty and having a special key for these items. Or forcing the child to implement methods that return the correct value.
I’m not a big fan of the third option. It just seems like too much work for the developer using the object. It would be the most flexible option, but I’m not so sure that we need that kind of flexibility. I like the second option, except it provides the possibility that the child could accidentally changes these values. If we say the table name is stored as _table_name_ then there is always the possibility that the developer could try to put something else in that value. This would cause strange bugs in the object. The only thing I don’t like with the first option is that it doesn’t use the SetProperty system. You have to define these values via some other system rather than the one used for everything else.
I think for now, These properties should use the current SetProperty system. This will keep things consistent. So in addition to the column_ properties, there will also be _table_name_, _index_column_, and _connection_string_. We can have the methods verify these properties exist before running any method that uses them.
First, let’s add some Properties to easly set/get the values
protected string TableName
{
get { return (string)this.GetProperty("_table_name_", string.Empty); }
set { this.SetProperty("_table_name_", value); }
}
protected string IndexColumn
{
get { return (string)this.GetProperty("_index_column_", string.Empty); }
set { this.SetProperty("_index_column_", value); }
}
protected string ConnectionString
{
get { return (string)this.GetProperty("_connection_string_", string.Empty); }
set { this.SetProperty("_connection_string_", value); }
}
Now let’s build an easy to use check, to verify these values exist.
protected bool canCallTable()
{
if (string.IsNullOrEmpty(this.TableName) || string.IsNullOrEmpty(this.IndexColumn) || string.IsNullOrEmpty(this.ConnectionString))
{
return false;
}
return true;
}
Now we just need to convert the methods to use these new properties and to call our check. If the check fails, throw an exception.
Add this as the first line in the methods that call the table and remove the parameters. Note: We can not do this to the two static methods, they still need the connection string passed to them.
if( !canCallTable() ) { throw new Exception("TableName, IndexColumn, or ConnectionString are not defined."); }
string table = this.TableName;
string index_column = this.IndexColumn;
string connection = this.ConnectionString;
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.
