473,581 Members | 2,819 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Hardcoding database primary key in switch statement / Design, Dependency issue

Hello,

I have a bit of a design issue around this application I am
developing, and I just want to run it through some of the brains out
here.

So I have a table called ErrorCheck which contains fields ErrorID (PK)
and ErrorName. There is another table called Client which contains
fields ClientID (PK) and ClientName. Then there is a relationship
table called ClientErrorChec k which contains foreign keys ErrorID and
ClientID.

The ErrorCheck table contains names and ids of checks that need to be
performed for clients.

In my code, based on the client that the user selects in a listbox, I
grab all the corresponding errors for that client and display in a
listbox (view only). The user then clicks a button which needs to
perform all those checks for the client.

The method that gets called when the user clicks that button has a
switch case statement which uses ErrorID to identify which method
needs to be called. Each ErrorCheck has a unique method containing the
logic for performing that check.

The problem is this: Even though the ErrorID is a primary key, shall
anyone ever change the ErrorID of a certain error in the ErrorCheck
table, my switch case statement may break or start pointing to the
wrong check methods. Even though the likelihood of this happening in
production is slim to none, I don't like this method because I am hard-
coding the ErrorIDs and their corresponding methods in my code, thus
causing a direct dependency between my application and the database.

So how can I solve this problem. Any ideas?

Regards,

Asad

Jul 13 '07 #1
6 1792
Is there any way that you can reflect the methodology associated with an
error in the database, and eliminate the problem all together?

For example, you have specific code based on the error id. What I would
do is abstract out the operation into an abstract class or interface, and
then have a field on the errors table which has the name of the type that
extends the abstract class or implements the interface. This way, if the
ids change, it doesn't matter to you, as your code would just get the
implementation of the algorithm from the error record itself.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

<as*******@gmai l.comwrote in message
news:11******** **************@ 22g2000hsm.goog legroups.com...
Hello,

I have a bit of a design issue around this application I am
developing, and I just want to run it through some of the brains out
here.

So I have a table called ErrorCheck which contains fields ErrorID (PK)
and ErrorName. There is another table called Client which contains
fields ClientID (PK) and ClientName. Then there is a relationship
table called ClientErrorChec k which contains foreign keys ErrorID and
ClientID.

The ErrorCheck table contains names and ids of checks that need to be
performed for clients.

In my code, based on the client that the user selects in a listbox, I
grab all the corresponding errors for that client and display in a
listbox (view only). The user then clicks a button which needs to
perform all those checks for the client.

The method that gets called when the user clicks that button has a
switch case statement which uses ErrorID to identify which method
needs to be called. Each ErrorCheck has a unique method containing the
logic for performing that check.

The problem is this: Even though the ErrorID is a primary key, shall
anyone ever change the ErrorID of a certain error in the ErrorCheck
table, my switch case statement may break or start pointing to the
wrong check methods. Even though the likelihood of this happening in
production is slim to none, I don't like this method because I am hard-
coding the ErrorIDs and their corresponding methods in my code, thus
causing a direct dependency between my application and the database.

So how can I solve this problem. Any ideas?

Regards,

Asad

Jul 13 '07 #2
On Jul 13, 11:39 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guar d.caspershouse. comwrote:
Is there any way that you can reflect the methodology associated with an
error in the database, and eliminate the problem all together?

For example, you have specific code based on the error id. What I would
do is abstract out the operation into an abstract class or interface, and
then have a field on the errors table which has the name of the type that
extends the abstract class or implements the interface. This way, if the
ids change, it doesn't matter to you, as your code would just get the
implementation of the algorithm from the error record itself.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard .caspershouse.c om

<asadik...@gmai l.comwrote in message

