Enums, following the C# Reference
, is a Type consisting of a set of name constants. The important thing about Enums is that this set is known at compile time, so you have all the nice type checking and IntelliSense.
Each enums value has two important things. A numeric value of the type of the enums underlying type (usually int), and a string identifier.
Using Enums has always been very convenient while you stay in your IDE, but using it from the outside is a bit too complicated for such a simple thing. When you are storing Enums on DB you have to choose one the following 3 alternatives:
- Store the Value: Easy, but hides the meaning of the value if you don't have access to the source code.
- Store the Identifier: Easy, but lacks normalization.
- Creating a Value - Identifier Table and a FK to it: A bit too much work for just an Enum.
Signum Framework has complete support for Enums from day 0 in all the areas: save and retrieve, database generation, queries... even synchronization.
Just place an Enum field in you entities and the third option will be created for you.
If your enum has [Flags] no FK will be created.
Enums and Synchronization ¶
One neat thing about Enums support is that the Synchronization
mechanism will keep them up to date as you change your code, creating, removing and updating the rows in the table for the enum.
Important Note: The Id (not the string representation) is used to make the synchronization. If you don't specify your enum values explicitly be carefull reordering them.
EnumProxy (Advanced topic)
The way Enum support is implemented internally is using EnumProxy<T> as a bridge between Enums and IdentifiableEntity. This class does two things:
- Stores the Enum Value in the Id field
- Stores the String value int the ToStr field
Commonly you don't need to know about EnumProxy but there are certain scenarios where you would need it.
1. If you use EnumProxy<DayOfWeek> instead of DayOfWeek as the type of some of your entities field you get a nice balance between intellisense:
EnumProxy<DayOfWeek> day = DayOfWeek.Friday;
if (day!= (EnumProxy<DayOfWeek>)DayOfWeek.Saturday)
And runtime flexibility;
EnumProxy<DayOfWeek> newDay = GetLastDayOfWeek();
2. Sometimes you need to save, retrieve or customize the Schema
for Enums at runtime. All the methods to do that will ask you for a IdentifiableEntity, so you will have to use an EnumProxy for it.