{"id":383,"date":"2014-03-18T02:47:00","date_gmt":"2014-03-18T02:47:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/odatateam\/2014\/03\/18\/tutorial-sample-use-enumeration-types-in-odata\/"},"modified":"2020-01-07T07:41:06","modified_gmt":"2020-01-07T14:41:06","slug":"tutorial-sample-use-enumeration-types-in-odata","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/odata\/tutorial-sample-use-enumeration-types-in-odata\/","title":{"rendered":"[Tutorial &amp; Sample] Use Enumeration types in OData"},"content":{"rendered":"<p>Enumeration is a very common data type in various data models. Enumeration types are useful means to represent data that needs to support certain programmatic options (such as the ability to detect one or more options are selected). OData v4 now supports Enumeration types, along with Primitive types, Complex types, and Entity types. And we expect there will be heavy usage of them.<\/p>\n<p>Let\u2019s look at an example. Suppose we have an entity type called <strong>Product<\/strong>, and we want to have a property member that stores the color of the product. We could use a string type property, but an enumeration type property would look better. We can define an enumeration type <strong>Color<\/strong>, and it contains members like <em>Red<\/em>, <em>Green<\/em>, <em>Blue<\/em>, etc. In Product definition we can define a member property called <em>PackageColor<\/em>, whose type is <strong>Color<\/strong>. Now we can assign color values to <em>PackageColor<\/em>. To get all the products that are in <em>Green<\/em> color, we can send a request like \u201c\/<span style=\"text-decoration: underline;\">svc\/Products?$filter=PackageColor eq Namespace.Color\u2019Green<\/span>\u2019\u201d.<\/p>\n<p>An enumeration type can be defined with \u201cIsFlags=true\u201d, meaning that the members can be combined using bitwise; otherwise the members are intended to be exclusive. The <strong>Color<\/strong> type in the example above is defined with \u201cIsFlags=false\u201d, as we don\u2019t want its members to be combined (we don\u2019t allow a color to be both green and blue). Here another example is an enumeration type called <strong>AccessLevel<\/strong>, which indicates the rights on some resource. <strong>AccessLevel<\/strong> has some members Like: <em>None, Read, Write, Execute<\/em>. In Product type we have another property named <em>UserAccess<\/em> which is of <strong>AccessLevel<\/strong> type. We can assign combined values to <em>UserAccess<\/em>, such as \u201cRead, Write\u201d. And if we want to get all the products that we have <em>Read<\/em> and <em>Write<\/em> access to, we can send a query like \u201c<span style=\"text-decoration: underline;\">\/svc\/Products?$filter=UserAccess has Namespace.AccessLevel\u2019Read, Write\u2019<\/span>\u201d. The \u201chas\u201d operator is evaluated in a bitwise way. That means UserAccess may contain more bits than the value on the right of the operator.<\/p>\n<p>How to use Enumeration types in your OData service? The following part will give the elaborated steps, as well as some code snippets to help you understand. You can get the complete sample code <a title=\"here\" href=\"https:\/\/odatasamples.codeplex.com\/SourceControl\/latest#Features\/Enum\/\">here<\/a>.<\/p>\n<p>On server side, you need to first build an EDM model that supports your scenario with enumeration types. With the model ready, your service needs to respond to client requests by writing the correct payload which may have enumeration type values in it. The second step is called serialization. Let\u2019s look at the details.<\/p>\n<p><span style=\"font-size: small;\"><strong>1. <\/strong><strong>Build the EdmModel.<\/strong><\/span><\/p>\n<p>To use Enum type, you need to first define your Enum types in EdmModel. The enum type can be defined with &#8220;IsFlags=true&#8221; or \u201cIsFlags=false\u201d. The absence of the IsFlags attribute implies the default behavior: the same as &#8220;IsFlags=false&#8221;.<\/p>\n<p>The following code defines two Enum types: <strong>Color<\/strong> and <strong>AccessLevel<\/strong>, and an entity type <strong>Product<\/strong> which has two properties <em>UserAccess<\/em> and <em>PackageColor<\/em>, of type <strong>AccessLevel<\/strong> and <strong>Color<\/strong> respectively.<\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/tianouyang\/5325e7ea53d705f96b5a.js\"><\/script><\/p>\n<p>In $metadata, they look like:<\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/tianouyang\/bbf88d87f23349b23433.js\"><\/script><\/p>\n<p><span style=\"font-size: small;\"><strong>2. <\/strong><strong>Write payload<\/strong><\/span><\/p>\n<p>The next step to do is enable the service to write payloads that have enumeration type values. To write an entity that has enumeration type properties is not very different from the other entities. The only difference is at the entry creation stage. An Enum property has to be assigned an ODataEnumValue object which represents the Enum value. The code snippet below shows how to do it.<\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/tianouyang\/1005f23ec2ca3708393d.js\"><\/script><\/p>\n<p>One thing to notice is, the ODataEnumValue is initialized with a string (\u201cGreen\u201d,\u201cWrite\u201d). The string can also be the underlying value like \u201c1\u201d, \u201c4\u201d.<\/p>\n<p>Once our service is ready to support enumeration type values, we can build a client to consume the data. On client side, we need to read the payload and construct the property values. This process is called deserialization. Let\u2019s look at how the client does it.<\/p>\n<p><span style=\"font-size: small;\"><strong>3. <\/strong><strong>Read payload<\/strong><\/span><\/p>\n<p>The following code shows a client that reads an entry with Enum properties. At the end, packageColor.Value and userAccess.Value have the string values of the Enum properties. In our sample, packageColor.Value would be \u201cGreen\u201d, and userAccess.Value would be \u201cWrite\u201d.<\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/tianouyang\/9c30e0f980c11c4d6b2f.js\"><\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Enumeration is a very common data type in various data models. Enumeration types are useful means to represent data that needs to support certain programmatic options (such as the ability to detect one or more options are selected). OData v4 now supports Enumeration types, along with Primitive types, Complex types, and Entity types. And we [&hellip;]<\/p>\n","protected":false},"author":524,"featured_media":3253,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-383","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-odata"],"acf":[],"blog_post_summary":"<p>Enumeration is a very common data type in various data models. Enumeration types are useful means to represent data that needs to support certain programmatic options (such as the ability to detect one or more options are selected). OData v4 now supports Enumeration types, along with Primitive types, Complex types, and Entity types. And we [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/posts\/383","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/users\/524"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/comments?post=383"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/posts\/383\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/media\/3253"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/media?parent=383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/categories?post=383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/tags?post=383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}