Ejemplo n.º 1
0
        public void OnInspectedResource(HalResourceInspectedContext context)
        {
            if (context.Resource == null)
            {
                return;
            }

            var linkCounts = new Dictionary <string, int>();

            foreach (var link in context.Resource.Links)
            {
                if (linkCounts.ContainsKey(link.Rel))
                {
                    linkCounts[link.Rel]++;
                }
                else
                {
                    linkCounts[link.Rel] = 1;
                }
            }

            foreach (var count in linkCounts)
            {
                if (context.Resource.SingularRelations.Contains(count.Key) &&
                    count.Value > 1)
                {
                    throw new HalException($"Relation '{count.Key}' is marked as singular, but contains {count.Value} links.");
                }
            }

            var embeddedCounts = new Dictionary <string, int>();

            foreach (var embedded in context.Resource.Embedded)
            {
                if (!linkCounts.ContainsKey(embedded.Rel))
                {
                    throw new HalException($"Resource contains embedded resource with rel '{embedded.Rel}', but not corresponding link exists.");
                }

                if (embeddedCounts.ContainsKey(embedded.Rel))
                {
                    embeddedCounts[embedded.Rel]++;
                }
                else
                {
                    embeddedCounts[embedded.Rel] = 1;
                }
            }

            foreach (var count in embeddedCounts)
            {
                if (context.Resource.SingularRelations.Contains(count.Key) &&
                    count.Value > 1)
                {
                    throw new HalException($"Relation '{count.Key}' is marked as singular, but contains {count.Value} embedded resources.");
                }
            }
        }
Ejemplo n.º 2
0
        private async Task <HalResourceInspectedContext> Next()
        {
            // TODO: Validate the inspectingcontext here to make sure the last pipeline component did not do something illegal.

            // If the last inspector was invoked, create the inspectedcontext.
            if (currentIndex >= inspectorsLength)
            {
                return(new HalResourceInspectedContext(inspectingContext));
            }

            HalResourceInspectedContext result = null;

            currentInspector = this.inspectors[currentIndex++];

            if (currentInspector is IAsyncHalResourceInspector asyncInspector)
            {
                result = await asyncInspector.OnResourceInspectionAsync(inspectingContext, Next);
            }
            else if (currentInspector is IHalResourceInspector syncInspector)
            {
                syncInspector.OnInspectingResource(inspectingContext);
                result = await Next();

                syncInspector.OnInspectedResource(result);
            }
            else
            {
                throw new InvalidOperationException(
                          $"inspector of type '{currentInspector.GetType().FullName}' is not an IHalResourceInspector or IAsyncHalResourceInspector.");
            }

            // TODO: Validate the inspectedcontext here to make sure the last pipeline component did not do something illegal.
            if (result == null)
            {
                throw new InvalidOperationException($"HalResourceInspectedContext was null after invoking '{currentInspector.GetType().FullName}'.");
            }

            return(result);
        }
Ejemplo n.º 3
0
 public void OnInspectedResource(HalResourceInspectedContext context)
 {
 }