private async Task WriteDeltaResourceSetAsync(IEnumerable enumerable, IEdmTypeReference feedType, ODataWriter writer,
                                                      ODataSerializerContext writeContext)
        {
            Contract.Assert(writer != null);
            Contract.Assert(writeContext != null);
            Contract.Assert(enumerable != null);
            Contract.Assert(feedType != null);

            IEdmStructuredTypeReference elementType = GetResourceType(feedType);

            if (elementType.IsComplex())
            {
                ODataResourceSet resourceSet = new ODataResourceSet()
                {
                    TypeName = feedType.FullName()
                };

                await writer.WriteStartAsync(resourceSet).ConfigureAwait(false);

                ODataResourceSerializer entrySerializer = SerializerProvider.GetEdmTypeSerializer(elementType) as ODataResourceSerializer;
                if (entrySerializer == null)
                {
                    throw new SerializationException(
                              Error.Format(SRResources.TypeCannotBeSerialized, elementType.FullName()));
                }

                foreach (object entry in enumerable)
                {
                    await entrySerializer.WriteDeltaObjectInlineAsync(entry, elementType, writer, writeContext).ConfigureAwait(false);
                }
            }
            else
            {
                ODataDeltaResourceSet deltaResourceSet = CreateODataDeltaResourceSet(enumerable, feedType.AsCollection(), writeContext);
                if (deltaResourceSet == null)
                {
                    throw new SerializationException(Error.Format(SRResources.CannotSerializerNull, DeltaResourceSet));
                }

                // save the next page link for later to support JSON odata.streaming.
                Func <object, Uri> nextLinkGenerator = GetNextLinkGenerator(deltaResourceSet, enumerable, writeContext);
                deltaResourceSet.NextPageLink = null;

                //Start writing of the Delta Feed
                await writer.WriteStartAsync(deltaResourceSet).ConfigureAwait(false);

                object lastResource = null;
                //Iterate over all the entries present and select the appropriate write method.
                //Write method creates ODataDeltaDeletedEntry / ODataDeltaDeletedLink / ODataDeltaLink or ODataEntry.
                foreach (object item in enumerable)
                {
                    if (item == null)
                    {
                        throw new SerializationException(SRResources.NullElementInCollection);
                    }

                    lastResource = item;
                    DeltaItemKind kind = GetDelteItemKind(item);
                    switch (kind)
                    {
                    case DeltaItemKind.DeletedResource:
                        await WriteDeltaDeletedResourceAsync(item, writer, writeContext).ConfigureAwait(false);

                        break;

                    case DeltaItemKind.DeltaDeletedLink:
                        await WriteDeltaDeletedLinkAsync(item, writer, writeContext).ConfigureAwait(false);

                        break;

                    case DeltaItemKind.DeltaLink:
                        await WriteDeltaLinkAsync(item, writer, writeContext).ConfigureAwait(false);

                        break;

                    case DeltaItemKind.Resource:
                    {
                        ODataResourceSerializer entrySerializer = SerializerProvider.GetEdmTypeSerializer(elementType) as ODataResourceSerializer;
                        if (entrySerializer == null)
                        {
                            throw new SerializationException(
                                      Error.Format(SRResources.TypeCannotBeSerialized, elementType.FullName()));
                        }
                        await entrySerializer.WriteDeltaObjectInlineAsync(item, elementType, writer, writeContext)
                        .ConfigureAwait(false);

                        break;
                    }

                    default:
                        break;
                    }
                }

                // Subtle and surprising behavior: If the NextPageLink property is set before calling WriteStart(feed),
                // the next page link will be written early in a manner not compatible with odata.streaming=true. Instead, if
                // the next page link is not set when calling WriteStart(feed) but is instead set later on that feed
                // object before calling WriteEnd(), the next page link will be written at the end, as required for
                // odata.streaming=true support.

                deltaResourceSet.NextPageLink = nextLinkGenerator(lastResource);
            }

            //End Writing of the Delta Feed
            await writer.WriteEndAsync().ConfigureAwait(false);
        }
Exemple #2
0
 internal EdmDeltaType(IEdmEntityType entityType, DeltaItemKind deltaKind)
 {
     EntityType = entityType ?? throw Error.ArgumentNull(nameof(entityType));
     DeltaKind  = deltaKind;
 }
Exemple #3
0
 internal EdmDeltaType(IEdmEntityType entityType, DeltaItemKind deltaKind)
 {
     _entityType = entityType ?? throw Error.ArgumentNull("entityType");
     _deltaKind  = deltaKind;
 }