开发者

Automatically create properties - Out of a database table

I already googled around to find a solution开发者_JAVA技巧 for my need, with no success.

Let's say I've a table that looks like this:

ID |KeyId |Name |Description

1 |153 |Currency |XXXXXXXX

2 |68 |Signature |YYYYYYYY

3 |983 |Contact |ZZZZZZZZ .

Now I want to access theses values not by a collection, because I cannot remember all the values, let's say for the name.

So this is not what I want: Values.Where(v => v.Name == "Currency").Select(v => v.KeyId);

The table content changes rarely but still it is not a nice solution having a struct with all "Names" and getting the KeyId like this.

struct Values
{
    public static int Currency
    {
        get { return GetKeyId("Currency"); }
    }
}

I'm looking for a solution that creates me automatically properties out of this table. So that I can access the KeyId with intellisense. As you have for Resources in ASP.NET. There the class is automatically updated as soon as you add a new entry in the RESX file.

For example: Values.Currency , this gives me back the corresponding KeyId.

Thanks for reply


I believe this feature has been added in C# 4.0 (not yet released), announced at the Microsoft PDC. However, even that solution may not give you Intellisense for anything you haven't already typed explicitly (because if you think about it, it would be impossible for Intellisense to help you type what values might be added in the future).

Until then, the standard practice is to use a Dictionary and/or Enum (but the Dictionary will require you typing a string, and the Enum won't update automatically, or both using an Enum as the key to the dictionary to get intellisence help).


I found the perfect solution for me => T4 Templates

With this feature I can iterate through my table and create properties for each row, pritty nice.

There you go:
Example how to work with T4
T4 Editor - Free
Infos to T4 Editor

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