예제 #1
0
        /// <summary>
        /// Writes the given deltaDeletedEntry specified by the parameter graph as a part of an existing OData message using the given
        /// messageWriter and the writeContext.
        /// </summary>
        /// <param name="graph">The object to be written.</param>
        /// <param name="writer">The <see cref="ODataDeltaWriter" /> to be used for writing.</param>
        /// <param name="writeContext">The <see cref="ODataSerializerContext"/>.</param>
        public virtual async Task WriteDeletedResourceAsync(object graph, ODataWriter writer, ODataSerializerContext writeContext)
        {
            if (writer == null)
            {
                throw Error.ArgumentNull(nameof(writer));
            }

            EdmDeltaDeletedResourceObject edmDeltaDeletedEntity = graph as EdmDeltaDeletedResourceObject;

            if (edmDeltaDeletedEntity == null)
            {
                throw new SerializationException(Error.Format(SRResources.CannotWriteType, GetType().Name, graph?.GetType().FullName));
            }

            Uri id = edmDeltaDeletedEntity.Id;
            DeltaDeletedEntryReason reason          = edmDeltaDeletedEntity.Reason ?? DeltaDeletedEntryReason.Deleted;
            ODataDeletedResource    deletedResource = new ODataDeletedResource(id, reason);

            if (edmDeltaDeletedEntity.NavigationSource != null)
            {
                ODataResourceSerializationInfo serializationInfo = new ODataResourceSerializationInfo
                {
                    NavigationSourceName = edmDeltaDeletedEntity.NavigationSource.Name
                };
                deletedResource.SetSerializationInfo(serializationInfo);
            }

            if (deletedResource != null)
            {
                await writer.WriteStartAsync(deletedResource).ConfigureAwait(false);

                await writer.WriteEndAsync().ConfigureAwait(false);
            }
        }
        public IActionResult Get()
        {
            EdmChangedObjectCollection changedCollection = new EdmChangedObjectCollection(DeltaCustomerType);

            //Changed or Modified objects are represented as EdmDeltaResourceObjects
            for (int i = 0; i < 10; i++)
            {
                dynamic typelessCustomer = new EdmDeltaResourceObject(DeltaCustomerType);
                typelessCustomer.Id              = i;
                typelessCustomer.Name            = string.Format("Name {0}", i);
                typelessCustomer.FavoriteNumbers = Enumerable.Range(0, i).ToArray();
                changedCollection.Add(typelessCustomer);
            }

            //Deleted objects are represented as EdmDeltaDeletedObjects
            for (int i = 10; i < 15; i++)
            {
                dynamic typelessCustomer = new EdmDeltaDeletedResourceObject(DeltaCustomerType);
                typelessCustomer.Id     = new Uri(i.ToString(), UriKind.RelativeOrAbsolute);
                typelessCustomer.Reason = DeltaDeletedEntryReason.Deleted;
                changedCollection.Add(typelessCustomer);
            }

            return(Ok(changedCollection));
        }
예제 #3
0
        public void ReadDeltaResource_Returns_DeletedResource(bool typed)
        {
            // Arrange
            IEdmModel      model    = GetEdmModel();
            IEdmEntityType customer = model.SchemaElements.OfType <IEdmEntityType>().First(c => c.Name == "Customer");
            IEdmStructuredTypeReference elementType = new EdmEntityTypeReference(customer, true);

            Mock <IODataDeserializerProvider> deserializerProvider = new Mock <IODataDeserializerProvider>();
            ODataResourceDeserializer         resourceDeserializer = new ODataResourceDeserializer(deserializerProvider.Object);

            Uri id = new Uri("Customers(8)", UriKind.RelativeOrAbsolute);
            ODataDeletedResource customerDeleted = new ODataDeletedResource(id, DeltaDeletedEntryReason.Deleted)
            {
                Properties = new List <ODataProperty>
                {
                    new ODataProperty {
                        Name = "FirstName", Value = "Peter"
                    },
                    new ODataProperty {
                        Name = "LastName", Value = "John"
                    }
                }
            };
            ODataResourceWrapper     resourceWrapper = new ODataResourceWrapper(customerDeleted);
            ODataDeserializerContext context         = new ODataDeserializerContext
            {
                Model = model,
            };

            if (typed)
            {
                context.ResourceType = typeof(DeltaSet <>);
            }
            else
            {
                context.ResourceType = typeof(EdmChangedObjectCollection);
            }

            deserializerProvider.Setup(d => d.GetEdmTypeDeserializer(It.IsAny <IEdmTypeReference>(), false)).Returns(resourceDeserializer);
            ODataDeltaResourceSetDeserializer deserializer = new ODataDeltaResourceSetDeserializer(deserializerProvider.Object);

            // Act
            object result = deserializer.ReadDeltaResource(resourceWrapper, elementType, context);

            // Assert
            Action <Delta> testPropertyAction = d =>
            {
                d.TryGetPropertyValue("FirstName", out object firstName);
                Assert.Equal("Peter", firstName);
                d.TryGetPropertyValue("LastName", out object lastName);
                Assert.Equal("John", lastName);
            };

            if (typed)
            {
                DeltaDeletedResource <Customer> deltaDeletedResource = Assert.IsType <DeltaDeletedResource <Customer> >(result);
                Assert.Equal(id, deltaDeletedResource.Id);
                Assert.Equal(DeltaDeletedEntryReason.Deleted, deltaDeletedResource.Reason);
                testPropertyAction(deltaDeletedResource);
            }
            else
            {
                EdmDeltaDeletedResourceObject deltaDeletedResource = Assert.IsType <EdmDeltaDeletedResourceObject>(result);
                Assert.Equal(id, deltaDeletedResource.Id);
                Assert.Equal(DeltaDeletedEntryReason.Deleted, deltaDeletedResource.Reason);
                testPropertyAction(deltaDeletedResource);
            }
        }