Signum Framework Logo
"Open framework that encourages convention over configuration, using C# code,
not XML files, to model at the right level of abstraction and achieve deadlines.
...but also has a full Linq provider, and syncs the schema for you!"
Login
RSS

Search

»



Main
Index
Map
Videos
Download
Source code
Tutorials
Forum
FAQ



Image



PoweredBy

Introduction

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.

Enums Support

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; // implicit cast
 if (day!= (EnumProxy<DayOfWeek>)DayOfWeek.Saturday) // no nasty string literals, full IntelliSense and type checking
 {...}

And runtime flexibility;

 EnumProxy<DayOfWeek> newDay = GetLastDayOfWeek(); 
 Console.WriteLine(newDay.ToStr); // you could retrieve the eighth day of the week and still working http://en.wikipedia.org/wiki/Week

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.



Creative Commons License Signum Framework Site by Signum Software is licensed under a Creative Commons Attribution 3.0 License.
Powered by ScrewTurn Wiki version 3.0.5.600.