public static IEnumerable <ExpandableOfmForGet> ToExpandableOfmForGets <TOfmForGet>(
            this IEnumerable <TOfmForGet> expandableOfmForGetSourceCollection) where TOfmForGet : IOfmForGet
        {
            if (expandableOfmForGetSourceCollection == null)
            {
                throw new ArgumentNullException("expandableOfmForGetSourceCollection");
            }

            var propertyInfoList = new List <PropertyInfo>();
            var propertyInfos    = typeof(TOfmForGet).GetProperties(BindingFlags.Public | BindingFlags.Instance);

            propertyInfoList.AddRange(propertyInfos);

            var expandableOfmForGetList = new List <ExpandableOfmForGet>();

            foreach (var ofmForGetSource in expandableOfmForGetSourceCollection)
            {
                var singleExpandableOfmForGet = new ExpandableOfmForGet();
                foreach (var propertyInfo in propertyInfoList)
                {
                    // GetValue returns the value of the property on the source object
                    var propertyValue = propertyInfo.GetValue(ofmForGetSource);

                    // add the field to the ExpandoObject
                    ((IDictionary <string, object>)singleExpandableOfmForGet).Add(propertyInfo.Name, propertyValue);
                }
                expandableOfmForGetList.Add(singleExpandableOfmForGet);
            }

            return(expandableOfmForGetList);
        }
Example #2
0
        public static ExpandableOfmForGet Shape(this ExpandableOfmForGet expandableOfmForGetSource, string fields)
        {
            if (expandableOfmForGetSource == null)
            {
                throw new ArgumentNullException("expandableOfmForGetSource");
            }

            if (string.IsNullOrWhiteSpace(fields))
            {
                return(expandableOfmForGetSource);
            }

            // the field are separated by ",", so we split it.
            var fieldsAfterSplit = fields.Split(',').Select(s => s.ToLower().Trim());

            var shapedExpandableOfmForGet = new ExpandableOfmForGet();

            foreach (var field in fieldsAfterSplit)
            {
                var property = expandableOfmForGetSource.FirstOrDefault(f => f.Key.ToLowerInvariant() == field);

                if (!property.IsDefault()) // in effect if the struct KeyValuePair is not null
                {
                    shapedExpandableOfmForGet.Add(property.Key, property.Value);
                }
            }

            // return
            return(shapedExpandableOfmForGet);
        }
        public static IEnumerable <ExpandableOfmForGet> Shape(
            this IEnumerable <ExpandableOfmForGet> expandableOfmForGetSourceCollection,
            string fields,
            bool includeHateoasLinks)
        {
            if (expandableOfmForGetSourceCollection == null)
            {
                throw new ArgumentNullException("expandableOfmForGetSourceCollection");
            }

            var expandableOfmForGetList           = new List <ExpandableOfmForGet>();
            IEnumerable <string> fieldsAfterSplit = null;

            if (string.IsNullOrWhiteSpace(fields))
            {
                return(expandableOfmForGetSourceCollection);
            }
            else
            {
                fieldsAfterSplit = fields.Split(',').Select(field => field.ToLower().Trim());
            }

            foreach (var ofmForGetSource in expandableOfmForGetSourceCollection)
            {
                var shapedExpandableOfmForGet = new ExpandableOfmForGet();

                foreach (var field in fieldsAfterSplit)
                {
                    var property = ofmForGetSource.FirstOrDefault(f => f.Key.ToLowerInvariant() == field);

                    if (!property.IsDefault()) // in effect if the struct KeyValuePair is not null
                    {
                        shapedExpandableOfmForGet.Add(property.Key, property.Value);
                    }
                }

                if (includeHateoasLinks)
                {
                    var property = ofmForGetSource.FirstOrDefault(f => f.Key.ToLowerInvariant() == "links");
                    shapedExpandableOfmForGet.Add(property.Key, property.Value);
                }

                expandableOfmForGetList.Add(shapedExpandableOfmForGet);
            }

            return(expandableOfmForGetList);
        }
Example #4
0
        public static ExpandableOfmForGet ToExpandableOfm <TOfmForGet>(this TOfmForGet ofmForGetSource) where TOfmForGet : IOfmForGet
        {
            if (ofmForGetSource == null)
            {
                throw new ArgumentNullException("ofmForGetSource");
            }

            var expandableOfm = new ExpandableOfmForGet();

            // all public properties should be in the ExpandoObject
            var propertyInfos = typeof(TOfmForGet)
                                .GetProperties(BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);

            foreach (var propertyInfo in propertyInfos)
            {
                // get the value of the property on the ofmForGetSource object
                var propertyValue = propertyInfo.GetValue(ofmForGetSource);

                // add the field to the ExpandoObject
                ((IDictionary <string, object>)expandableOfm).Add(propertyInfo.Name, propertyValue);
            }

            return(expandableOfm);
        }