Ok to hard-code reference to database ID value in app code?

  • Thread starter Thread starter Ronald S. Cook
  • Start date Start date
R

Ronald S. Cook

Hi,

I have a class "User" in my application's business tier, and an associated
table "User" in my database. A user has a type. Currently they are:

ID Name
1 Regular User
2 Power User
3 Executive User
Etc...

In my current model, I have a table "UserType" for the above and a foreign
key reference in my table "User".

QUESTION: In my app, is it cool to be having code that says like "if
(objUser.UserType == 2)...?

For one thing, I'm not crazy about business logic that refences a tables ID
value (but maybe that's ok?). Also, we're actually using GUIDs so that line
would be uglier as the ID value would be that much longer.

Any thoughts?

Thanks,
Ron
 
Problems arise if you hard code the value in multiple places (maybe not
now... but you may do in the future - or worse still, someone else might).
Then when something changes it has to be changed everywhere it occurs: and
the chances are you don't know where every occurrence is. So don't do it,
basically :)

A solution is to define an enumeration. You then use the enumerated values
everywhere you need them. The advantage of this is that if things change,
they only change in one place.

This is just off the top of my head. Someone else may have a much better
solution.


Peter
 
Ronald S. Cook said:
Hi,

I have a class "User" in my application's business tier, and an associated
table "User" in my database. A user has a type. Currently they are:

ID Name
1 Regular User
2 Power User
3 Executive User
Etc...

In my current model, I have a table "UserType" for the above and a foreign
key reference in my table "User".

QUESTION: In my app, is it cool to be having code that says like "if
(objUser.UserType == 2)...?

For one thing, I'm not crazy about business logic that refences a tables
ID value (but maybe that's ok?). Also, we're actually using GUIDs so that
line would be uglier as the ID value would be that much longer.

I would write (or codegen) an enumerated class for UserType. Like this:

public class UserType
{
string name;
public string Name
{
get { return name; }
}

int id;
public int Id
{
get { return id; }
}

private UserType(string name, int id)
{
this.name = name;
this.id = id;
}

public static readonly UserType RegularUser = new UserType("Regular
User",1);
public static readonly UserType PowerUser = new UserType("Power User",2);
public static readonly UserType ExecutiveUser = new UserType("Executive
User",3);

}

Some codegen tools have built-in support for doing this, you can just type
it in, or spit it out with a query like

select 'public static readonly UserType RegularUser = new UserType("' + Name
+ '",' + ID + ');'
from UserTypes


David
 
Ronald S. Cook said:
QUESTION: In my app, is it cool to be having code that says like "if
(objUser.UserType == 2)...?

We use enums for this purpose, which are tied to a database table containing
the same values and names. We have automated tests that compare the enums to
the tables to make sure they're in sync.

///ark
 
Ronald S. Cook said:
Since our ID is a GUID can we still do enum?

These aren't .NET enums. It's just a pattern for writing a class whose
instances are all enumerated and accessible through static variables. So
yes, you can do this, just replace int with Guid, and change the constructor
appropriately.

David
 
Back
Top