/// <summary>
        /// Gets the resource object for <paramref name="parent"/> by searching the included list.
        /// If it was not already build, it is constructed and added to the included list.
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="relationship"></param>
        /// <returns></returns>
        private ResourceObject GetOrBuildResourceObject(IIdentifiable parent, RelationshipAttribute relationship)
        {
            var type         = parent.GetType();
            var resourceName = _provider.GetResourceContext(type).ResourceName;
            var entry        = _included.SingleOrDefault(ro => ro.Type == resourceName && ro.Id == parent.StringId);

            if (entry == null)
            {
                entry = Build(parent, _fieldsToSerialize.GetAllowedAttributes(type, relationship), _fieldsToSerialize.GetAllowedRelationships(type));
                _included.Add(entry);
            }
            return(entry);
        }
Пример #2
0
        /// <summary>
        /// Gets the list of attributes to serialize for the given <paramref name="resourceType"/>.
        /// Note that the choice omitting null-values is not handled here,
        /// but in <see cref="IResourceObjectBuilderSettingsProvider"/>.
        /// </summary>
        /// <param name="resourceType">Type of entity to be serialized</param>
        /// <returns>List of allowed attributes in the serialized result</returns>
        private List <AttrAttribute> GetAttributesToSerialize(Type resourceType)
        {
            // Check the attributes cache to see if the allowed attrs for this resource type were determined before.
            if (_attributesToSerializeCache.TryGetValue(resourceType, out List <AttrAttribute> allowedAttributes))
            {
                return(allowedAttributes);
            }

            // Get the list of attributes to be exposed for this type
            allowedAttributes = _fieldsToSerialize.GetAllowedAttributes(resourceType);

            // add to cache so we we don't have to look this up next time.
            _attributesToSerializeCache.Add(resourceType, allowedAttributes);
            return(allowedAttributes);
        }