473,406 Members | 2,217 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,406 software developers and data experts.

Magic codes, enums, or ...?

Hi,

In a database some code is stored as a string. This code can have a
limited (known) set of values. What is the best way to use this in my
(program-)code?

I don't want to use "magic codes" like
CustomerInterface.StoreLevel(CustomerID, "BEH");
because this would give no indication what sort of string was expected
and what the accepted values were.
I could declare a const:
public const string Level1 = "BEH";
and use it like
CustomerInterface.StoreLevel(CustomerID, CustomerInterface.Level1);

This way the magic code is somewhat under control, but still there
would be no way to prevent someone from using an unknown string like
"XYZ". The compiler would not complain, but a runtime error would
(probably) follow. There would be no way to guarantee that one of the
"CustomerInterface" conts is used.
Third option, use an enum:
public enum Levels { Low, Medium, ... };
CustomerInterface.StoreLevel(CustomerID, Levels.Level1);

This way the compiler would make sure I really use a "Levels" value
(OK, I could use '(Levels)6' but that would be cheating).
The downside is that somewhere I would have to translate from this
Levels enum to the string code because that database only accepts a
string, and that can't be changed. So I use a separate class:
class EnumConverter
{
public static string LevelToCode(Levels theLevel)
{
switch (theLevel)
{
case Levels.Low: return "BEH";
case Levels.Medium: return "EO";
...
}
}
}
But this seems to me more like Oh-Oh programming than the OO version.

Does anyone know a better way where I can specify the "level" in a
typesafe way and encapsulate the "ToCode" and "ParseFromCode" methods?

Thanks,

Hans Kesting
Aug 8 '06 #1
5 1403
The downside is that somewhere I would have to translate from this Levels
enum to the string code because that database only accepts a string, and
that can't be changed.
{snip}
But this seems to me more like Oh-Oh programming than the OO version.
This still seems the right option to me; the natural place for a finite,
fixed list of
options in the
CLR is an enum; how the database stores it is the database's problem. When
transitioning between universes (OO <--DB), there is always some level
of translation required; sometimes this can be with casting or
..Parse()/.ToString(),
and sometimes it can't. You just need to put this translation code alongside
this code that deals with the sp-name, or SQL command, or whatever (your
adapter, DAL, or where-ever else that code is living).

At the end of the day, your data entities are there to model the system; NOT
to model the database... the database exists purely for long-term storage.
So
let your OO-code use the best that the OO-world has to offer, and let your
SQL code do likewise with SQL.

Marc
Aug 8 '06 #2
>The downside is that somewhere I would have to translate from this Levels
>enum to the string code because that database only accepts a string, and
that can't be changed.
{snip}
But this seems to me more like Oh-Oh programming than the OO version.

This still seems the right option to me; the natural place for a finite,
fixed list of
options in the
CLR is an enum; how the database stores it is the database's problem. When
transitioning between universes (OO <--DB), there is always some level
of translation required; sometimes this can be with casting or
.Parse()/.ToString(),
and sometimes it can't. You just need to put this translation code alongside
this code that deals with the sp-name, or SQL command, or whatever (your
adapter, DAL, or where-ever else that code is living).

