If I have, say, a table of films which amongs other things has a int FilmTypeId field and a table of film types, with the id and a meaningful description along the lines of:
- 1 – horror
- 2 – comedy
- …
Whats the best way of using that information in a C# class?
currently I would have them as Constants in a helper class (unsuprisingly air code):
public class FilmHelper { public const int HorrorFilmType = 1; public const int ComedyFilmType = 2; ... }
but this doesn’t seem that maintainable. But I would like to avoid a database call everytime I came to use the constant or an additional db call everytime I used either the helper or the main entity.
Is the list of types fixed or does it change?
If fixed, I would encapsulate it in an enum:
Then just cast. You can use attributes (and a few lines of bespoke code) to store an extra description per enum item.
If the list changes I would probably read it once early on in the app, and cache the list in a lookup somewhere (perhaps static, perhaps in a specific cache). Then just do the lookups from the cached copy.
You can take this further by adding properties that change between the two representations.