Create transfers from a database or vice versa?

I am trying to figure out which “right” way to do this. I have my own lookup table in my database and would like to place the enumeration on top of these values, so when encoding it is easier to read (and also not use hardcoded values).

I am wondering if I should generate my table values ​​based on an existing enumeration or if I should generate my enum from my table values.

EDIT

Based on the first two comments, here are some explanations:

The frequency of changes in values ​​can be quite frequent, since they must be quite dynamic. In this case, compilation will be necessary before adding any of them in any case, because the enumeration needs to be updated to show new values.

The main reason for this need is that we do not want to bind people to a specific list of values, we would like applications to be able to add new entries as needed.

In the past, we generated data from enumerations, but I second guess about myself

+6
source share
2 answers

Usually we generate transfers from the database. We use CodeSmith , which allows us to create project files that can easily regenerate enumerations as needed.

We sometimes met, as a rule, for reporting purposes (when existing enumeration values ​​are preserved).

And, of course, we have enumerations whose values ​​are never stored.

In general, the only reason for generating transfers from the database is that the code must make decisions based on them. If you just want to populate the ComboBox and save the user's choice, do not create an enumeration.

Obviously, making decisions based on enumerations (or strings) whose values ​​can change is fragile. You might want to consider including the expiration dates (or “from” and “to”) in your database schema so that existing values ​​are not deleted. Filter outdated values ​​when populating user interface selectors. It also facilitates referential integrity.

As always in C #, you should be aware that enumeration values ​​may fall outside the expected range. Turn default on switch .

We have developed helper classes for creating cached query lists that simplify their use.

I do not protect this route. If necessary, we did it.

+1
source

There is also a third option, in which you have an explicit model that describes the circuit at the required level of detail, and then you generate both data and the circuit from this model.

As for your question, I think that you should think about the problem in your context and list the pros and cons for you with each alternative and decide what matters most to you and your business.

I worked with all three strategies for different applications, and I personally prefer to have an explicit model, but depending on the context.

Sorry for the vagueness, but I think that for such issues there is a real golden rule that always applies in all cases.

0
source

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


All Articles