ConfinitySelectable
Instead of using Enumeration types properties on your entities, consider using ConfinitySelectable. It is designed to replace enums in your Entity Apps. It is similar to an enum, but is optimized for stability and the use with Confinity.
ConfinitySelectable is a base class with the properties Id, Label and SortIndex (optionally). Stored to the database is only the Id. The Label is used in the entity app. Confinity will search for static fields to find all constants of your ConfinitySelectable.
Define a ConfinitySelectable
To define a ConfinitySelectable you just create a new class which inherits from ConfinitySelectable. For every constant, create a public static field as in the following example.
public class ShirtSize : ConfinitySelectable
{
public static readonly ShirtSize Small = new("small", "Small");
public static readonly ShirtSize Medium = new("medium", "Medium");
public static readonly ShirtSize Large = new("large", "Large");
public ShirtSize(string id, string? label = null, int sortIndex = 0)
: base(id, label, sortIndex)
{
}
}
The ConfinitySelectable can be extended with additional properties or fields.
public class Color : ConfinitySelectable
{
private readonly string _hex;
public static readonly Color Red = new("small", "Small", " #FF0000");
public static readonly Color Green = new("medium", "Medium", "#00FF00");
public static readonly Color Blue = new("large", "Large", "#0000FF");
public Color(string id, string? label = null, int sortIndex = 0)
: base(id, label, sortIndex)
{
_hex = "";
}
private Color(string id, string label, string hex)
: base(id, label, 0)
{
_hex = hex;
}
}
Restrictions
Every ConfinitySelectable must have a constructor for id, label and sortIndex.
Warning
The parameters must not be changed from id, label and sortIndex.
Comparison to enum
- Advantages
- No mapping(s) for labels and other data in the entity app
- Less error prone / easier to handle instead of always remembering that a enum is a number.
- Backwards compatible (unknown values are still read, the author can update them in the entity app and a developer can fix them by using a ef core migration)
- Disadvantages
- Working with enums is slightly faster
Recommendations
For tables with many rows, use short id and set the field length on the column.