news:11******** **************@ 22g2000hsm.goog legroups.com...
Hello,
I have a bit of a design issue around this application I am
developing, and I just want to run it through some of the brains out
here.
So I have a table called ErrorCheck which contains fields ErrorID (PK)
and ErrorName. There is another table called Client which contains
fields ClientID (PK) and ClientName. Then there is a relationship
table called ClientErrorChec k which contains foreign keys ErrorID and
ClientID.
The ErrorCheck table contains names and ids of checks that need to be
performed for clients.
In my code, based on the client that the user selects in a listbox, I
grab all the corresponding errors for that client and display in a
listbox (view only). The user then clicks a button which needs to
perform all those checks for the client.
The method that gets called when the user clicks that button has a
switch case statement which uses ErrorID to identify which method
needs to be called. Each ErrorCheck has a unique method containing the
logic for performing that check.
The problem is this: Even though the ErrorID is a primary key, shall
anyone ever change the ErrorID of a certain error in the ErrorCheck
table, my switch case statement may break or start pointing to the
wrong check methods. Even though the likelihood of this happening in
production is slim to none, I don't like this method because I am hard-
coding the ErrorIDs and their corresponding methods in my code, thus
causing a direct dependency between my application and the database.
So how can I solve this problem. Any ideas?
Regards,
Asad
Would you mind to elaborate on that a little. I read what you said a
few times, but I'm not clear on it. Can you give me some example code?
Nothing big, just makeup small classes with comments should do? Really
appreciate it.

Thanks.

Jul 13 '07 #3
So you have this switch statement:

switch (id)
{
case 1:
// Do something
case 2:
// Do something else
}

Where id is the id in the database. The errors table looks like this:

Id Error Other Field
-- ----- -----------
1 ERR1 Some Info
2 ERR2 Other Info

What I am suggesting is creating another field, ErrorHandlerTyp e (or
something like that):

Id Error Other Field ErrorHandlerTyp e
-- ----- ----------- ----------------
1 ERR1 Some Info MyCompany.Produ ctNamespace.Err or1Handler
2 ERR2 Other Info MyCompany.Produ ctNamespace.Err or2Handler

Then defining an interface like this:

public interface IErrorHandler
{
void HandleError();
}

You then define classes which look like this:

namespace MyCompany.Produ ctNamespace
{
public class Error1Handler : IErrorHandler
{
public void HandleError()
{
// Do the error handling for error 1 here.
}
}

public class Error2Handler : IErrorHandler
{
public void HandleError()
{
// Do the error handling for error 2 here.
}
}
}

Then, in your code, instead of just getting the id from the database,
you would also get the ErrorHandlerTyp e from the database. Once you have
that string, you can create a Type instance from that, and pass it to the
static CreateInstance method on the Activator class. It will create a type.
You then cast that return value to the IErrorHandler interface, and call the
HandleError method.

Of course, your situation might require more properties/methods on the
interface, as you will have to initialize with other information, most
likely, but that's pretty much it.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

<as*******@gmai l.comwrote in message
news:11******** **************@ r34g2000hsd.goo glegroups.com.. .
On Jul 13, 11:39 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guar d.caspershouse. comwrote:
> Is there any way that you can reflect the methodology associated with
an
error in the database, and eliminate the problem all together?

For example, you have specific code based on the error id. What I
would
do is abstract out the operation into an abstract class or interface, and
then have a field on the errors table which has the name of the type that
extends the abstract class or implements the interface. This way, if the
ids change, it doesn't matter to you, as your code would just get the
implementati on of the algorithm from the error record itself.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard .caspershouse.c om

<asadik...@gma il.comwrote in message

news:11******* *************** @22g2000hsm.goo glegroups.com.. .
Hello,
I have a bit of a design issue around this application I am
developing, and I just want to run it through some of the brains out
here.
So I have a table called ErrorCheck which contains fields ErrorID (PK)
and ErrorName. There is another table called Client which contains
fields ClientID (PK) and ClientName. Then there is a relationship
table called ClientErrorChec k which contains foreign keys ErrorID and
ClientID.
The ErrorCheck table contains names and ids of checks that need to be
performed for clients.
In my code, based on the client that the user selects in a listbox, I
grab all the corresponding errors for that client and display in a
listbox (view only). The user then clicks a button which needs to
perform all those checks for the client.
The method that gets called when the user clicks that button has a
switch case statement which uses ErrorID to identify which method
needs to be called. Each ErrorCheck has a unique method containing the
logic for performing that check.
The problem is this: Even though the ErrorID is a primary key, shall
anyone ever change the ErrorID of a certain error in the ErrorCheck
table, my switch case statement may break or start pointing to the
wrong check methods. Even though the likelihood of this happening in
production is slim to none, I don't like this method because I am hard-
coding the ErrorIDs and their corresponding methods in my code, thus
causing a direct dependency between my application and the database.
So how can I solve this problem. Any ideas?
Regards,
Asad

