Setting values automatically

  • Thread starter Thread starter Maziar Aflatoun
  • Start date Start date
M

Maziar Aflatoun

Hi,

I have the lKey object that can be used to enable different operation
systems such
Windows 2000
Linux
FreeBSD
etc.

Now I want to read a database table that contains key/values and if the key
is "OSTypes", I like to set the appropraite OS (more than one OS can be
set).

lKey.Enable(Company.OSTypes.Redhat);
lKey.Enable(Company.OSTypes.Windows);
....

I really don't want to use IF and SWITCH for every possible value. Is it
possible to set the ??? in lKey.Enable(Company.OSTypes.????) to whatever
value read from the database?

Thank you
Maz.
 
I can think of two ways to do this. The wrong way and the right way.
;-)

You could use Reflection to search for a name in the OSTypes
enumeration that matches the string in your database table. This is
what I call the "wrong way" because it ties your type names to your
database contents. If you change your database representation, or
rename an enum, your program will break. (Imagine, for example, that
Redhat is bought out by Microsoft and renamed Microhat. How are you
going to change your program enum if it's locked into your database
contents?)

You could also take a much more low-tech approach, which for me is the
"right way": simply create a static read-only hash table that maps the
database string (the key) to the matching enum value (the value). Then
you can just do this:

string os = (string)dbTable["OS_TYPE"];
IKey.Enable((Company.OSTypes)OsTypeMapping[os]);

For future expansion purposes, you could have your hash table store a
class that contain the O/S type and a flag indicating whether this
mapping is the preferred mapping for going backward (from
Company.OSTypes to the database string):

internal class OSMapping
{
private static map = null;

Company.OSTypes _osType;
bool _preferredMapping;

private OSMapping(Company.OSTypes osType, bool preferredMapping)
{
this._osType = osType;
this._preferredMapping = preferredMapping;
}

static OSMapping()
{
OSMapping.map["Redhat"] = new OSMapping(Company.OSTypes.Redhat,
true);
OSMapping.map["Windows"] = new
OSMapping(Company.OSTypes.Windows, true);
...
}

public static Company.OSType GetOSType(string databaseOSType)
{
OSMapping mapping = (OSMapping)OSMapping.map[databaseOSType];
if (mapping == null)
{
return Company.OSTypes.None;
}
else
{
return mapping._osType;
}
}

public static string GetDatabaseOSType(Company.OSTypes os)
{
string result = null;
foreach (DictionaryEntry de in OSMapping.map)
{
OSMapping mapping = (OSMapping)de.Value;
if (mapping._osType == os)
{
if (mapping._preferredMapping)
{
result = (string)de.Key;
}
else if (result == null)
{
result = (string)de.Key;
}
}
}
return result;
}
}

Then you can just say:

string os = (string)dbTable["OS_TYPE"];
IKey.Enable(OSMapping.GetOSType(os));

or

dbTable["OS_TYPE"] =
OSMapping.GetDatabaseOSType(Company.OSTypes.Redhat);

While this may seem like a lot of code, the advantage is that the
values stored in your database are not in any way tied to the names of
the enum types. Plus, if you use the "preferredMapping" flag, you can
even _change_ some mappings, giving new database names to existing
O/S's, while leaving intact the ability to read the old names that are
already recorded in the databse.
 
Back
Top