Exemplo n.º 1
0
        protected override Object CreateRootEntity(ODataResource resource, IReadOnlyList <NavigationProperty> navigationProperties, Type entityType)
        {
            var openType = new SortedDictionary <String, Object>(StringComparer.Ordinal);

            foreach (ODataProperty property in resource.Properties)
            {
                if (property.Value is ODataUntypedValue)
                {
                    openType.Add(property.Name, null);
                }
                else if (property.Value is ODataEnumValue enumValue)
                {
                    Type enumType = Type.GetType(enumValue.TypeName);
                    openType.Add(property.Name, Enum.Parse(enumType, enumValue.Value));
                }
                else
                {
                    openType.Add(property.Name, property.Value);
                }
            }

            Dictionary <PropertyInfo, ODataResourceSetBase> propertyInfos = null;

            foreach (NavigationProperty property in navigationProperties)
            {
                Object value = property.Value;

                if (property.ResourceSet != null && (property.ResourceSet.Count != null || property.ResourceSet.NextPageLink != null))
                {
                    PropertyInfo clrProperty = entityType.GetProperty(property.Name);
                    if (value == null && property.ResourceSet.NextPageLink != null)
                    {
                        value = ResponseReader.CreateCollection(clrProperty.PropertyType);
                    }

                    if (value is IEnumerable collection)
                    {
                        base.NavigationProperties.Add(collection, property.ResourceSet);

                        if (propertyInfos == null)
                        {
                            propertyInfos = new Dictionary <PropertyInfo, ODataResourceSetBase>(navigationProperties.Count);
                            base.NavigationPropertyEntities.Add(openType, propertyInfos);
                        }
                        propertyInfos.Add(clrProperty, property.ResourceSet);
                    }
                }

                openType.Add(property.Name, value);
            }

            return(openType);
        }
Exemplo n.º 2
0
        protected override Object CreateRootEntity(ODataResource resource, IReadOnlyList <NavigationInfo> navigationProperties, Type entityType)
        {
            var openType = new SortedDictionary <String, Object>(StringComparer.Ordinal);

            foreach (ODataProperty property in resource.Properties)
            {
                if (property.Value is ODataUntypedValue)
                {
                    openType.Add(property.Name, null);
                }
                else if (property.Value is ODataEnumValue enumValue)
                {
                    Type enumType = Type.GetType(enumValue.TypeName);
                    openType.Add(property.Name, Enum.Parse(enumType, enumValue.Value));
                }
                else
                {
                    openType.Add(property.Name, property.Value);
                }
            }

            Dictionary <PropertyInfo, NavigationInfo> propertyInfos = null;

            foreach (NavigationInfo navigationInfo in navigationProperties)
            {
                Object value = navigationInfo.Value;

                if (navigationInfo.Count != null || navigationInfo.NextPageLink != null)
                {
                    PropertyInfo clrProperty = entityType.GetProperty(navigationInfo.Name);
                    if (value == null && navigationInfo.NextPageLink != null)
                    {
                        value = ResponseReader.CreateCollection(clrProperty.PropertyType);
                    }

                    if (value is IEnumerable collection)
                    {
                        base.NavigationProperties.Add(collection, navigationInfo);

                        if (propertyInfos == null)
                        {
                            propertyInfos = new Dictionary <PropertyInfo, NavigationInfo>(navigationProperties.Count);
                            base.NavigationInfoEntities.Add(openType, propertyInfos);
                        }
                        propertyInfos.Add(clrProperty, navigationInfo);
                    }
                }

                if (value == null)
                {
                    PropertyInfo clrProprety = entityType.GetProperty(navigationInfo.Name);
                    Type         type        = Parsers.OeExpressionHelper.GetCollectionItemTypeOrNull(clrProprety.PropertyType);
                    if (type == null)
                    {
                        type = clrProprety.PropertyType;
                    }

                    if (Parsers.OeExpressionHelper.IsEntityType(type))
                    {
                        value = clrProprety.PropertyType;
                    }
                }

                openType.Add(navigationInfo.Name, value);
            }

            return(openType);
        }