Skip to main content

Cloud

31 Days of OData – Day 4 Metadata in OData

OData-logo_bigger_thumb1Let’s go back 15 years ago when I started professionally developing software and using relational databases. The relational database was a great tool to deal with data. No more did we have to use flat files to store our data with tools named Paradox, dBase and other such data management systems. We now had more flexibility to build our solutions and the new architecture pattern Client-Server that was the rage for all developers circa 1996.

Relational databases gave us the SQL language and that in turn allowed the developers of the day to quickly create, read, update and delete (CRUD) data that was stored on the server and build our applications at the client. Life was great (for me at least) because I knew nothing different. If I wanted to learn about a new database, I would get the schema either in a text file that had a set of CREATE SQL calls for all the objects that made up the database. If we were really lucky we received a printed graph of the database that gave the developers a nice graphical layout of the data tables and views with the relationships between the tables and views. It took time to understand the meaning of the database but that is what we did.
In 2007 I experienced a glimpse into the future that shook my world. It was when I started experimenting with a new incubation project from Microsoft called Astoria. I saw it at first as way to get data across HTTP and that excited me. What blew my mind after really learning about it was Metadata. Compared to the time I spent discovering the meaning and shape of the data schema of a relational database that a DBA created and handed to me, Metadata was the bridge into a new frontier.
Let’s step back and take a look at what Metadata is and how it works within OData. Metadata is defined broadly as “Data about Data”. Think of metadata as the Dewey Decimal we learned as kids when we visited the library. The card catalog does not carry the information about the content inside the books but it does describe the books and information that the books contain in a broad level to allow readers to find and locate the books they are interested in on the shelves. Metadata does the same for us as users of the data that the OData feeds allow us to consume.
In OData, the Service Metadata Document allows the consumers of the data provided by the OData feed to understand the Entity Data Model (Day 3 of the series). The Service Metadata Document does not carry any details of the data held within the data repository of the OData feed. It contains the roadmap to the data. In the SOAP world the Service Metadata Document would be the equivalent of the WSDL file. Let’s look at the structure of the Service Metadata Document (in XML) and walk through its map. We will use the Baseball Statistics OData feed found here as our example.
We first need to query the Service Metadata Document from the OData feed. How this is accomplished is my using the $metadata query option at the base of the OData feed as shown below:
http://baseball-stats.info/OData/baseballstats.svc/$metadata
The Service Metadata Document is returned in the payload after the $metadata query and has three parts: the EDMX which is made up of the OData Model and also the Entity Collection.

