Share via


Closed Enumerations

[This content is no longer valid. For the latest information on "M", "Quadrant", SQL Server Modeling Services, and the Repository, see the Model Citizen blog.]

A closed enumeration should be used when a fixed set of values must be defined as part of the schema. With a closed enumeration, an application that takes a dependency on the schema (version) can take a dependency on these values.

Design Pattern

Closed enumerations can be defined using a type as part of the schema, or as an extent populated with an initial set of values, which is then secured to prevent update – access control being used to “close” what is otherwise an open enumeration.

An enumeration can be defined in-line as an anonymous type, or as a reusable type which is then referenced explicitly. A reusable type, with a singular name, should be defined if the same enumeration with the same meaning is referenced more than once. In either case, the enumeration is expressed as a collection of values. Most simply, the collection can be used directly as a type, in which case the data type is inferred from the values. Alternatively an explicit data type can be referenced and the collection used in a membership constraint.

When using an enumeration to define a stored field, consideration should be given to the space required. Meaningful text values are appropriate if readability of the stored values is more important than space efficiency. If space is a concern (particularly if the enumeration values are long text strings or there are high volumes involved) a short codified value or alias scheme should be considered. Using small integer alias values results in the most compact storage; however, if alias values are used, they will require translation on update and query.

In the following code, the field ContactKind is declared as an enumeration in-line with meaningful text values. The field Gender is declared inline using an explicit type and a membership constraint over a series of short text codes. PrimaryPhoneUse and SecondaryPhoneUse use an integer-based enumeration defined by the type PhoneUse where the data type is inferred from the values.

module Patterns.Enumerations.Closed
{   
    People : 
    {
        Id : Integer64 = AutoNumber();
        
        Name : Text where value.Count <= 100;
        
        ContactKind :  
        {
            "Customer",            
            "Colleague",
            "Friend"
        };
        
        Gender : {"M", "F"};
    
        PrimaryPhone : (Text where value.Count <= 20)?;
        
        PrimaryPhoneUse : PhoneUse?;
        
        SecondaryPhone : (Text where value.Count <= 20)?;
        
        SecondaryPhoneUse : PhoneUse?;
   
    }* where identity Id;    

    type PhoneUse 
        {            
            // Home
            1,
            
            // Work
            2,
            
            // Unknown
            3            
        };
}

See Also

Concepts

Modeling Patterns and Guidelines