Пример #1
0
        public PostBuilder WithComment(int id, string body)
        {
            var comment = new Comment()
            {
                Body= body,
                Id = id,
                Post = post
            };

            post.Replies.Add(comment);
            return this;
        }
Пример #2
0
 public Author()
 {
     Posts = new List<Post>();
     Comments = new Comment[1];
 }
        public void WithComplexObjectTest()
        {
            //Arrange
            const int authorId = 5;
            const string authorName = "Valentin";
            const int postId = 6;
            const string postTitle = "The measure of a man";
            const string commentBody = "Comment body";
            const int commentId = 7;
            var author = new Author() { Id = authorId, Name = authorName };
            var post = new Post() { Id = postId, Title = postTitle, Author = author };
            var comment = new Comment() { Id = commentId, Body = commentBody, Post = post };
            post.Replies = new List<Comment>() { comment };
            author.Posts = new List<Post>() { post };

            var configurationBuilder = new ConfigurationBuilder();

            //Act
            var resourceConfigurationForPost = configurationBuilder
                .Resource<Post, PostsController>()
                .WithSimpleProperty(p => p.Title)
                .WithIdSelector(p => p.Id)
                .WithLinkedResource(p => p.Replies);
            var resourceConfigurationForAuthor = configurationBuilder
                .Resource<Author, AuthorsController>()
                .WithSimpleProperty(a => a.Name)
                .WithIdSelector(a => a.Id)
                .WithLinkedResource(a => a.Posts);
            var resourceConfigurationForComment = configurationBuilder
                .Resource<Comment, CommentsController>()
                .WithIdSelector(c => c.Id)
                .WithSimpleProperty(c => c.Body);
            var result = configurationBuilder.Build();

            //Assert
            Assert.Equal(resourceConfigurationForPost.BuiltResourceMapping.Relationships.Count, 1);
            Assert.Equal(resourceConfigurationForAuthor.BuiltResourceMapping.Relationships.Count, 1);
            configurationBuilder.ResourceConfigurationsByType.All(
                r => r.Value.BuiltResourceMapping.Relationships.All(l => l.ResourceMapping != null));
            var authorLinks =
                 configurationBuilder.ResourceConfigurationsByType[
                     resourceConfigurationForAuthor.BuiltResourceMapping.ResourceRepresentationType].BuiltResourceMapping.Relationships;
            Assert.Equal(authorLinks.Count, 1);
            Assert.Equal(authorLinks[0].RelationshipName, "posts");
            Assert.Equal(authorLinks[0].ResourceMapping.PropertyGetters.Count, 1);
        }