Code Snippet
  1. <?xmlversion=1.0encoding=UTF8standalone=true?>
  2. <edmx:Edmxxmlns:edmx=http://schemas.microsoft.com/ado/2007/06/edmxVersion=1.0>
  3. <edmx:DataServicesm:DataServiceVersion=1.0xmlns:m=http://schemas.microsoft.com/ado/2007/08/dataservices/metadata>
  4. <Schemaxmlns=http://schemas.microsoft.com/ado/2008/09/edmxmlns:d=http://schemas.microsoft.com/ado/2007/08/dataservicesxmlns:m=http://schemas.microsoft.com/ado/2007/08/dataservices/metadataNamespace=BaseballStatsModel>
  5. <EntityTypeName=Allstar>
  6. <Key>
  7. <PropertyRefName=playerID/>
  8. <PropertyRefName=yearID/>
  9. <PropertyRefName=lgID/>
  10. </Key>
  11. <PropertyName=playerIDFixedLength=falseUnicode=trueMaxLength=10Nullable=falseType=Edm.String/>
  12. <PropertyName=yearIDNullable=falseType=Edm.Int16/> <PropertyName=lgIDFixedLength=falseUnicode=trueMaxLength=2Nullable=falseType=Edm.String/>
  13. <NavigationPropertyName=PlayerToRole=PlayerFromRole=AllstarRelationship=BaseballStatsModel.FK_Allstar_Player/>
  14. </EntityType>
  15. <EntityTypeName=AllstarFull>
  16. <Key>
  17. <PropertyRefName=playerID/>
  18. <PropertyRefName=yearID/>
  19. <PropertyRefName=gameNum/>
  20. </Key>
  21. <PropertyName=playerIDFixedLength=falseUnicode=trueMaxLength=10Nullable=falseType=Edm.String/>
  22. <PropertyName=yearIDNullable=falseType=Edm.Int16/> <PropertyName=gameNumNullable=falseType=Edm.Int16/>
  23. <PropertyName=gameIDFixedLength=falseUnicode=trueMaxLength=12Nullable=trueType=Edm.String/>
  24. <PropertyName=teamIDFixedLength=falseUnicode=trueMaxLength=3Nullable=trueType=Edm.String/>
  25. <PropertyName=lgIDFixedLength=falseUnicode=trueMaxLength=2Nullable=trueType=Edm.String/>
  26. <PropertyName=GPNullable=trueType=Edm.Int16/> <PropertyName=startingPosNullable=trueType=Edm.Int16/>
  27. <NavigationPropertyName=PlayerToRole=PlayerFromRole=AllstarFullRelationship=BaseballStatsModel.FK_AllstarFull_Player/>
  28. </EntityType>
  29. <EntityTypeName=PitchingTotals>
  30. <Key>
  31. <PropertyRefName=playerID/>
  32. </Key>
  33. <PropertyName=playerIDFixedLength=falseUnicode=trueMaxLength=10Nullable=falseType=Edm.String/>
  34. <PropertyName=WNullable=trueType=Edm.Int32/> <PropertyName=LNullable=trueType=Edm.Int32/>
  35. <PropertyName=GNullable=trueType=Edm.Int32/> <PropertyName=GSNullable=trueType=Edm.Int32/>
  36. <PropertyName=CGNullable=trueType=Edm.Int32/> <PropertyName=SHONullable=trueType=Edm.Int32/>
  37. <PropertyName=SVNullable=trueType=Edm.Int32/> <PropertyName=IPoutsNullable=trueType=Edm.Int32/>
  38. <PropertyName=HNullable=trueType=Edm.Int32/> <PropertyName=ERNullable=trueType=Edm.Int32/>
  39. <PropertyName=HRNullable=trueType=Edm.Int32/> <PropertyName=BBNullable=trueType=Edm.Int32/>
  40. <PropertyName=SONullable=trueType=Edm.Int32/> <PropertyName=BAOppNullable=trueType=Edm.Double/>
  41. <PropertyName=IBBNullable=trueType=Edm.Int32/> <PropertyName=WPNullable=trueType=Edm.Int32/>
  42. <PropertyName=HBPNullable=trueType=Edm.Int32/> <PropertyName=BKNullable=trueType=Edm.Int32/>
  43. <PropertyName=BFPNullable=trueType=Edm.Int32/> <PropertyName=GFNullable=trueType=Edm.Int32/>
  44. <PropertyName=RNullable=trueType=Edm.Int32/> <PropertyName=SHNullable=trueType=Edm.Int32/>
  45. <PropertyName=SFNullable=trueType=Edm.Int32/> <PropertyName=GIDPNullable=trueType=Edm.Int32/>
  46. </EntityType>
  47. <AssociationName=FK_Allstar_Player>
  48. <EndType=BaseballStatsModel.PlayerMultiplicity=1Role=Player/> <EndType=BaseballStatsModel.AllstarMultiplicity=*Role=Allstar/> <ReferentialConstraint>
  49. <PrincipalRole=Player>
  50. <PropertyRefName=playerID/>
  51. </Principal> <DependentRole=Allstar>
  52. <PropertyRefName=playerID/>
  53. </Dependent>
  54. </ReferentialConstraint>
  55. </Association>
  56. <AssociationName=FK_AllstarFull_Player>
  57. <EndType=BaseballStatsModel.PlayerMultiplicity=1Role=Player/> <EndType=BaseballStatsModel.AllstarFullMultiplicity=*Role=AllstarFull/> <ReferentialConstraint>
  58. <PrincipalRole=Player>
  59. <PropertyRefName=playerID/>
  60. </Principal> <DependentRole=AllstarFull>
  61. <PropertyRefName=playerID/>
  62. </Dependent>
  63. </ReferentialConstraint>
  64. </Association>
  65. <AssociationName=FK_TeamsHalf_Teams>
  66. <EndType=BaseballStatsModel.TeamMultiplicity=1Role=Team/> <EndType=BaseballStatsModel.TeamHalfMultiplicity=*Role=TeamHalf/> <ReferentialConstraint>
  67. <PrincipalRole=Team>
  68. <PropertyRefName=yearID/>
  69. <PropertyRefName=lgID/>
  70. <PropertyRefName=teamID/>
  71. </Principal> <DependentRole=TeamHalf>
  72. <PropertyRefName=yearID/>
  73. <PropertyRefName=lgID/>
  74. <PropertyRefName=teamID/>
  75. </Dependent>
  76. </ReferentialConstraint>
  77. </Association>
  78. </Schema>
  79. <Schemaxmlns=http://schemas.microsoft.com/ado/2008/09/edmxmlns:d=http://schemas.microsoft.com/ado/2007/08/dataservicesxmlns:m=http://schemas.microsoft.com/ado/2007/08/dataservices/metadataNamespace=BaseballStatsOData>
  80. <EntityContainerName=BaseballStatsEntitiesxmlns:p7=http://schemas.microsoft.com/ado/2009/02/edm/annotationm:IsDefaultEntityContainer=truep7:LazyLoadingEnabled=true>
  81. <EntitySetName=AllstarEntityType=BaseballStatsModel.Allstar/>
  82. <EntitySetName=AllstarFullEntityType=BaseballStatsModel.AllstarFull/>
  83. <EntitySetName=PitchingTotalsEntityType=BaseballStatsModel.PitchingTotals/>
  84. <AssociationSetName=FK_Allstar_PlayerAssociation=BaseballStatsModel.FK_Allstar_Player>
  85. <EndRole=PlayerEntitySet=Player/>
  86. <EndRole=AllstarEntitySet=Allstar/>
  87. </AssociationSet>
  88. <AssociationSetName=FK_AllstarFull_PlayerAssociation=BaseballStatsModel.FK_AllstarFull_Player>
  89. <EndRole=PlayerEntitySet=Player/>
  90. <EndRole=AllstarFullEntitySet=AllstarFull/>
  91. </AssociationSet>
  92. <AssociationSetName=FK_TeamsHalf_TeamsAssociation=BaseballStatsModel.FK_TeamsHalf_Teams>
  93. <EndRole=TeamEntitySet=Team/>
  94. <EndRole=TeamHalfEntitySet=TeamHalf/>
  95. </AssociationSet>
  96. </EntityContainer>
  97. </Schema>
  98. </edmx:DataServices>
  99. </edmx:Edmx>

