Exemplo n.º 1
0
        private void AddToLinks(string linkName, LinkObject link)
        {
            if (!_innerExpandoDict.ContainsKey("Links"))
            {
                _innerExpandoDict.Add("Links", new ExpandoObject());
            }
            var links = (IDictionary <string, object>)_innerExpandoDict["Links"];

            links.Add(linkName, link);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Convert child objects marked with the [Resource] attribute into ResourceObject instances
        /// </summary>
        private static void Resourcify(object forObject, IDictionary <string, object> expandoDict, IJsonApiProfile withProfile)
        {
            foreach (var propertyInfo in forObject.GetType().GetPropertiesAndFields(TypeExtensions.PUBLIC_INSTANCE))
            {
                var resRel = propertyInfo.GetCustomAttribute <ResourceRelationshipAttribute>();
                if (resRel != null)
                {
                    var propValue = propertyInfo.GetValue(forObject);
                    if (propertyInfo.OfType == typeof(Uri) && propValue != null)
                    {
                        expandoDict[propertyInfo.Name] = LinkObject.LinkToUri((Uri)propValue);
                        continue;
                    }

                    Type enumerableType = propertyInfo.OfType.GetGenericIEnumerables().FirstOrDefault();
                    if (enumerableType != null)
                    {
                        if (propValue != null)
                        {
                            // It's a to-many relationship
                            var resources = (((IEnumerable)propValue))
                                            .Cast <object>()
                                            .Select(o => new ResourceObject(o, withProfile))
                                            .ToList();
                            expandoDict[propertyInfo.Name] = LinkObject.LinkToMany(resources, resRel.Sideload);
                        }
                        else
                        {
                            expandoDict[propertyInfo.Name] = LinkObject.Empty(LinkType.ToMany);
                        }
                    }
                    else
                    {
                        // It's a to-one relationship
                        if (propValue != null)
                        {
                            var resourceObject = new ResourceObject(propValue, withProfile);
                            expandoDict[propertyInfo.Name] = LinkObject.LinkToOne(resourceObject, resRel.Sideload);
                        }
                        else
                        {
                            expandoDict[propertyInfo.Name] = LinkObject.Empty(LinkType.ToOne);
                        }
                    }
                }
            }
        }