CreateIncludedRepresentations() public method

public CreateIncludedRepresentations ( List primaryResourceList, IResourceMapping resourceMapping, Context context ) : List
primaryResourceList List
resourceMapping IResourceMapping
context Context
return List
示例#1
0
        public CompoundDocument Transform(object objectGraph, Context context)
        {
            Type innerObjectType = Reflection.GetObjectType(objectGraph);

            transformationHelper.VerifyTypeSupport(innerObjectType);
            transformationHelper.AssureAllMappingsRegistered(innerObjectType, configuration);

            var result = new CompoundDocument
            {
                Meta = transformationHelper.GetMetadata(objectGraph)
            };

            var resource        = transformationHelper.UnwrapResourceObject(objectGraph);
            var resourceMapping = configuration.GetMapping(innerObjectType);

            var resourceList       = transformationHelper.UnifyObjectsToList(resource);
            var representationList = resourceList.Select(
                o => transformationHelper.CreateResourceRepresentation(o, resourceMapping, context));

            result.Data  = transformationHelper.ChooseProperResourceRepresentation(resource, representationList);
            result.Links = transformationHelper.GetTopLevelLinks(objectGraph, context.RequestUri);

            if (resourceMapping.Relationships.Any())
            {
                result.Included = transformationHelper.CreateIncludedRepresentations(resourceList, resourceMapping, context);
            }

            return(result);
        }
        public void AppendIncludedRepresentationRecursive_RecursesWholeTree()
        {
            // Arrange
            var source = new PostBuilder()
                .WithAuthor(PostBuilder.Asimov)
                .WithComment(1, "Comment One")
                .WithComment(2, "Comment Two")
                .Build();

            var sourceList = new List<object>()
            {
                source
            };

            var config = TestModelConfigurationBuilder.BuilderForEverything.Build();

            var mapping = config.GetMapping(typeof(Post));
            var context = new Context(
                new Uri("http://dummy:4242/posts"),
                new string[] { "authors.comments" });

            var transformationHelper = new TransformationHelper(config, new FakeLinkBuilder());

            // Act
            var result = transformationHelper.CreateIncludedRepresentations(sourceList, mapping, context);

            // Assert
            Assert.NotNull(result.Single(x => x.Id == "1" && x.Type == "comments"));
            Assert.NotNull(result.Single(x => x.Id == "2" && x.Type == "comments"));
            Assert.NotNull(result.Single(x => x.Id == "1" && x.Type == "authors"));
            Assert.False(result.Any(x => x.Type == "posts"));
        }
示例#3
0
        public CompoundDocument Transform(object objectGraph, Context context)
        {
            CompoundDocument result = objectGraph as CompoundDocument;

            if (result != null)
            {
                return(result);
            }

            Type innerObjectType = TransformationHelper.GetObjectType(objectGraph);

            TransformationHelper.VerifyTypeSupport(innerObjectType);
            TransformationHelper.AssureAllMappingsRegistered(innerObjectType, context.Configuration);

            result = new CompoundDocument
            {
                Meta = TransformationHelper.GetMetadata(objectGraph)
            };

            var resource        = TransformationHelper.UnwrapResourceObject(objectGraph);
            var resourceMapping = context.Configuration.GetMapping(innerObjectType);

            var resourceList       = TransformationHelper.UnifyObjectsToList(resource);
            var representationList = resourceList.Select(o => TransformationHelper.CreateResourceRepresentation(o, resourceMapping, context));
            var primaryResource    = TransformationHelper.ChooseProperResourceRepresentation(resource, representationList);

            result.Data = primaryResource;

            if (resourceMapping.Relationships.Any())
            {
                result.Included = TransformationHelper.CreateIncludedRepresentations(resourceList, resourceMapping, context);
            }

            return(result);
        }
        public void AppendIncludedRepresentationRecursive_RecursesWholeTree_No_Duplicates()
        {
            // Arrange
            var duplicateAuthor = PostBuilder.Asimov;

            var firstSource = new PostBuilder()
                .WithAuthor(duplicateAuthor)
                .Build();

            var secondSource = new PostBuilder()
                .WithAuthor(duplicateAuthor)
                .Build();

            var sourceList = new List<object>()
            {
                firstSource,
                secondSource
            };

            var config = TestModelConfigurationBuilder.BuilderForEverything.Build();

            var mapping = config.GetMapping(typeof(Post));
            var context = new Context(
                new Uri("http://dummy:4242/posts"),
                new string[] { "authors.comments" });

            var transformationHelper = new TransformationHelper(config, new FakeLinkBuilder());

            // Act
            var result = transformationHelper.CreateIncludedRepresentations(sourceList, mapping, context);

            // Assert
            Assert.Equal(1, result.Count(x => 
                x.Type == "authors" && 
                x.Id == PostBuilder.Asimov.Id.ToString()));
        }
        public void GIVEN_NoIncludedItems_WHEN_Get_THEN_IncludedItemsAreNull()
        {
            // Arrange
            var source = new PostBuilder()
                .Build();

            var sourceList = new List<object>()
            {
                source
            };

            var config = TestModelConfigurationBuilder.BuilderForEverything.Build();

            var mapping = config.GetMapping(typeof(Post));
            var context = new Context(new Uri("http://dummy:4242/posts"));

            var transformationHelper = new TransformationHelper(config, new FakeLinkBuilder());

            // Act
            var result = transformationHelper.CreateIncludedRepresentations(sourceList, mapping, context);

            // Assert
            Assert.Null(result);
        }