private void ApplyResponseToDescriptor(DescriptorData descriptor, HttpResponseData response) { var entityDescriptor = descriptor as EntityDescriptorData; var streamDescriptor = descriptor as StreamDescriptorData; if (entityDescriptor != null) { if (entityDescriptor.IsMediaLinkEntry && entityDescriptor.DefaultStreamState == EntityStates.Modified) { // in this case (and only this case), the response headers apply to the stream itself entityDescriptor.DefaultStreamDescriptor.UpdateFromHeaders(response.Headers); } else { entityDescriptor.UpdateFromHeaders(response.Headers); } if (response.StatusCode != HttpStatusCode.NoContent) { var entityInstance = response.DeserializeAndCast <EntityInstance>(this.parent.FormatSelector); entityDescriptor.UpdateFromPayload(entityInstance, this.contextData.BaseUri); } if (entityDescriptor.IsMediaLinkEntry && entityDescriptor.DefaultStreamState != EntityStates.Unchanged) { this.VerifyStreamClosed(entityDescriptor.DefaultStreamDescriptor); } } else { if (streamDescriptor != null) { streamDescriptor.UpdateFromHeaders(response.Headers); this.VerifyStreamClosed(streamDescriptor); } else { ExceptionUtilities.Assert(descriptor is LinkDescriptorData, "Descriptor is of unexpected type '{0}'", descriptor.GetType()); } } }
private void UpdateDescriptorState(DescriptorData descriptor) { var entityDescriptor = descriptor as EntityDescriptorData; var streamDescriptor = descriptor as StreamDescriptorData; if (entityDescriptor != null) { var initialState = entityDescriptor.State; if (entityDescriptor.State == EntityStates.Deleted) { this.contextData.RemoveDescriptorData(entityDescriptor); foreach (var link in this.contextData.LinkDescriptorsData.Where(l => l.SourceDescriptor == entityDescriptor || l.TargetDescriptor == entityDescriptor).ToList()) { this.contextData.RemoveDescriptorData(link); } } else { if (entityDescriptor.State == EntityStates.Added) { this.contextData.LinkDescriptorsData .Where(l => l.SourceDescriptor == entityDescriptor && l.State != EntityStates.Deleted && l.TargetDescriptor.State != EntityStates.Added) .ForEach(l => l.State = EntityStates.Unchanged); if (entityDescriptor.ParentForInsert != null) { var parentLink = this.contextData.LinkDescriptorsData .SingleOrDefault(l => l.SourceDescriptor.Entity == entityDescriptor.ParentForInsert && l.SourcePropertyName == entityDescriptor.ParentPropertyForInsert && l.TargetDescriptor == descriptor); ExceptionUtilities.CheckObjectNotNull(parentLink, "Could not find parent link descriptor for entity descriptor: '{0}'", entityDescriptor); ExceptionUtilities.Assert(parentLink.State == EntityStates.Added, "Parent link for entity descriptor '{0}' was not in the added state", entityDescriptor); parentLink.State = EntityStates.Unchanged; } entityDescriptor.InsertLink = null; entityDescriptor.ParentForInsert = null; entityDescriptor.ParentPropertyForInsert = null; } entityDescriptor.State = EntityStates.Unchanged; } if (entityDescriptor.IsMediaLinkEntry) { if (entityDescriptor.DefaultStreamState == EntityStates.Added) { // the stream insert should always leave the entity in modified state, so that the properties are updated entityDescriptor.State = EntityStates.Modified; } else if (entityDescriptor.DefaultStreamState == EntityStates.Modified) { // if the stream was being updated, the entity's state should not have changed entityDescriptor.State = initialState; } entityDescriptor.DefaultStreamState = EntityStates.Unchanged; } } else { if (streamDescriptor == null) { ExceptionUtilities.CheckObjectNotNull(descriptor as LinkDescriptorData, "Descriptor is of unexpected type '{0}'", descriptor.GetType()); } if (descriptor.State == EntityStates.Deleted) { this.contextData.RemoveDescriptorData(descriptor); } else { descriptor.State = EntityStates.Unchanged; } } }