473,513 Members | 2,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Options for replacing enums stored in database....

Hi,

I'm working on a project that historically contains around 40 enums. In the
database various fields refer to the int values of these enums, but of
course ref integrity is not enofrced and when looking at the db we can't
tell what the value in a field represents. The other problem is that our
enums are currently all stored in a single class, which means that because
of no visibility constraints the enums are often used out of context e.g. A
CarEngine enum being used instead of a BikeEngine enum.

What I'd like to do is:

1) store the enums in the database so that we can enforce ref integrity. I
don't want to have to create 40 odd tables to store these values either. The
actual int value of the enum is unimportant as we won't be performing enum
addition (like CarEngine.4Cylinder + CarEngine.Petrol).
2) dynamically create several classes within individual projects to allow us
to restrict visibility of the enums to within a project.

I have considered using three tables to store them - one for the class, one
for the enum type and one for the enum value and then (somehow) dynamically
building the class at compile time, but I have no idea how to do this!
Also, it does leave the problem of still not enforcing the ref integrity
100% i.e. the CarEngine field could store a BikeEngine enum value and still
enforce basic referential integrity as they're both enum values, albeit of
different types. Is it possible to enforce ref integrity in such a way to
prevent this?

The values held in the enum will not be changed between code releases and so
do not need to be truly dynamic. I would rather not have to hard code in
strings in our code to refer to the database.

Any clues about how to go about this or any better ideas / nice URLs to look
at?

Hope you're still following me!

Martin
Nov 17 '05 #1
4 5563
Hi,

Martin Pritchard wrote:
I'm working on a project that historically contains around 40 enums.
By this I presume you mean C# enum type.
In the database various fields refer to the int values of these enums, but
of course ref integrity is not enofrced
Two words -- "check constraints":

http://msdn.microsoft.com/library/en...es_04_77qr.asp

The basic check constraint validates values within a single row. Should you
ever require more elaborate validation logic (comparing to values from other
tables/rows, and so forth), you can even do that -- place it in a T-SQL user
defined function and refer to this function in the constraint.
when looking at the db we can't tell what the value in a field represents.
If that is a serious issue, store the enum value names as strings. It's ok.
Really. :) Slight (and usually insignificant) size/performance penalty
aside, the SQL database engine can do everything it does with strings just
as well as it can with ints. You can still use check constraints and you
can easily translate names to enums in your C# code with enum.Parse().

If it comes up rarely -- for example, every once in a while you are asked to
extract some sort of a one-time report where you could just run a SQL query,
but need the names -- put the int values and their corresponding names in a
table you can join to your query on those occasions.

The other problem is that our enums are currently all stored in a single
class, which means that because of no visibility constraints the enums are
often used out of context e.g. A CarEngine enum being used instead of a
BikeEngine enum.


Split them up. Also, adding proper check constraints to your database will
quickly put a stop to that.
--
Chris Priede
Nov 17 '05 #2
>> In the database various fields refer to the int values of these enums,
but of course ref integrity is not enofrced
Two words -- "check constraints":

I know all about check constraints, and to get them to do what I want it
would require hard coding data (the enum type) into the check constraint.
Not exactly best practice is it? Not to mention check constraints will be
slower than proper foreign keys.
If that is a serious issue, store the enum value names as strings. It's
ok. Really. :)

No it's not. Performance is a major issue here, and with the number of
records that need to refer to this, we need a properly indexed & optimised
solution. Strings will kill it. Not to mention still leaving the problem of
referential integrity not being truly enforced.

We're trying to implement a best practice solution under VS 2005 and looking
to go down the whole n-unit testing before check-in route, so that's why I
was wishing to build the classes at compile time.
Martin
Nov 17 '05 #3
Hi,

Martin Pritchard wrote:
Not exactly best practice is it? Not to mention check
constraints will be slower than proper foreign keys.
You keep referring to this problem as one of "referential integrity".
Referential integrity exists between tables. With regards to something that
only exists outside the database, such as enum values defined in application
code, I think the issue would be better characterized as one of data
validation. Thus the suggestion of check constraints.

Since you don't like check constraints, but love foreign keys, perhaps you
should consider the enum values to names table suggestion. If you do that,
you can make the values a primary key and treat all references to it
elsewhere in the database as foreign keys -- referential integrity
accomplished.
I was wishing to build the classes at compile time.


I am not presently aware of any way to do that.

You could do it at runtime, combining the name/value table suggestion along
with a class simulating an enum type, and loading the valid names and values
from the database (hopefully only once at startup). However, that seems
worth the trouble only if there is a need to add new values without
rebuilding the application -- and I think you said there isn't.

--
Chris Priede
Nov 17 '05 #4
Thanks for Chris's response.

Hi Martin,

I agree with Chris that we need to add constraints on this. When storing to
database, the constraints will be checked automatically. But it's not
recommended to put enums and values for all columns, as this will give a
hit on both performance and coding complexity.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #5

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

Similar topics

3
5277
by: Florian | last post by:
I need to set multiple values for some SQL statements, for example SET NUMERIC_ROUNDABORT OFF GO SET ANSI_PADDING,ANSI_WARNINGS,CONCAT_NULL_YIELDS_NULL,ARITHABORT,QUOTED_IDENTIF IER,ANSI_NULLS...
3
23483
by: Iqbal | last post by:
Hi, I am getting the following error when I run a stored procedure in which I am inserting/deleting data from a view that selects from a remote table. INSERT failed because the following SET...
2
1263
by: TheNortonZ | last post by:
We are using MSDE 2000 and ADO.Net for our database/datalayer in our client server based app. The application is not expected to have more than one user because the whole thing is installed on a...
2
1362
by: Mark Lees | last post by:
I split a database. Back end (be) is stored on a server. Front end (fe) is stored locally on one workstation. I will need to make changes to the forms and reports on the fe on a different...
7
2634
by: narshe | last post by:
I haven't come across an elegant way to do this yet, and would like to know what other people have come up with. There are two tables: CREATE TABLE EmployeeStatus ( pkId int not null primary...
1
1778
by: ward | last post by:
Good morning everyone. I'm building a very simple content management site that tracks "tasks." The options available are: 1. Add Task 2. Edit Task 3. View Task 4. Print Task
10
3823
by: Jay | last post by:
In C# I can set up an Enum so that number are represented as keywords, which is very useful. Is there such a datatype in a database? I suppose I could use an extra table, with the ID column as...
4
2988
by: Jon Slaughter | last post by:
is there a simple way to "step" through enums? I have a button that I want to click and have it "cycle" through a set of states defined by enums but the only way I can think of doing this...
3
1548
by: =?Utf-8?B?Sm9uYXRoYW4gU21pdGg=?= | last post by:
I have a class as follows: class GamesConsole { public int iReference; public enum Maker {Nintendo, Sega, Sony, Panasonic} }
0
7260
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,...
0
7384
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
7537
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...
0
7525
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...
0
5685
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,...
1
5086
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...
0
3233
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...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
456
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...

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.