예제 #1
0
        private async Task WriteOperationParameterAsync(ODataParameterWriter parameterWriter, IEdmOperationParameter operationParameter, string paramName, object paramValue)
        {
            switch (operationParameter.Type.Definition.TypeKind)
            {
            case EdmTypeKind.Primitive:
                var value = GetPropertyValue(operationParameter.Type, paramValue, null);
                await parameterWriter.WriteValueAsync(paramName, value).ConfigureAwait(false);

                break;

            case EdmTypeKind.Enum:
                await parameterWriter.WriteValueAsync(paramName, new ODataEnumValue(paramValue.ToString())).ConfigureAwait(false);

                break;

            case EdmTypeKind.Untyped:
                await parameterWriter.WriteValueAsync(paramName, new ODataUntypedValue { RawValue = paramValue.ToString() }).ConfigureAwait(false);

                break;

            case EdmTypeKind.Entity:
            {
                var entryWriter = await parameterWriter.CreateResourceWriterAsync(paramName).ConfigureAwait(false);

                var paramValueDict = paramValue.ToDictionary(TypeCache);
                var contentId      = _deferredBatchWriter?.Value.GetContentId(paramValueDict, null);

                var typeName = operationParameter.Type.Definition.FullTypeName();
                if (paramValueDict.ContainsKey("@odata.type") && paramValueDict["@odata.type"] is string)
                {
                    typeName = paramValueDict["@odata.type"] as string;
                    paramValueDict.Remove("@odata.type");
                }

                var entryDetails = _session.Metadata.ParseEntryDetails(typeName, paramValueDict, contentId);
                var entry        = CreateODataEntry(typeName, entryDetails.Properties, null);

                RegisterRootEntry(entry);
                await WriteEntryPropertiesAsync(entryWriter, entry, entryDetails.Links).ConfigureAwait(false);

                UnregisterRootEntry(entry);
            }
            break;

            case EdmTypeKind.Complex:
            {
                var entryWriter = await parameterWriter.CreateResourceWriterAsync(paramName).ConfigureAwait(false);

                var paramValueDict = paramValue.ToDictionary(TypeCache);

                var typeName = operationParameter.Type.Definition.FullTypeName();
                if (paramValueDict.ContainsKey("@odata.type") && paramValueDict["@odata.type"] is string)
                {
                    typeName = paramValueDict["@odata.type"] as string;
                    paramValueDict.Remove("@odata.type");
                }

                var entry = CreateODataEntry(typeName, paramValueDict, null);

                RegisterRootEntry(entry);
                await WriteEntryPropertiesAsync(entryWriter, entry, new Dictionary <string, List <ReferenceLink> >()).ConfigureAwait(false);

                UnregisterRootEntry(entry);
            }
            break;

            case EdmTypeKind.Collection:
                var collectionType = operationParameter.Type.Definition as IEdmCollectionType;
                var elementType    = collectionType.ElementType;
                if (elementType.Definition.TypeKind == EdmTypeKind.Entity)
                {
                    var feedWriter = await parameterWriter.CreateResourceSetWriterAsync(paramName).ConfigureAwait(false);

                    var feed = new ODataResourceSet();
                    await feedWriter.WriteStartAsync(feed).ConfigureAwait(false);

                    foreach (var item in (IEnumerable)paramValue)
                    {
                        var feedEntry = CreateODataEntry(elementType.Definition.FullTypeName(), item.ToDictionary(TypeCache), null);

                        RegisterRootEntry(feedEntry);
                        await feedWriter.WriteStartAsync(feedEntry).ConfigureAwait(false);

                        await feedWriter.WriteEndAsync().ConfigureAwait(false);

                        UnregisterRootEntry(feedEntry);
                    }
                    await feedWriter.WriteEndAsync().ConfigureAwait(false);
                }
                else
                {
                    var collectionWriter = await parameterWriter.CreateCollectionWriterAsync(paramName).ConfigureAwait(false);

                    await collectionWriter.WriteStartAsync(new ODataCollectionStart()).ConfigureAwait(false);

                    foreach (var item in (IEnumerable)paramValue)
                    {
                        await collectionWriter.WriteItemAsync(item).ConfigureAwait(false);
                    }
                    await collectionWriter.WriteEndAsync().ConfigureAwait(false);
                }
                break;

            default:
                throw new NotSupportedException($"Unable to write action parameter of a type {operationParameter.Type.Definition.TypeKind}");
            }
        }
        private async Task WriteOperationParameterAsync(ODataParameterWriter parameterWriter, IEdmOperationParameter operationParameter, string paramName, object paramValue)
        {
            switch (operationParameter.Type.Definition.TypeKind)
            {
            case EdmTypeKind.Primitive:
            case EdmTypeKind.Complex:
                var value = GetPropertyValue(operationParameter.Type, paramValue, null);
                await parameterWriter.WriteValueAsync(paramName, value).ConfigureAwait(false);

                break;

            case EdmTypeKind.Enum:
                await parameterWriter.WriteValueAsync(paramName, new ODataEnumValue(paramValue.ToString())).ConfigureAwait(false);

                break;

            case EdmTypeKind.Untyped:
                await parameterWriter.WriteValueAsync(paramName, new ODataUntypedValue { RawValue = paramValue.ToString() }).ConfigureAwait(false);

                break;

            case EdmTypeKind.Entity:
                var entryWriter = await parameterWriter.CreateResourceWriterAsync(paramName).ConfigureAwait(false);

                var entry = CreateODataEntry(operationParameter.Type.Definition.FullTypeName(), paramValue.ToDictionary(), null);

                RegisterRootEntry(entry);
                await entryWriter.WriteStartAsync(entry).ConfigureAwait(false);

                await entryWriter.WriteEndAsync().ConfigureAwait(false);

                UnregisterRootEntry(entry);

                break;

            case EdmTypeKind.Collection:
                var collectionType = operationParameter.Type.Definition as IEdmCollectionType;
                var elementType    = collectionType.ElementType;
                if (elementType.Definition.TypeKind == EdmTypeKind.Entity)
                {
                    var feedWriter = await parameterWriter.CreateResourceSetWriterAsync(paramName).ConfigureAwait(false);

                    var feed = new ODataResourceSet();
                    await feedWriter.WriteStartAsync(feed).ConfigureAwait(false);

                    foreach (var item in (IEnumerable)paramValue)
                    {
                        var feedEntry = CreateODataEntry(elementType.Definition.FullTypeName(), item.ToDictionary(), null);

                        RegisterRootEntry(feedEntry);
                        await feedWriter.WriteStartAsync(feedEntry).ConfigureAwait(false);

                        await feedWriter.WriteEndAsync().ConfigureAwait(false);

                        UnregisterRootEntry(feedEntry);
                    }
                    await feedWriter.WriteEndAsync().ConfigureAwait(false);
                }
                else
                {
                    var collectionWriter = await parameterWriter.CreateCollectionWriterAsync(paramName).ConfigureAwait(false);

                    await collectionWriter.WriteStartAsync(new ODataCollectionStart()).ConfigureAwait(false);

                    foreach (var item in (IEnumerable)paramValue)
                    {
                        await collectionWriter.WriteItemAsync(item).ConfigureAwait(false);
                    }
                    await collectionWriter.WriteEndAsync().ConfigureAwait(false);
                }
                break;

            default:
                throw new NotSupportedException($"Unable to write action parameter of a type {operationParameter.Type.Definition.TypeKind}");
            }
        }