You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: graph/GuidelinesGraph.md
+3-128Lines changed: 3 additions & 128 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -191,6 +191,9 @@ The three most often used patterns in Microsoft Graph today are type hierarchy,
191
191
192
192
-**[Flat bag of properties](./patterns/flat-bag.md)** is represented by one entity type with all the potential properties plus an additional property to distinguish the variants, often called type. The type property describes the variant and also defines properties that are required or meaningful for the variant given by the type property.
193
193
194
+
-**[Enums](./patterns/enums.md)** represent a subset of the nominal type they rely on, and are especially useful in cases where certain properties have predefined, limited options.
195
+
196
+
194
197
The following table shows a summary of the main qualities for each pattern and can help you select a pattern fit for your use case.
195
198
196
199
| API qualities\patterns | Properties and behavior described in metadata | Supports combinations of properties and behaviors | Simple query construction |
@@ -319,135 +322,7 @@ For a complete mapping of error codes to HTTP statuses, see
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.
325
-
326
-
```xml
327
-
<EnumTypeName="Color">
328
-
<MemberName="Red"Value="0" />
329
-
<MemberName="Green"Value="1" />
330
-
<MemberName="Blue"Value="2" />
331
-
</EnumType>
332
-
```
333
-
334
-
#### Pros
335
-
336
-
- 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
337
-
338
-
#### Cons
339
-
340
-
- Adding a new value requires to go through a (generally fast) API Review
341
-
- If the enum is not [evolvable](./patterns/evolvable-enums.md), adding a new value is a breaking change and will generally not be allowed
342
-
343
-
#### Enum or Booleans
344
-
345
-
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 `Error` type and a property to communicate how to display it:
346
-
347
-
```xml
348
-
<ComplexTypeName="Error">
349
-
<PropertyName="title"Type="Edm.String" />
350
-
<PropertyName="message"Type="Edm.String" />
351
-
<PropertyName="displayAsTip"Type="Edm.Boolean" />
352
-
</ComplexType>
353
-
```
354
-
355
-
The `false` value here merely communicates that the error shall not be displayed as a tip. What if, in the future, the error could be displayed as a `tip` or `alert`, and then in a more distant future, a `dialog` option is viable?
356
-
357
-
With the current model, the only way is to add more boolean properties to convey the new information:
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:
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:
433
-
434
-
```diff
435
-
- <EnumType Name="displayMethod">
436
-
+ <EnumType Name="displayMethod" isFlag="true">
437
-
- <Member Name="tip" Value="0" />
438
-
+ <Member Name="tip" Value="1" />
439
-
- <Member Name="unknownFutureValue" Value="1" />
440
-
+ <Member Name="unknownFutureValue" Value="2" />
441
-
- <Member Name="alert" Value="2" />
442
-
+ <Member Name="alert" Value="4" />
443
-
- <Member Name="dialog" Value="3" />
444
-
+ <Member Name="dialog" Value="8" />
445
-
</EnumType>
446
-
```
447
-
448
-
With such enum, customers can select multiple values in a single field:
449
325
450
-
`displayMethod = tip | alert`
451
326
452
327
## API contract and non-backward compatible changes
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
+
<EnumTypeName="Color">
7
+
<MemberName="Red"Value="0" />
8
+
<MemberName="Green"Value="1" />
9
+
<MemberName="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 `Error` type and a property to communicate how to display it:
25
+
26
+
```xml
27
+
<ComplexTypeName="Error">
28
+
<PropertyName="title"Type="Edm.String" />
29
+
<PropertyName="message"Type="Edm.String" />
30
+
<PropertyName="displayAsTip"Type="Edm.Boolean" />
31
+
</ComplexType>
32
+
```
33
+
34
+
The `false` value here merely communicates that the error shall not be displayed as a tip. What if, in the future, the error 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:
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:
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:
0 commit comments