473,396 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
Dec 6 '06 #1
5 2210
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" <rc***@westinis.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
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


Dec 6 '06 #2


"Ronald S. Cook" <rc***@westinis.comwrote in message
news:#N**************@TK2MSFTNGP06.phx.gbl...
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

Dec 6 '06 #3
"Ronald S. Cook" <rc***@westinis.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>
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
Dec 6 '06 #4
Since our ID is a GUID can we still do enum?

Thanks!
Ron

"David Browne" <davidbaxterbrowne no potted me**@hotmail.comwrote in
message news:%2****************@TK2MSFTNGP05.phx.gbl...
>

"Ronald S. Cook" <rc***@westinis.comwrote in message
news:#N**************@TK2MSFTNGP06.phx.gbl...
>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

Dec 6 '06 #5


"Ronald S. Cook" <rc***@westinis.comwrote in message
news:Oh**************@TK2MSFTNGP04.phx.gbl...
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


Dec 6 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: 3A Web Hosting | last post by:
Hi Folks This is probably starring me in the face but how do I read the contents of a hard drive directory? I've been playing around with the opendir($dir) sample from php.net and can read from...
1
by: Daniel | last post by:
when writing out a file from .net, when is the file created? after the bytes are all written to the hard drive or before the bytes are written to the hard drive?
36
by: Ron Johnson | last post by:
http://hardware.devchannel.org/hardwarechannel/03/10/20/1953249.shtml?tid=20&tid=38&tid=49 -- ----------------------------------------------------------------- Ron Johnson, Jr....
0
by: Bruce W...1 | last post by:
I installed a new hard drive in my PC, setup Windows, Visual Studio, etc.. The old hard drive is now on IDE 2, and I will eventually be using it for backup. On the old hard drive are a few...
18
by: Joe Lester | last post by:
This thread was renamed. It used to be: "shared_buffers Question". The old thread kind of died out. I'm hoping to get some more direction by rephrasing the problem, along with some extra...
16
by: Otie | last post by:
Hi, Is there a way for VB5 to determine exactly where on a hard drive a .exe file is stored upon the .exe file's first copying to the hard drive? What I need to know is the exact hard drive...
2
by: =?Utf-8?B?R2VvcmR5?= | last post by:
Hello everyone, I would really appreciate if someone helped me in this matter cause I am going to lose my mind... I am using a Sony Vaio Laptop with Windows XP professional (512MB Ram, 1,7 GHz CPU...
1
moishy
by: moishy | last post by:
Hi all! I use a internal hard drive as my external hard drive by using a IDE to USB wire. One day, I plug in the electric, but it won't work! I plug the electric into a different hard drive, it...
3
by: Kurt Mueller | last post by:
David, Am 07.10.2008 um 01:25 schrieb Blubaugh, David A.: As others mentioned before, python is not the right tool for "HARD REAL TIME". But: Maybe you can isolate the part of your...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.