• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 187
  • Last Modified:

Using database config data in code

[.NET 3.5]
In have several configuration tables in a db and map them via an enum in my .NET app.  The enum may look like this:

public enum TransactionTypes{
start = 1,
end = 2,
complete = 3}

where each number corresponds to an ID column in a corresponding table.   This approach is error prone and not very scalable.  A change in the ID column value can disassociate the enum mapping.  You also have to remember to add any new IDs.   The enum is nice though since I can get great intellisense in code:

//implementation
public static void SomeMethod(TransactionTypes trans){...}

//caller
MyClass.SomeMethod(TransactionTypes.{all enums display})

Is there a way to accomplish this without the problems mentioned above?
0
brettr
Asked:
brettr
  • 2
  • 2
  • 2
2 Solutions
 
Bob LearnedCommented:
Do you want (or need) Intellisense mapping?  The approach that I usually take is to have a TransactionType lookup table that you can access with an SQL inner join.

SQL Server example:

TransactionType
---------------------
TransactionTypeID, int
TransactionTypeName, varchar(25)
0
 
brettrAuthor Commented:
Yes - I'd like to have intellisense.
0
 
Bob LearnedCommented:
Do you know anything about code generation, like CodeSmith?  There aren't a lot of options when you require Intellisense, and you want to avoid errors and sins of omission.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
PockyMasterCommented:
Well,you can use the enum even if the integer values do not exist in your enum class.
It only becomes an issue if you use them by name.
E.g if you require to use TransactionTypes.start anywhere in your code, it must exist.

But if you need to use an undefined value, e.g. TransactionTypes.failed you will notice it during development that your value is missing.
Code generation tools might help, like TheLearnedOne said, I personally liked MyGeneration since it's free.
Maybe you need to wonder why ppl add lookup values to the database if the business logic does not use/expose them.
0
 
brettrAuthor Commented:
@PockyMaster:
Can you comment on how you use MyGeneration specifically?  I'm just starting to read up on it.
0
 
PockyMasterCommented:
It's merely based on database driven code generation. It has templates included when you download it, but it's easy to write your own using VB script. The help is self explaining, or based on the included templates it's easy to create your own
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now