• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 174
  • 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

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