Marc
Still it would be nice if I could add the "ToCode()" method to the
enum. (C# 1.1)

Thanks for the reply,

Hans Kesting
Aug 8 '06 #3
In some ways yes, but it depends on how "pure" you are at modelling your
objects. Some people stick strictly to the "one class, one purpose"
paradigm; the job of your entities is to represent the data, not to talk to
a database. That is sometimes the role of a separate adapter / DAL
construct. With this view, the object shouldn't really even need to know
that there *is* a database - or indeed, the object could sit above 5
different database implementations with different schemas and
representations of the magic codes, with a different adapter for each. You'd
then need a parameter on the ToCode() method, and all of a sudden your
business objects are tightly coupled to the database again...

But I see your point; Jon Skeet has some interesting thoughts on other ways
to improve enums along Java lines; I suspect what you are talking about
would fit somewhere on this page...

http://msmvps.com/blogs/jon.skeet/ar...classenum.aspx

Marc
Aug 8 '06 #4
public enum stringEnum
{
foo = 0,
bar = 1
}

string s = stringEnum.foo.ToString(); // yields "foo"

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Expect the unaccepted.

"Hans Kesting" <ne***********@spamgourmet.comwrote in message
news:mn***********************@spamgourmet.com...
Hi,

In a database some code is stored as a string. This code can have a
limited (known) set of values. What is the best way to use this in my
(program-)code?

I don't want to use "magic codes" like
CustomerInterface.StoreLevel(CustomerID, "BEH");
because this would give no indication what sort of string was expected and
what the accepted values were.
I could declare a const:
public const string Level1 = "BEH";
and use it like
CustomerInterface.StoreLevel(CustomerID, CustomerInterface.Level1);

This way the magic code is somewhat under control, but still there would
be no way to prevent someone from using an unknown string like "XYZ". The
compiler would not complain, but a runtime error would (probably) follow.
There would be no way to guarantee that one of the "CustomerInterface"
conts is used.
Third option, use an enum:
public enum Levels { Low, Medium, ... };
CustomerInterface.StoreLevel(CustomerID, Levels.Level1);

This way the compiler would make sure I really use a "Levels" value (OK, I
could use '(Levels)6' but that would be cheating).
The downside is that somewhere I would have to translate from this Levels
enum to the string code because that database only accepts a string, and
that can't be changed. So I use a separate class:
class EnumConverter
{
public static string LevelToCode(Levels theLevel)
{
switch (theLevel)
{
case Levels.Low: return "BEH";
case Levels.Medium: return "EO";
...
}
}
}
But this seems to me more like Oh-Oh programming than the OO version.

Does anyone know a better way where I can specify the "level" in a
typesafe way and encapsulate the "ToCode" and "ParseFromCode" methods?

Thanks,

Hans Kesting


Aug 8 '06 #5
public enum stringEnum
{
foo = 0,
bar = 1
}

string s = stringEnum.foo.ToString(); // yields "foo"

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery
Yes, but the code is *really* a "code", that is: short, not really
descriptive. I don't want a "ServiceLevel.EO", I want a
"ServiceLevel.ExecuteOnly" that translates to "EO".
(I know that it is possible to add IntelliSense comments so that the
description for EO is "Execute Only" but I don't like to *depend* on
that to make sense of my code)
Hans Kesting
Aug 8 '06 #6

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

Similar topics

2
by: Josh Close | last post by:
Does anyone know what the return codes for an mx adns python lookup are? I know 0 means a valid domain, and anything else isn't, but there are "no nameservers found" and "timeout" and other things...
13
by: SpaceCowboy | last post by:
I recently got into a discussion with a co-worker about using enums across a dll interface. He wanted to use chars instead, argueing that depending on compiler settings the size of an enum could...
2
by: Faisal | last post by:
Can anyone tell me if it is possible to enumerate through all the Enums within a class . I have a class with many Enums and would like to accees the Enums through an array/collection etc. I can't...
27
by: Mark A. Gibbs | last post by:
i have been toying with the idea of making my enums smarter - ie, more in line with the rest of the language. i haven't tested it yet, but what i came up with is a template like this: template...
3
by: c# beginner | last post by:
we are trying to standardize return codes across our .NET applications (that are soon to be developed.) What is the best practice for standardizing return codes? I know of only the following...
4
by: Martin Pritchard | last post by:
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...
2
by: Simon Elliott | last post by:
I have some legacy C++ code which requires some enums to be 1 or 2 bytes in size. I'd ideally like to be able to specify that a few carefully selected enums are a particular size. By default,...
11
by: Marc Gravell | last post by:
This one stumped me while refactoring some code to use generics... Suppose I declare an enum MyEnum {...} Is there a good reason why MyEnum doesn't implement IEquatable<MyEnum> ? Of course,...
5
by: =?GB2312?B?17/HvyBaaHVvLCBRaWFuZw==?= | last post by:
Hi, I would like to have someone comments on what's the best practice defining error codes in C. Here's what I think: solution A: using enum pros: type safe. better for debug (some debugger...
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
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
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
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
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,...
0
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...

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.