OData Model

The OData Model section of the Service Metadata Document has the following structure:

Code Snippet
  1. <SchemaNamespace=“”>
  2. <EntityTypeName=“”>
  3. <Key>
  4. <PropertyRefName=“”/>
  5. </Key>
  6. <PropertyName=“”Type=Edm.String/>
  7. <NavigationPropertyName=“”ToRole=“”FromRole=“”Relationship=“”/>
  8. </EntityType>
  9. <AssociationName=“”>
  10. <EndType=“”Multiplicity=1Role=“”/>
  11. <EndType=“”Multiplicity=*Role=“”/>
  12. <ReferentialConstraint>
  13. <PrincipalRole=“”>
  14. <PropertyRefName=“”/>
  15. </Principal>
  16. <DependentRole=“”>
  17. <PropertyRefName=“”/>
  18. </Dependent>
  19. </ReferentialConstraint>
  20. </Association>
  21. </Schema>

The purpose of the OData Model section is to give the detailed information for each object in the OData feed. The first objects aggregated in the OData Model are the EntityTypes with their full descriptions including the Key, Properties and NavigationProperties. Next is the set of Associations that are in the Entity Data Model. In addition each metadata type is fully described based on the data such as the EntityType’s Property details such as type, if the Property is nullable, the maximum length of the data of the property and/or many more that give us a complete view of the Property. Another example is the details of the Association which will give the entities the Association join together as well as the type of join.

