Using database config data in code

Posted on 2008-11-13
Last Modified: 2013-11-11
[.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:

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

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

Is there a way to accomplish this without the problems mentioned above?
Question by:brettr
    LVL 96

    Expert Comment

    by:Bob Learned
    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:

    TransactionTypeID, int
    TransactionTypeName, varchar(25)

    Author Comment

    Yes - I'd like to have intellisense.
    LVL 96

    Accepted Solution

    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.
    LVL 14

    Assisted Solution

    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.

    Author Comment

    Can you comment on how you use MyGeneration specifically?  I'm just starting to read up on it.
    LVL 14

    Expert Comment

    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

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    This video discusses moving either the default database or any database to a new volume.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now