How would you link to search / metadata?

I encountered this problem quite a few times and never liked the solution I chose. Say you have a list of states (as a simple example) in a database. In the code of the code, you want to be able to refer to the state by identifier and have a list of them available through Intellisense.

For instance:

States.Arizona.Id  //returns a GUID

But the problem is that I do not want to hard code GUIDS. In the past, I have done all of the following:

  • Creating class constants (worst-case hard coding .. ugh!)

  • Create search classes that have an ID property (among others) (still hard-coded and will need to update the project if it has ever been updated)

  • Put all the GUIDs in the .config file, create an enumeration and inside the static constructor load the GUIDS from the .config into the Hashtable with the enumeration element as the key. Then I can do: StateHash[StatEnum.Arizona]. Pleasant, because if the GUID changes, re-creation is not required. However, it does not help if a new record is added or the old one is deleted, because the enumeration will need to be updated.

So I ask if anyone has a better solution? Ideally, I want to be able to search through Intellisense and do not need to rebuild the code when updating. Not even sure if this is possible.

EDIT: Using states was just an example (perhaps a bad one). This may be a list of widgets, types of cars, etc., if that helps.

+3
source share
3 answers

, Intellisense, .

, , , , , , , . , FOO.cs, . , .

0

, , . , - (,.ApplyDoubleTax - ). intellisense - , , .

.

, - - - , , ...

... , ?

+1

This screams for the MSBuild custom task. In this case, you really want to create an auto-generated enumeration or class; if the identifiers are obtained from the database and can / will change, and they are easy to predict. Then you can put the task into your project, and it will be performed before each update of the assembly as necessary.

Or start watching ORM :)

0
source

Source: https://habr.com/ru/post/1698147/


All Articles