/// <summary> /// Use the ODataReader to read results and convert them feed into usable CLR instances. /// </summary> /// <param name="odataReader">OData Reader representing current OData feed download.</param> private void MaterializeResponse(ODataMessageReader odataReader) { var feedReader = this.CreateReader(odataReader); List<ODataEntity> odataEntities = new List<ODataEntity>(); Exception readingError = null; Uri nextPageLink = null; long? totalCount = null; try { ODataEntity currentEntity = new ODataEntity(); while (feedReader.Read()) { // we never expand in this application, so there is no chance of getting back an expanded link switch (feedReader.State) { case ODataReaderState.EntryStart: currentEntity = new ODataEntity(); odataEntities.Add(currentEntity); break; case ODataReaderState.NavigationLinkEnd: ODataNavigationLink navigationLink = (ODataNavigationLink)feedReader.Item; currentEntity.NavigationProperties[navigationLink.Name] = navigationLink.Url; break; case ODataReaderState.EntryEnd: ODataEntry entry = (ODataEntry)feedReader.Item; currentEntity.TypeName = entry.TypeName; currentEntity.EditLink = entry.EditLink; currentEntity.ReadLink = entry.ReadLink; if (entry.MediaResource != null) { currentEntity.MediaUri = entry.MediaResource.ReadLink.OriginalString; currentEntity.MediaType = entry.MediaResource.ContentType; } foreach (var property in entry.Properties) { this.MaterializeProperty(currentEntity, property, property.Name); } break; case ODataReaderState.FeedEnd: ODataFeed feed = (ODataFeed)feedReader.Item; nextPageLink = feed.NextPageLink; totalCount = feed.Count; break; } } } catch (Exception deserializationError) { readingError = deserializationError; } this.RaiseFeedDownloaded(odataEntities, nextPageLink, totalCount, readingError); }
/// <summary> /// Read instance property value and add to Property dictionary on ODataEntity instance. /// </summary> /// <param name="currentEntity">ODataEntity instance being initialized</param> /// <param name="property">The instance property value being read.</param> /// <param name="propertyName">The name of the instance property being read.</param> private void MaterializeProperty(ODataEntity currentEntity, ODataProperty property, string propertyName) { if (property.Value is ODataComplexValue) { // for a given complex value , we will flatten it out so that // Excel can show these values in a tabular format instead of having to nest rows. // e.g.: if the property value is BoxArt{ SmallUrl ="", LargeUrl =""} // we will convert it into . // instance[BoxArt.SmallUrl] = ""; // instance[BoxArt.LargeUrl] = ""; ODataComplexValue complexPropertyValue = (ODataComplexValue)property.Value; foreach (var primitiveProperty in complexPropertyValue.Properties) { string flattenedPropertyName = string.Join(".", propertyName, primitiveProperty.Name); this.MaterializeProperty(currentEntity, primitiveProperty, flattenedPropertyName); } } else if (property.Value is ODataCollectionValue) { // we don't support collections here yet. } else { // this is a primitive property, assign results to dictionary. currentEntity[propertyName] = property.Value; } }