Ejemplo n.º 1
0
        private Expression[] ProcessActionInvokePostBody(IODataRequestMessage message, IEdmOperation operation)
        {
            using (var messageReader = new ODataMessageReader(message, this.GetReaderSettings()))
            {
                List <Expression> parameterValues = new List <Expression>();
                var parameterReader = messageReader.CreateODataParameterReader(operation);

                while (parameterReader.Read())
                {
                    switch (parameterReader.State)
                    {
                    case ODataParameterReaderState.Value:
                    {
                        object clrValue = ODataObjectModelConverter.ConvertPropertyValue(parameterReader.Value);
                        parameterValues.Add(Expression.Constant(clrValue));
                        break;
                    }

                    case ODataParameterReaderState.Collection:
                    {
                        ODataCollectionReader collectionReader = parameterReader.CreateCollectionReader();
                        object clrValue = ODataObjectModelConverter.ConvertPropertyValue(ODataObjectModelConverter.ReadCollectionParameterValue(collectionReader));
                        parameterValues.Add(Expression.Constant(clrValue, clrValue.GetType()));
                        break;
                    }

                    case ODataParameterReaderState.Resource:
                    {
                        var    entryReader = parameterReader.CreateResourceReader();
                        object clrValue    = ODataObjectModelConverter.ReadEntityOrEntityCollection(entryReader, false);
                        parameterValues.Add(Expression.Constant(clrValue, clrValue.GetType()));
                        break;
                    }

                    case ODataParameterReaderState.ResourceSet:
                    {
                        var feedReader     = parameterReader.CreateResourceSetReader();
                        var collectionList = ODataObjectModelConverter.ReadEntityOrEntityCollection(feedReader, true);
                        parameterValues.Add(Expression.Constant(collectionList, collectionList.GetType()));
                        break;
                    }
                    }
                }

                return(parameterValues.ToArray());
            }
        }
