示例#1
0
        /// <summary>
        /// Creates an OData route from an ODCM node.
        /// </summary>
        /// <param name="node">The ODCM node</param>
        public ODataRoute(OdcmNode node)
        {
            if (node == null)
            {
                throw new ArgumentNullException(nameof(node));
            }

            // Store the node's ODCM property
            this.Property = node.OdcmProperty;

            // Store the parent node's ODCM property if it exists
            this.ParentProperty = node.Parent?.OdcmProperty;

            // Use a stack to add segments so we don't have to reverse them later
            Stack <OdcmProperty> segments = new Stack <OdcmProperty>();

            // Keep track of the ID parameters
            ICollection <string> idParameters = new List <string>();

            // Iterate over the node's parents (i.e. traverse the route in reverse)
            OdcmNode currentNode = node;
            OdcmNode childNode   = null;

            while (currentNode != null)
            {
                // Get the current node's property
                OdcmProperty property = currentNode.OdcmProperty;

                // Add this node to the route
                segments.Push(property);

                // Ignore this logic for the last segment in the route
                bool isLastSegment = childNode == null;
                if (!isLastSegment)
                {
                    // If the property this node represents is an enumeration, track its ID as a parameter
                    if (property.TryGetIdParameterName(out string idParameterName))
                    {
                        this._idParameters.Add(property, idParameterName);
                    }

                    // If the property's type is not the same as the class that the child property is defined in, the child class must be a subtype
                    if (property.Type != childNode.OdcmProperty.Class)
                    {
                        this._typeCastParameters.Add(property, property.GetResourceTypeParameterName());
                    }
                }

                // Update pointers
                childNode   = currentNode;
                currentNode = currentNode.Parent;
            }

            this.Segments = segments.ToList();
        }