|
| 1 | +### Enums |
| 2 | + |
| 3 | +In OData, enums represent a subset of the nominal type they rely on, and are especially useful in cases where certain properties have predefined, limited options. |
| 4 | + |
| 5 | +```xml |
| 6 | +<EnumType Name="color"> |
| 7 | + <Member Name="Red" Value="0" /> |
| 8 | + <Member Name="Green" Value="1" /> |
| 9 | + <Member Name="Blue" Value="2" /> |
| 10 | +</EnumType> |
| 11 | +``` |
| 12 | + |
| 13 | +#### Pros |
| 14 | + |
| 15 | +- Our SDK generators will translate the enum to the best representation of the target programming language, resulting in a better developer experience and free client side validation |
| 16 | + |
| 17 | +#### Cons |
| 18 | + |
| 19 | +- Adding a new value requires to go through a (generally fast) API Review |
| 20 | +- If the enum is not [evolvable](./patterns/evolvable-enums.md), adding a new value is a breaking change and will generally not be allowed |
| 21 | + |
| 22 | +#### Enum or Booleans |
| 23 | + |
| 24 | +Enumerations are a good alternative to Booleans when one of the two values (`true`, `false`) conveys other possible values not yet conceived. Let's assume we have an `publicNotification` type and a property to communicate how to display it: |
| 25 | + |
| 26 | +```xml |
| 27 | +<ComplexType Name="publicNotification"> |
| 28 | + <Property Name="title" Type="Edm.String" /> |
| 29 | + <Property Name="message" Type="Edm.String" /> |
| 30 | + <Property Name="displayAsTip" Type="Edm.Boolean" /> |
| 31 | +</ComplexType> |
| 32 | +``` |
| 33 | + |
| 34 | +The `false` value here merely communicates that the notification shall not be displayed as a tip. What if, in the future, the notification could be displayed as a `tip` or `alert`, and then in a more distant future, a `dialog` option is viable? |
| 35 | + |
| 36 | +With the current model, the only way is to add more boolean properties to convey the new information: |
| 37 | + |
| 38 | +```diff |
| 39 | +<ComplexType Name="publicNotification"> |
| 40 | + <Property Name="title" Type="Edm.String" /> |
| 41 | + <Property Name="message" Type="Edm.String" /> |
| 42 | + <Property Name="displayAsTip" Type="Edm.Boolean" /> |
| 43 | ++ <Property Name="displayAsAlert" Type="Edm.Boolean" /> |
| 44 | ++ <Property Name="displayAsDialog" Type="Edm.Boolean" /> |
| 45 | +</ComplexType> |
| 46 | +``` |
| 47 | + |
| 48 | +Additionally speaking, the workload will now also have to validate the data structure and make sure that only one of the 3 values is `true` |
| 49 | + |
| 50 | +By using an evolvable enum, instead, all we need to do is to add new members: |
| 51 | + |
| 52 | +```diff |
| 53 | +<ComplexType Name="publicNotification"> |
| 54 | + <Property Name="title" Type="Edm.String" /> |
| 55 | + <Property Name="message" Type="Edm.String" /> |
| 56 | ++ <Property Name="displayMethod" Type="microsoft.graph.displayMethod" /> |
| 57 | +- <Property Name="displayAsTip" Type="Edm.Boolean" /> |
| 58 | +- <Property Name="displayAsAlert" Type="Edm.Boolean" /> |
| 59 | +- <Property Name="displayAsDialog" Type="Edm.Boolean" /> |
| 60 | +</ComplexType> |
| 61 | +``` |
| 62 | + |
| 63 | +```xml |
| 64 | +<EnumType Name="displayMethod"> |
| 65 | + <Member Name="tip" Value="0" /> |
| 66 | + <Member Name="unknownFutureValue" Value="1" /> |
| 67 | + <Member Name="alert" Value="2" /> |
| 68 | + <Member Name="dialog" Value="3" /> |
| 69 | +</EnumType> |
| 70 | +``` |
| 71 | + |
| 72 | +Similarly speaking, if you find yourself using a `nullable` Enum, that is a indication that maybe what you are trying to model is something that has 3 states and an enum is more appropraite. For instance, let's assume we have a boolean property called `syncEnabled`, where `null` means that the value is undefined and inherited from the general tenant configuration. Instead of modelling like a boolean: |
| 73 | + |
| 74 | +```xml |
| 75 | +<Property Name="syncEnabled" Type="Edm.Boolean" Nullable="true"/> |
| 76 | +``` |
| 77 | + |
| 78 | +An enum not only better conveys the message: |
| 79 | + |
| 80 | +```xml |
| 81 | +<EnumType Name="syncState"> |
| 82 | + <Member Name="enabled" Value="0" /> |
| 83 | + <Member Name="disabled" Value="1" /> |
| 84 | + <Member Name="tenantInherit" Value="2" /> |
| 85 | + <Member Name="unknownFutureValue" Value="3" /> |
| 86 | +</EnumType> |
| 87 | +``` |
| 88 | + |
| 89 | +but it is also open for future scenarios: |
| 90 | + |
| 91 | +```diff |
| 92 | +<EnumType Name="syncState"> |
| 93 | + <Member Name="enabled" Value="0" /> |
| 94 | + <Member Name="disabled" Value="1" /> |
| 95 | + <Member Name="tenantInherit" Value="2" /> |
| 96 | + <Member Name="unknownFutureValue" Value="3" /> |
| 97 | ++ <Member Name="groupInherit" Value="4" /> |
| 98 | +</EnumType> |
| 99 | +``` |
| 100 | + |
| 101 | +Additionally speaking, depending on the situation, a nullable enum can very likely be avoided by adding a `none` member. |
| 102 | + |
| 103 | +#### Flag Enums or Collection of Enums |
| 104 | + |
| 105 | +In case an enum can have multiple values at the same time the tentation is to model the property as a collection of Enums: |
| 106 | + |
| 107 | +```xml |
| 108 | +<Property Name="displayMethods" Type="Collection(displayMethod)"/> |
| 109 | +``` |
| 110 | + |
| 111 | +However, [Flagged Enums](https://docs.oasis-open.org/odata/odata-csdl-xml/v4.01/odata-csdl-xml-v4.01.html#_Toc38530378) can model this use case scenario: |
| 112 | + |
| 113 | +```diff |
| 114 | +- <EnumType Name="displayMethod"> |
| 115 | ++ <EnumType Name="displayMethod" isFlag="true"> |
| 116 | +- <Member Name="tip" Value="0" /> |
| 117 | ++ <Member Name="tip" Value="1" /> |
| 118 | +- <Member Name="unknownFutureValue" Value="1" /> |
| 119 | ++ <Member Name="unknownFutureValue" Value="2" /> |
| 120 | +- <Member Name="alert" Value="2" /> |
| 121 | ++ <Member Name="alert" Value="4" /> |
| 122 | +- <Member Name="dialog" Value="3" /> |
| 123 | ++ <Member Name="dialog" Value="8" /> |
| 124 | +</EnumType> |
| 125 | +``` |
| 126 | + |
| 127 | +With such enum, customers can select multiple values in a single field: |
| 128 | + |
| 129 | +`displayMethod = tip | alert` |
0 commit comments