Ejemplo n.º 2
0
        private Expression[] ProcessActionInvokePostBody(IODataRequestMessage message, IEdmOperation operation)
        {
            using (var messageReader = new ODataMessageReader(message, this.GetReaderSettings(), this.DataSource.Model))
            {
                List <Expression> parameterValues = new List <Expression>();
                var parameterReader = messageReader.CreateODataParameterReader(operation);

                while (parameterReader.Read())
                {
                    switch (parameterReader.State)
                    {
                    case ODataParameterReaderState.Value:
                    {
                        object clrValue = ODataObjectModelConverter.ConvertPropertyValue(parameterReader.Value);
                        parameterValues.Add(Expression.Constant(clrValue));
                        break;
                    }

                    case ODataParameterReaderState.Collection:
                    {
                        ODataCollectionReader collectionReader = parameterReader.CreateCollectionReader();
                        object clrValue = ODataObjectModelConverter.ConvertPropertyValue(ODataObjectModelConverter.ReadCollectionParameterValue(collectionReader));
                        parameterValues.Add(Expression.Constant(clrValue, clrValue.GetType()));
                        break;
                    }

                    case ODataParameterReaderState.Entry:
                    {
                        var    entryReader = parameterReader.CreateEntryReader();
                        object clrValue    = ODataObjectModelConverter.ConvertPropertyValue(ODataObjectModelConverter.ReadEntryParameterValue(entryReader));
                        parameterValues.Add(Expression.Constant(clrValue, clrValue.GetType()));
                        break;
                    }

                    case ODataParameterReaderState.Feed:
                    {
                        IList collectionList = null;
                        var   feedReader     = parameterReader.CreateFeedReader();
                        while (feedReader.Read())
                        {
                            if (feedReader.State == ODataReaderState.EntryEnd)
                            {
                                object clrItem = ODataObjectModelConverter.ConvertPropertyValue(feedReader.Item);
                                if (collectionList == null)
                                {
                                    Type itemType = clrItem.GetType();
                                    Type listType = typeof(List <>).MakeGenericType(new[] { itemType });
                                    collectionList = (IList)Utility.QuickCreateInstance(listType);
                                }

                                collectionList.Add(clrItem);
                            }
                        }

                        parameterValues.Add(Expression.Constant(collectionList, collectionList.GetType()));
                        break;
                    }
                    }
                }

                return(parameterValues.ToArray());
            }
        }
        private static void UpdateCore(object target, string propertyName, object propertyValue)
        {
            var odataComplexValue    = propertyValue as ODataComplexValue;
            var odataCollectionValue = propertyValue as ODataCollectionValue;
            var odataEnumValue       = propertyValue as ODataEnumValue;
            var odataPrimitiveValue  = propertyValue as ODataPrimitiveValue;

            if (odataComplexValue != null)
            {
                var property = target.GetType().GetProperty(propertyName);
                if (property != null)
                {
                    var value = property.GetValue(target, null);
                    if (value == null)
                    {
                        var valueType    = EdmClrTypeUtils.GetInstanceType(odataComplexValue.TypeName);
                        var propertyType = property.PropertyType;
                        if (valueType.IsSubclassOf(propertyType) || valueType == propertyType)
                        {
                            value = Utility.QuickCreateInstance(valueType);
                        }
                        else
                        {
                            throw new InvalidOperationException(string.Format("{0} is not equal or derived from {1}", valueType, propertyType));
                        }

                        property.SetValue(target, value, null);
                    }

                    foreach (var p in odataComplexValue.Properties)
                    {
                        UpdateCore(value, p.Name, p.Value);
                    }

                    return;
                }
            }
            else if (odataCollectionValue != null)
            {
                var property = target.GetType().GetProperty(propertyName);
                if (property != null)
                {
                    var collection = Utility.QuickCreateInstance(property.PropertyType);

                    foreach (var item in odataCollectionValue.Items)
                    {
                        var itemType = property.PropertyType.GetGenericArguments().Single();
                        odataComplexValue    = item as ODataComplexValue;
                        odataCollectionValue = item as ODataCollectionValue;
                        object collectionItem = null;
                        if (odataComplexValue != null)
                        {
                            // TODO: call Create method to new instance instead
                            collectionItem = Utility.QuickCreateInstance(itemType);
                            foreach (var p in odataComplexValue.Properties)
                            {
                                UpdateCore(collectionItem, p.Name, p.Value);
                            }
                        }
                        else if (odataCollectionValue != null)
                        {
                            // TODO, check should support this type or not
                            throw new NotImplementedException();
                        }
                        else
                        {
                            collectionItem = ODataObjectModelConverter.ConvertPropertyValue(item, itemType);
                        }

                        property.PropertyType.GetMethod("Add").Invoke(collection, new object[] { collectionItem });
                    }

                    property.SetValue(target, collection, null);

                    return;
                }
            }
            else
            {
                var property = target.GetType().GetProperty(propertyName);
                if (property != null)
                {
                    property.SetValue(target, ODataObjectModelConverter.ConvertPropertyValue(propertyValue, property.PropertyType), null);
                    return;
                }
            }

            var openClrObject = target as OpenClrObject;

            if (openClrObject != null)
            {
                var structuredType = (IEdmStructuredType)EdmClrTypeUtils.GetEdmType(DataSourceManager.GetCurrentDataSource().Model, openClrObject);

                //check if the edmType is an open type
                if (structuredType.IsOpen)
                {
                    if (odataCollectionValue != null)
                    {
                        // Collection of Edm.String
                        if (odataCollectionValue.TypeName == "Collection(Edm.String)")
                        {
                            var collection = new Collection <string>();
                            foreach (var it in odataCollectionValue.Items)
                            {
                                collection.Add(it as string);
                            }
                            openClrObject.OpenProperties[propertyName] = collection;
                        }
                        else
                        {
                            // TODO: handle other types.
                            throw new NotImplementedException();
                        }
                    }
                    else if (odataComplexValue != null)
                    {
                        var type  = EdmClrTypeUtils.GetInstanceType(odataComplexValue.TypeName);
                        var value = Utility.QuickCreateInstance(type);
                        foreach (var property in odataComplexValue.Properties)
                        {
                            UpdateCore(value, property.Name, property.Value);
                        }
                        openClrObject.OpenProperties[propertyName] = value;
                    }
                    else if (odataEnumValue != null)
                    {
                        var type = EdmClrTypeUtils.GetInstanceType(odataEnumValue.TypeName);
                        openClrObject.OpenProperties[propertyName] = ODataObjectModelConverter.ConvertPropertyValue(propertyValue, type);
                    }
                    else if (odataPrimitiveValue != null)
                    {
                        openClrObject.OpenProperties[propertyName] = odataPrimitiveValue.Value;
                    }
                    else
                    {
                        openClrObject.OpenProperties[propertyName] = propertyValue;
                    }
                }
            }
        }