Would you mind to elaborate on that a little. I read what you said a
few times, but I'm not clear on it. Can you give me some example code?
Nothing big, just makeup small classes with comments should do? Really
appreciate it.

Thanks.

Jul 13 '07 #4
>The method that gets called when the user clicks that button has a
switch case statement which uses ErrorID to identify which method
needs to be called. Each ErrorCheck has a unique method containing the
logic for performing that check.


Typically what I would suggest is adding a new field like ErrorRef
which is a C#-keyword appropriate name and would be considered
internal to your system (whereas ErrorName can change over time if
wording needs to be different and can have invalid characters,
ErrorRef would never change and would be a valid C# keyword).

Then create a enum in C# where the names are the ErrorRef values and
the values of the enum are the ID's. Ideally the enum can be
generated directly from the database and you would have a unit test or
even a runtime test to confirm that the values all match.

Then youc an program against the enum which is safer than using the ID
(especially if you have tests to confirm matching values in db and
code).

HTH,

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
On Fri, 13 Jul 2007 08:30:42 -0700, as*******@gmail .com wrote:
>Hello,

I have a bit of a design issue around this application I am
developing, and I just want to run it through some of the brains out
here.
....
>
Regards,

Asad
Jul 13 '07 #5
I like Nicholas's idea. However, a simpler approach is the one we use, where
tables like ErrorCheck have a Moniker column with a check constraint and a
uniqueness constraint. We refer to rows via the moniker, not the ID.

///ark
Jul 13 '07 #6
Ok thanks guys. That's exactly the kind of ideas I was looking for.
Some of this stuff went right over my head, but now I'm gonna play
around with it until I understand. Thanks again.

Regards,

Asad

Jul 13 '07 #7

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

Similar topics

4
2663
by: Christopher Brandsdal | last post by:
Hi! I have a delicatg problem.... I have made a registration form for adding my friends information in a database. The problem is that I want to connect the persons with companies in the same database. I wonder how i could connect a person to several other id's in a table. I wonder if I can make a string in the database that holds all...
0
3320
by: Cherrish Vaidiyan | last post by:
sir, The following are the steps that i followed in setting up standby database on Red hat Linux 9. i am using Oracle 9i. i have followed the steps in this site : http://download-west.oracle.com/docs/cd/B10501_01/server.920/a96653/create_ps.htm#66206 Still i give the steps i followed. Preparing the Primary Database for Standby...
1
4190
by: David Thompson | last post by:
Looking for a book to help me develop a philosophy for building databases (particularly on MySQL). And then taking them from concept to construction. Something like.... Start by asking which queries you will be performing, then define all data needed for each of those queries, then progress to normalize this data, etc. Any...
8
10009
by: Chango V. | last post by:
Hello, We seem to need a PIA for MSXML 4. According to the documentation, only the original publisher of a COM type library is supposed to create a .NET PIA. As far as I know, Microsoft hasn't done that yet. Our problem is that we very much want to create a PIA for a COM component of ours, but TlbImp.exe /primary insists that all referenced...
0
1263
by: btober | last post by:
/* I have a customer requirement to provide for flexibility on how vacation leave is calculated and am having trouble with executing the necessary SQL which is stored in the database as a text column. The employee table and a newly minted vacation leave formula table, shown below, are related and identify what vacation leave calculation is...
25
2546
by: Colin McKinnon | last post by:
Hi all, There's lots of DB abstraction layers out there, but a quick look around them hasn't turned up anything which seems to met my requirements. Before I go off and write one I thought I'd ask here if anyone knows of such a beast... I want some code where I present an array of data, and the corresponding primary key and let the code...
10
7687
by: Jim Devenish | last post by:
I have a split front end/back end system. However I create a number of local tables to carry out certain operations. There is a tendency for the front end to bloat so I have set 'compact on close' I think that I have read in some threads (althoug I cannot find them now) that others place such tables in a local, linked database. I could...
12
12297
by: | last post by:
Is it fine to call another method from Switch? Eg. Switch (stringVar) { case ("a"): somVar = "whatever"; Another_Method(); //call another method return;
8
4122
by: morleyc | last post by:
Hi, until recently i was quite happy to add data sources from mssql database in visual studio and drag the datasets directly onto the form this creating a directly editable form which worked well. However i have recently started a project which will require synchronization to a remote database. Also the underlying database provider may...
0
7860
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7788
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8139
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7890
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8166
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6546
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3813
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1398
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1127
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.