/// <summary> /// Applies the values of a nested <paramref name="feed"/> to the collection /// <paramref name="property"/> of the specified <paramref name="entry"/>. /// </summary> /// <param name="entry">Entry with collection to be modified.</param> /// <param name="property">Collection property on the entry.</param> /// <param name="feed">Values to apply onto the collection.</param> /// <param name="includeLinks">Whether links that are expanded should be materialized.</param> private void ApplyFeedToCollection( MaterializerEntry entry, ClientPropertyAnnotation property, ODataFeed feed, bool includeLinks) { Debug.Assert(entry.Entry != null, "entry != null"); Debug.Assert(property != null, "property != null"); Debug.Assert(feed != null, "feed != null"); ClientEdmModel edmModel = this.MaterializerContext.Model; ClientTypeAnnotation collectionType = edmModel.GetClientTypeAnnotation(edmModel.GetOrCreateEdmType(property.EntityCollectionItemType)); IEnumerable <ODataEntry> entries = MaterializerFeed.GetFeed(feed).Entries; foreach (ODataEntry feedEntry in entries) { this.Materialize(MaterializerEntry.GetEntry(feedEntry), collectionType.ElementType, includeLinks); } ProjectionPlan continuationPlan = includeLinks ? ODataEntityMaterializer.CreatePlanForDirectMaterialization(property.EntityCollectionItemType) : ODataEntityMaterializer.CreatePlanForShallowMaterialization(property.EntityCollectionItemType); this.ApplyItemsToCollection( entry, property, entries.Select(e => MaterializerEntry.GetEntry(e).ResolvedObject), feed.NextPageLink, continuationPlan, false); }
/// <summary> /// Read a feed or entry, with the expected type. /// </summary> /// <returns>true if a value was read, otherwise false</returns> public bool Read() { if (this.feedEntries != null) { // ISSUE: this might throw - refactor? if (this.feedEntries.MoveNext()) { this.currentEntry = this.feedEntries.Current; return(true); } else { this.feedEntries = null; this.currentEntry = null; } } switch (this.reader.State) { case ODataReaderState.Completed: this.currentEntry = null; return(false); case ODataReaderState.Start: { ODataFeed feed; MaterializerEntry entryAndState; if (this.TryReadFeedOrEntry(true, out feed, out entryAndState)) { this.currentEntry = entryAndState != null ? entryAndState.Entry : null; this.currentFeed = feed; if (this.currentFeed != null) { this.feedEntries = MaterializerFeed.GetFeed(this.currentFeed).Entries.GetEnumerator(); } return(true); } else { throw new NotImplementedException(); } } case ODataReaderState.FeedEnd: case ODataReaderState.EntryEnd: if (this.TryRead() || this.reader.State != ODataReaderState.Completed) { throw DSClient.Error.InternalError(InternalError.UnexpectedReadState); } this.currentEntry = null; return(false); default: throw DSClient.Error.InternalError(InternalError.UnexpectedReadState); } }
public bool Read() { if (this.feedEntries != null) { if (this.feedEntries.MoveNext()) { this.currentEntry = this.feedEntries.Current; return(true); } this.feedEntries = null; this.currentEntry = null; } ODataReaderState state = this.reader.State; switch (state) { case ODataReaderState.Start: ODataFeed feed; MaterializerEntry entry; if (!this.TryReadFeedOrEntry(true, out feed, out entry)) { throw new NotImplementedException(); } this.currentEntry = (entry != null) ? entry.Entry : null; this.currentFeed = feed; if (this.currentFeed != null) { this.feedEntries = MaterializerFeed.GetFeed(this.currentFeed).Entries.GetEnumerator(); } return(true); case ODataReaderState.FeedStart: case ODataReaderState.EntryStart: break; case ODataReaderState.FeedEnd: case ODataReaderState.EntryEnd: if (this.TryRead() || (this.reader.State != ODataReaderState.Completed)) { throw System.Data.Services.Client.Error.InternalError(InternalError.UnexpectedReadState); } this.currentEntry = null; return(false); default: if (state != ODataReaderState.Completed) { break; } this.currentEntry = null; return(false); } throw System.Data.Services.Client.Error.InternalError(InternalError.UnexpectedReadState); }
public long GetCountValue(bool readIfNoFeed) { if (((this.currentFeed == null) && (this.currentEntry == null)) && (readIfNoFeed && this.TryReadFeed(true, out this.currentFeed))) { this.feedEntries = MaterializerFeed.GetFeed(this.currentFeed).Entries.GetEnumerator(); } if ((this.currentFeed == null) || !this.currentFeed.Count.HasValue) { throw new InvalidOperationException(System.Data.Services.Client.Strings.MaterializeFromAtom_CountNotPresent); } return(this.currentFeed.Count.Value); }
private void ApplyFeedToCollection(MaterializerEntry entry, ClientPropertyAnnotation property, ODataFeed feed, bool includeLinks) { ClientEdmModel model = ClientEdmModel.GetModel(base.ResponseInfo.MaxProtocolVersion); ClientTypeAnnotation clientTypeAnnotation = model.GetClientTypeAnnotation(model.GetOrCreateEdmType(property.EntityCollectionItemType)); IEnumerable <ODataEntry> entries = MaterializerFeed.GetFeed(feed).Entries; foreach (ODataEntry entry2 in entries) { this.Materialize(MaterializerEntry.GetEntry(entry2), clientTypeAnnotation.ElementType, includeLinks); } ProjectionPlan continuationPlan = includeLinks ? CreatePlanForDirectMaterialization(property.EntityCollectionItemType) : CreatePlanForShallowMaterialization(property.EntityCollectionItemType); this.ApplyItemsToCollection(entry, property, from e in entries select MaterializerEntry.GetEntry(e).ResolvedObject, feed.NextPageLink, continuationPlan); }
/// <summary> /// The count tag's value, if requested /// </summary> /// <param name="readIfNoFeed">Should read pull if no feed exists.</param> /// <returns>The count value returned from the server</returns> public long GetCountValue(bool readIfNoFeed) { if (this.currentFeed == null && this.currentEntry == null && readIfNoFeed && this.TryReadFeed(true, out this.currentFeed)) { this.feedEntries = MaterializerFeed.GetFeed(this.currentFeed).Entries.GetEnumerator(); } if (this.currentFeed != null && this.currentFeed.Count.HasValue) { return(this.currentFeed.Count.Value); } throw new InvalidOperationException(DSClient.Strings.MaterializeFromAtom_CountNotPresent); }
internal static IEnumerable ProjectionSelect(ODataEntityMaterializer materializer, MaterializerEntry entry, Type expectedType, Type resultType, ProjectionPath path, Func <object, object, Type, object> selector) { ClientEdmModel model = ClientEdmModel.GetModel(materializer.ResponseInfo.MaxProtocolVersion); ClientTypeAnnotation clientTypeAnnotation = entry.ActualType ?? model.GetClientTypeAnnotation(model.GetOrCreateEdmType(expectedType)); IEnumerable enumerable = (IEnumerable)Util.ActivatorCreateInstance(typeof(List <>).MakeGenericType(new Type[] { resultType }), new object[0]); MaterializerNavigationLink link = null; ClientPropertyAnnotation property = null; for (int i = 0; i < path.Count; i++) { ProjectionPathSegment segment = path[i]; if (segment.SourceTypeAs != null) { clientTypeAnnotation = model.GetClientTypeAnnotation(model.GetOrCreateEdmType(segment.SourceTypeAs)); } if (segment.Member != null) { string member = segment.Member; property = clientTypeAnnotation.GetProperty(member, false); link = GetPropertyOrThrow(entry.NavigationLinks, member, entry.Id); if (link.Entry != null) { entry = link.Entry; clientTypeAnnotation = model.GetClientTypeAnnotation(model.GetOrCreateEdmType(property.PropertyType)); } } } ValidatePropertyMatch(property, link.Link); MaterializerFeed feed = MaterializerFeed.GetFeed(link.Feed); Action <object, object> addToCollectionDelegate = ODataMaterializer.GetAddToCollectionDelegate(enumerable.GetType()); foreach (ODataEntry entry2 in feed.Entries) { object obj2 = selector(materializer, entry2, property.EntityCollectionItemType); addToCollectionDelegate(enumerable, obj2); } ProjectionPlan plan = new ProjectionPlan { LastSegmentType = property.EntityCollectionItemType, Plan = selector, ProjectedType = resultType }; materializer.FoundNextLinkForCollection(enumerable, feed.NextPageLink, plan); return(enumerable); }
internal static object ProjectionValueForPath(ODataEntityMaterializer materializer, MaterializerEntry entry, Type expectedType, ProjectionPath path) { if ((path.Count == 0) || ((path.Count == 1) && (path[0].Member == null))) { if (!entry.EntityHasBeenResolved) { materializer.Materialize(entry, expectedType, false); } return(entry.ResolvedObject); } object streamLink = null; ODataNavigationLink link = null; ODataProperty atomProperty = null; ICollection <ODataNavigationLink> navigationLinks = entry.NavigationLinks; IEnumerable <ODataProperty> properties = entry.Entry.Properties; ClientEdmModel model = ClientEdmModel.GetModel(materializer.ResponseInfo.MaxProtocolVersion); for (int i = 0; i < path.Count; i++) { Func <StreamDescriptor, bool> predicate = null; Func <ODataNavigationLink, bool> func2 = null; Func <ODataProperty, bool> func3 = null; Func <ODataProperty, bool> func4 = null; Func <ODataNavigationLink, bool> func5 = null; string propertyName; ProjectionPathSegment segment = path[i]; if (segment.Member != null) { bool flag = i == (path.Count - 1); propertyName = segment.Member; expectedType = segment.SourceTypeAs ?? expectedType; ClientPropertyAnnotation property = model.GetClientTypeAnnotation(model.GetOrCreateEdmType(expectedType)).GetProperty(propertyName, false); if (property.IsStreamLinkProperty) { if (predicate == null) { predicate = sd => sd.Name == propertyName; } StreamDescriptor descriptor = entry.EntityDescriptor.StreamDescriptors.Where <StreamDescriptor>(predicate).SingleOrDefault <StreamDescriptor>(); if (descriptor == null) { if (segment.SourceTypeAs == null) { throw new InvalidOperationException(System.Data.Services.Client.Strings.AtomMaterializer_PropertyMissing(propertyName, entry.Entry.Id)); } return(WebUtil.GetDefaultValue <DataServiceStreamLink>()); } streamLink = descriptor.StreamLink; } else { if (segment.SourceTypeAs != null) { if (func2 == null) { func2 = p => p.Name == propertyName; } if (!navigationLinks.Any <ODataNavigationLink>(func2)) { if (func3 == null) { func3 = p => p.Name == propertyName; } if (!properties.Any <ODataProperty>(func3) && flag) { return(WebUtil.GetDefaultValue(property.PropertyType)); } } } if (func4 == null) { func4 = p => p.Name == propertyName; } atomProperty = properties.Where <ODataProperty>(func4).FirstOrDefault <ODataProperty>(); if (func5 == null) { func5 = p => p.Name == propertyName; } link = ((atomProperty == null) && (navigationLinks != null)) ? navigationLinks.Where <ODataNavigationLink>(func5).FirstOrDefault <ODataNavigationLink>() : null; if ((link == null) && (atomProperty == null)) { throw new InvalidOperationException(System.Data.Services.Client.Strings.AtomMaterializer_PropertyMissing(propertyName, entry.Entry.Id)); } if (link != null) { ValidatePropertyMatch(property, link); MaterializerNavigationLink link2 = MaterializerNavigationLink.GetLink(link); if (link2.Feed != null) { MaterializerFeed feed = MaterializerFeed.GetFeed(link2.Feed); Type implementationType = ClientTypeUtil.GetImplementationType(segment.ProjectionType, typeof(ICollection <>)); if (implementationType == null) { implementationType = ClientTypeUtil.GetImplementationType(segment.ProjectionType, typeof(IEnumerable <>)); } Type nestedExpectedType = implementationType.GetGenericArguments()[0]; Type projectionType = segment.ProjectionType; if (projectionType.IsInterfaceEx() || ODataMaterializer.IsDataServiceCollection(projectionType)) { projectionType = typeof(Collection <>).MakeGenericType(new Type[] { nestedExpectedType }); } IEnumerable list = (IEnumerable)Util.ActivatorCreateInstance(projectionType, new object[0]); MaterializeToList(materializer, list, nestedExpectedType, feed.Entries); if (ODataMaterializer.IsDataServiceCollection(segment.ProjectionType)) { list = (IEnumerable)Util.ActivatorCreateInstance(WebUtil.GetDataServiceCollectionOfT(new Type[] { nestedExpectedType }), new object[] { list, TrackingMode.None }); } ProjectionPlan plan = CreatePlanForShallowMaterialization(nestedExpectedType); materializer.FoundNextLinkForCollection(list, feed.Feed.NextPageLink, plan); streamLink = list; } else if (link2.Entry != null) { MaterializerEntry entry2 = link2.Entry; if (flag) { if ((entry2.Entry != null) && !entry2.EntityHasBeenResolved) { materializer.Materialize(entry2, property.PropertyType, false); } } else { CheckEntryToAccessNotNull(entry2, propertyName); } properties = entry2.Properties; navigationLinks = entry2.NavigationLinks; streamLink = entry2.ResolvedObject; entry = entry2; } } else { if (atomProperty.Value is ODataStreamReferenceValue) { streamLink = null; navigationLinks = ODataMaterializer.EmptyLinks; properties = ODataMaterializer.EmptyProperties; continue; } ValidatePropertyMatch(property, atomProperty); if (ClientTypeUtil.TypeOrElementTypeIsEntity(property.PropertyType)) { throw System.Data.Services.Client.Error.InvalidOperation(System.Data.Services.Client.Strings.AtomMaterializer_InvalidEntityType(property.EntityCollectionItemType ?? property.PropertyType)); } if (property.IsPrimitiveOrComplexCollection) { object instance = streamLink ?? (entry.ResolvedObject ?? Util.ActivatorCreateInstance(expectedType, new object[0])); ODataMaterializer.ApplyDataValue(model.GetClientTypeAnnotation(model.GetOrCreateEdmType(instance.GetType())), atomProperty, materializer.ResponseInfo.IgnoreMissingProperties, materializer.ResponseInfo, instance); navigationLinks = ODataMaterializer.EmptyLinks; properties = ODataMaterializer.EmptyProperties; } else if (atomProperty.Value is ODataComplexValue) { ODataComplexValue complexValue = atomProperty.Value as ODataComplexValue; ODataMaterializer.MaterializeComplexTypeProperty(property.PropertyType, complexValue, materializer.ResponseInfo.IgnoreMissingProperties, materializer.ResponseInfo); properties = complexValue.Properties; navigationLinks = ODataMaterializer.EmptyLinks; } else { if ((atomProperty.Value == null) && !ClientTypeUtil.CanAssignNull(property.NullablePropertyType)) { throw new InvalidOperationException(System.Data.Services.Client.Strings.AtomMaterializer_CannotAssignNull(atomProperty.Name, property.NullablePropertyType)); } ODataMaterializer.MaterializePrimitiveDataValue(property.NullablePropertyType, atomProperty); navigationLinks = ODataMaterializer.EmptyLinks; properties = ODataMaterializer.EmptyProperties; } streamLink = atomProperty.GetMaterializedValue(); } } expectedType = property.PropertyType; } } return(streamLink); }