Entity Collection

The Entity Collection is also part of the Service Metadata Document and it follows the OData Model in the payload that is returned from the OData feed. An example of the Entity Collection follows:

Code Snippet
  1. <Schemaxmlns=http://schemas.microsoft.com/ado/2008/09/edmxmlns:d=http://schemas.microsoft.com/ado/2007/08/dataservicesxmlns:m=http://schemas.microsoft.com/ado/2007/08/dataservices/metadataNamespace=MyOData>
  2. <EntityContainerName=MyEntitiesxmlns:p7=http://schemas.microsoft.com/ado/2009/02/edm/annotationm:IsDefaultEntityContainer=truep7:LazyLoadingEnabled=true>
  3. <EntitySetName=Entity1EntityType=ODataModel.Entity1/>
  4. <EntitySetName=Entity2EntityType=ODataModel.Entity2/>
  5. <EntitySetName=ODataModelEntityType=ODataModel.EntityN/>
  6. <AssociationSetName=FK_Entity1_Entity2Association=ODataModel.FK_Entity1_Entity2>
  7. <EndRole=Entity1EntitySet=Entity1/>
  8. <EndRole=Entity2EntitySet=Entity2/>
  9. </AssociationSet>
  10. <AssociationSetName=FK_Entity1_EntityNAssociation=ODataModel.FK_Entity1_EntityN>
  11. <EndRole=Entity1EntitySet=Entity1/>
  12. <EndRole=EntityNEntitySet=EntityN/>
  13. </AssociationSet>
  14. </EntityContainer>
  15. </Schema>

The Entity Collection does not contain the details of the Entities and Associations contained in the OData feed. It is only the references from the OData Model and gives the roadmap to quickly transverse the landscape from the OData feed. The Entity Collection is made up of the following sections:

  • EntitySet
  • AssociationSet

You can learn more about the Entity Collection from the third day of the blog series. I will not go through it again since it is the same content and details from that post.

Why should I love the Metadata?

image_thumb3When I speak and present to the community about OData, I think most people do not get the beauty of Metadata. I have a slide deck where I show an iceberg and the data is shown above the water and metadata below. I have the image in this post so you can see what I mean. I do not assume that the Metadata is larger in size or greater in importance to the Data of the OData feed. What I try to get across to the audience is that we may either not see the importance of Metadata or the power we receive from it. It really is the hidden secret of OData (and the Semantic Web).
what most developers also do not know is that is they have worked with OData within Visual Studio 2010 they have benefited from the Metadata of OData. You may ask how and if so lets look at adding a Service Reference to a VS10 project.When we need to add a reference to an OData feed into our VS10 project we usually add it through the “Add Service Reference” feature as shown below:
AddServiceRefence_thumb2
Going through the Wizard to allow the project access to the OData feed is not anything but the Wizard gathering the metadata from the OData feed and creating the proxy classes based on the EntityTypes from the Service Metadata Document. You can see that the Wizard after discovering the metadata from the OData feed gives us the list of the EntityTypes in the Wizard as shown below. With the proxy classes generated your application has the local Model that can be used to build, retrieve and handle the data from the OData.
AddServiceRefenceWizard_thumb3
Now that we have looked at the Metadata of OData, the moving parts of the Metadata Service Document and finally a real world example on how we work with the Metadata each day, I hope this gives you a little more appreciation for OData and also for the protocol.

Tags

Thoughts on “31 Days of OData – Day 4 Metadata in OData”

  1. Hi,thanks for making this post available, it is easy to understand. I was wondering if we can use metadata to generate C# strong typed classes; something similar to what you do with WSDL with the .net utilities?
    Thank you

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Chris Woodruff

More from this Author

Follow Us