473,765 Members | 1,987 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 1802
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
2674
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 the id's separated with , for example....?
0
3340
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 Database Creation
1
4207
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 Ideas....thanks...
8
10036
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 dependencies be PIAs as well. Our component has a type library reference to MSXML 4, hence the...
0
1277
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 used for each employee (there are three different formulas for calculating vacation leave). */
25
2565
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 work out whether to INSERT or UPDATE it, I also want to be able to present the data from a QBF or...
10
7706
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 do this but I am interested to know what would be the advantages. And disadvantages, if any. Any...
12
12349
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
4134
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 change at a later date. From what i have read it seems that a layered approach is necessary, or at...
0
9568
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9399
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9955
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9833
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7378
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5421
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.