473,807 Members | 2,853 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
CustomerInterfa ce.StoreLevel(C ustomerID, "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
CustomerInterfa ce.StoreLevel(C ustomerID, CustomerInterfa ce.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
"CustomerInterf ace" conts is used.
Third option, use an enum:
public enum Levels { Low, Medium, ... };
CustomerInterfa ce.StoreLevel(C ustomerID, 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(Lev els 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 "ParseFromC ode" methods?

Thanks,

Hans Kesting
Aug 8 '06 #1
5 1419
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.com wrote in message
news:mn******** *************** @spamgourmet.co m...
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
CustomerInterfa ce.StoreLevel(C ustomerID, "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
CustomerInterfa ce.StoreLevel(C ustomerID, CustomerInterfa ce.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 "CustomerInterf ace"
conts is used.
Third option, use an enum:
public enum Levels { Low, Medium, ... };
CustomerInterfa ce.StoreLevel(C ustomerID, 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(Lev els 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 "ParseFromC ode" 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.E O", I want a
"ServiceLevel.E xecuteOnly" 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
2278
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 that would have to be retried again. I looked through the source and didn't find anything, and looked through the gnu adns C source and didn't find anything, but I really don't know C at all, so I could have missed something. Thanks.
13
9558
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 change and lead to memory corruption. I didn't see how this was possible. He claims that if a dll built for a program is built with different compiler settings than the launcher program, the enum size could change. The only way I could figure...
2
2083
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 seem to find an appropriate Reflection method to access Enums within a class I would like to loop through the Enums in the 'Foo' Class retrieve the Enums 'Car' and 'House Public Class Fo Public Enum Ca For Chevrole Toyot
27
2748
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 <typename Enum> class smart_enum { public: typedef Enum enum_type;
3
2359
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 option. Creating a class library that will contain the return codes (which will be static strings). Calling the return codes from this library across different applications. Please let me know if you have implemented return codes in
4
5594
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 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...
2
2885
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, g++ seems to create enums of 4 bytes in length, unless I use the -fshort-enums option. I don't much want to use this all or nothing approach in case it breaks library code etc, and there's no guarantee that other compilers have a comparable...
11
12563
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, I can cast a MyEnum instance down to the int / short whatever (since int implements IEquatable<int>), but I don't like doing that, as it feels a bit messy, and I am then propegating the things that know what the base represenation is...
5
13502
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 will show the name not only the value) cons: enum can not be forward declared which makes all error codes
0
9720
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
10372
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
10374
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
10112
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...
0
9193
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5546
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
5685
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3854
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.