Пример #1
0
        public ActionResult Edit(BlogPost post)
        {
            _repository.Update<BlogPost>()
                .Set(x => x.Title, post.Title)
                .Set(x => x.Content, post.Content)
                .Set(x => x.PublishDate, post.PublishDate)
                .Where(x => x.Id == post.Id)
                .Execute();

            return RedirectToAction("Index", new { id = post.Id });
        }
Пример #2
0
        public ActionResult Create(BlogPost post, int blogId)
        {
            var user = _repository.Find<User>().Top(1).Execute();

            post.Blog = new Blog() { Id = blogId };
            post.Author = user;

            _repository.Insert(post);

            return RedirectToAction("Index", new { id = post.Id });
        }
Пример #3
0
        public ActionResult Create(int id)
        {
            var post = new BlogPost
                {
                    PublishDate = DateTime.Now,
                };

            ViewBag.BlogId = id;

            return View(post);
        }
Пример #4
0
        public void Join_example_with_transaction_insert()
        {
            Repository.DefaultConnectionString = @"Data source=.\SQLEXPRESS;Initial Catalog=WeenyMapper;Trusted_Connection=true";
            Repository.DefaultConvention = new BlogConvention();

            var myBlog = new Blog("My blog");

            var steve = new User("Steve", "password");

            var post1 = new BlogPost("Title 1", "Content 1 goes here") { Blog = myBlog, Author = steve };
            var post2 = new BlogPost("Title 2", "Content 2 goes here") { Blog = myBlog, Author = steve };
            var post3 = new BlogPost("Title 3", "Content 3 goes here") { Blog = myBlog, Author = steve };
            var post4 = new BlogPost("Title 4", "Content 4 goes here") { Blog = myBlog, Author = steve };

            using (var repository = new Repository())
            {
                using (var transaction = repository.BeginTransaction())
                {
                    repository.Insert(myBlog);
                    repository.Insert(steve);
                    repository.Insert(post1, post2, post3, post4);

                    transaction.Commit();
                }

                repository.Update<BlogPost>()
                          .Set(x => x.Title, "Updated title 2")
                          .Where(x => x.Id == post2.Id)
                          .Execute();

                repository.Delete<BlogPost>()
                          .Where(x => x.Id == post3.Id || x.Title == "Title 4")
                          .Execute();

                var actualBlog = repository.Find<Blog>().Where(x => x.Id == myBlog.Id)
                                           .Join(x => x.Posts, x => x.Blog)
                                           .OrderBy<BlogPost>(x => x.Title)
                                           .Execute();

                Assert.AreEqual("My blog", actualBlog.Name);
                Assert.AreEqual(2, actualBlog.Posts.Count);
                Assert.AreEqual("Title 1", actualBlog.Posts[0].Title);
                Assert.AreEqual("Updated title 2", actualBlog.Posts[1].Title);
            }
        }
        public void Alias_can_be_given_to_the_starting_table_of_a_query()
        {
            Repository.Convention = new BlogConvention();

            var user1 = new User("kalle", "password");
            var blog1 = new Blog("blog 1");

            Repository.Insert(blog1);
            Repository.Insert(user1);

            var post1 = new BlogPost("title 1", "content 1") { Blog = blog1, Author = user1 };
            var post2 = new BlogPost("title 2", "content 2") { Blog = blog1, Author = user1 };

            Repository.Insert(post1, post2);

            var actualBlogs = Repository.Find<Blog>("blog_alias")
                                        .OrderBy<BlogPost>("blogpost_alias", x => x.Title)
                                        .Join<Blog, BlogPost>(x => x.Posts, x => x.Blog, parentAlias: "blog_alias", childAlias: "blogpost_alias")
                                        .ExecuteList();

            Assert.AreEqual(1, actualBlogs.Count);
            Assert.AreEqual(2, actualBlogs[0].Posts.Count);
        }
        public void Transactions_are_not_rolled_back_if_already_rolled_back_from_exception_during_execution_of_a_SQL_statement()
        {
            var myBlog = new Blog("My blog");

            var post1 = new BlogPost("Title 1", "Content 1 goes here");

            using (var repository = new Repository())
            {

                try
                {
                    using (var transaction = repository.BeginTransaction())
                    {
                        repository.Insert(myBlog);

                        // Will throw exception due to foreign key constraints not being met
                        repository.Insert(post1);

                        transaction.Commit();
                    }
                }
                catch
                {
                   // Swallow the exception
                }

                var actualBlog = repository.Find<Blog>().Where(x => x.Name == "My blog").Execute();

                Assert.IsNull(actualBlog);
            }
        }
Пример #7
0
 public void AddPost(BlogPost post)
 {
     Posts.Add(post);
     post.Blog = this;
 }
        public void Disjuncted_conditions_on_multiple_tables_can_be_specified_in_a_single_join_query()
        {
            Repository.IsEntityCachingEnabled = true;

            Repository.DefaultConvention = new BlogConvention();

            var blog1 = new Blog
                {
                    Name = "Blog 1",
                };

            var blog2 = new Blog
                {
                    Name = "Blog 2",
                };

            var post1 = new BlogPost
                {
                    Title = "Blog post 1",
                    Content = "Post 1 content",
                    PublishDate = new DateTime(2011, 4, 1),
                };

            var post2 = new BlogPost
                {
                    Title = "Blog post 2",
                    Content = "Post 2 content",
                    PublishDate = new DateTime(2011, 2, 2),
                };

            var post3 = new BlogPost
                {
                    Title = "Blog post 3",
                    Content = "Post 3 content",
                    PublishDate = new DateTime(2011, 3, 3),
                };

            var post4 = new BlogPost
                {
                    Title = "Blog post 4",
                    Content = "Post 4 content",
                    PublishDate = new DateTime(2011, 4, 4),
                };

            blog1.AddPost(post1);
            blog2.AddPost(post2);
            blog2.AddPost(post3);
            blog2.AddPost(post4);

            Repository.Insert(blog1, blog2);
            Repository.Insert(post1, post2, post3, post4);

            var actualBlogs = Repository.Find<Blog>()
                                        .Where(x => x.Name == "Blog 1")
                                        .OrWhere<BlogPost>(x => x.Title == "Blog post 2")
                                        .OrderBy(x => x.Name)
                                        .Join<Blog, BlogPost>(x => x.Posts, x => x.Blog)
                                        .ExecuteList();

            // Change the original objects to the expected state for the retrieved objects, so that
            // we can use Equals to compare the actual result with what is expected
            blog2.Posts.Remove(post3);
            blog2.Posts.Remove(post4);

            Assert.AreEqual(2, actualBlogs.Count);
            Assert.AreEqual(blog1, actualBlogs[0]);
            Assert.AreEqual(blog2, actualBlogs[1]);
        }
        public void One_way_child_to_parent_relationship_of_a_joined_table_can_be_loaded_with_another_join()
        {
            Repository.Convention = new BlogConvention();

            var user1 = new User("kalle", "password");
            var user2 = new User("pelle", "password");
            var blog = new Blog("blog 1");

            Repository.Insert(blog);
            Repository.Insert(user1, user2);

            var post1 = new BlogPost("title 1", "content 1") { Blog = blog };
            var post2 = new BlogPost("title 2", "content 2") { Blog = blog };

            Repository.Insert(post1, post2);

            var comment1 = new Comment("comment 1") { User = user1, BlogPost = post1 };
            var comment2 = new Comment("comment 2") { User = user2, BlogPost = post1 };
            var comment3 = new Comment("comment 3") { User = user1, BlogPost = post2 };

            Repository.Insert(comment1, comment2, comment3);

            var actualPost = Repository.Find<BlogPost>()
                                  .Where(x => x.Id == post1.Id)
                                  .OrderBy<Comment>(x => x.Content)
                                  .Join(x => x.Comments, x => x.BlogPost)
                                  .Join<User, Comment>(x => x.User)
                                  .Execute();

            Assert.AreEqual("title 1", actualPost.Title);
            Assert.AreEqual("comment 1", actualPost.Comments[0].Content);
            Assert.AreEqual("comment 2", actualPost.Comments[1].Content);

            Assert.AreEqual(2, actualPost.Comments.Count);
            Assert.IsNotNull(actualPost.Comments[0].User);
            Assert.IsNotNull(actualPost.Comments[1].User);
            Assert.AreEqual("kalle", actualPost.Comments[0].User.Username);
            Assert.AreEqual("pelle", actualPost.Comments[1].User.Username);
        }
        public virtual void Multi_level_relationship_directed_from_middle_level_out_to_parent_and_to_child_can_be_read_with_two_way_relations()
        {
            Repository.DefaultConvention = new BlogConvention();

            var blog1 = new Blog
                {
                    Name = "Blog 1",
                };

            var blog2 = new Blog
                {
                    Name = "Blog 2",
                };

            var post1 = new BlogPost
                {
                    Title = "Blog post 1",
                    Content = "Post 1 content",
                    PublishDate = new DateTime(2011, 1, 1),
                };

            var post2 = new BlogPost
                {
                    Title = "Blog post 2",
                    Content = "Post 2 content",
                    PublishDate = new DateTime(2011, 1, 2)
                };

            var post3 = new BlogPost
                {
                    Title = "Blog post 3",
                    Content = "Post 3 content",
                    PublishDate = new DateTime(2011, 1, 3)
                };

            var comment1 = new Comment
                {
                    Content = "Comment 1",
                    PublishDate = new DateTime(2011, 1, 5)
                };

            var comment2 = new Comment
                {
                    Content = "Comment 2",
                    PublishDate = new DateTime(2011, 1, 6)
                };

            var comment3 = new Comment
                {
                    Content = "Comment 3",
                    PublishDate = new DateTime(2011, 1, 7)
                };

            var comment4 = new Comment
                {
                    Content = "Comment 4",
                    PublishDate = new DateTime(2011, 1, 8)
                };

            var comment5 = new Comment
                {
                    Content = "Comment 5",
                    PublishDate = new DateTime(2011, 1, 9)
                };

            blog1.AddPost(post1);
            blog1.AddPost(post2);
            blog2.AddPost(post3);

            post1.AddComment(comment1);
            post1.AddComment(comment2);
            post1.AddComment(comment3);
            post2.AddComment(comment4);
            post2.AddComment(comment5);

            Repository.Insert(blog1, blog2);
            Repository.Insert(post1, post2, post3);
            Repository.Insert(comment1, comment2, comment3, comment4, comment5);

            var actualBlogPosts = Repository.Find<BlogPost>().Where(x => x.PublishDate >= new DateTime(2011, 1, 2))
                                            .OrderBy(x => x.PublishDate)
                                            .OrderBy<Comment>(x => x.Content)
                                            .Join<BlogPost, Comment>(x => x.Comments, x => x.BlogPost)
                                            .Join<Blog, BlogPost>(x => x.Posts, x => x.Blog)
                                            .ExecuteList();

            Assert.AreEqual(2, actualBlogPosts.Count);

            Assert.AreEqual(post2, actualBlogPosts[0]);
            Assert.AreEqual(post3, actualBlogPosts[1]);

            // Post1 does not match the filter condition of the query, so that won't be in the result
            // Hence, remove that post from the expected blog, so that we can use the Equals method
            // to compare the expected with the actual
            blog1.Posts.Remove(post1);

            Assert.AreEqual(blog1, actualBlogPosts[0].Blog);
            Assert.AreEqual(blog2, actualBlogPosts[1].Blog);

            var actualBlog1 = actualBlogPosts[0].Blog;
            var actualBlog2 = actualBlogPosts[1].Blog;

            Assert.AreEqual(1, actualBlog1.Posts.Count);
            Assert.AreEqual(1, actualBlog2.Posts.Count);

            Assert.AreSame(actualBlogPosts[0], actualBlog1.Posts.First());
            Assert.AreSame(actualBlogPosts[1], actualBlog2.Posts.First());

            actualBlogPosts[0].Comments.ToList().ForEach(x => Assert.AreSame(actualBlogPosts[0], x.BlogPost));
            actualBlogPosts[1].Comments.ToList().ForEach(x => Assert.AreSame(actualBlogPosts[1], x.BlogPost));
        }
        public virtual void Multi_level_relationship_directed_from_child_to_parent_can_be_written_and_read_back_in_single_query()
        {
            Repository.DefaultConvention = new BlogConvention();

            var blog1 = new Blog
                {
                    Name = "Blog 1",
                };

            var blog2 = new Blog
                {
                    Name = "Blog 2",
                };

            var post1 = new BlogPost
                {
                    Title = "Blog post 1",
                    Content = "Post 1 content",
                    PublishDate = new DateTime(2011, 1, 1),
                };

            var post2 = new BlogPost
                {
                    Title = "Blog post 2",
                    Content = "Post 2 content",
                    PublishDate = new DateTime(2011, 1, 2)
                };

            var post3 = new BlogPost
                {
                    Title = "Blog post 3",
                    Content = "Post 3 content",
                    PublishDate = new DateTime(2011, 1, 3)
                };

            var comment1 = new Comment
                {
                    Content = "Comment 1",
                    PublishDate = new DateTime(2011, 1, 5)
                };

            var comment2 = new Comment
                {
                    Content = "Comment 2",
                    PublishDate = new DateTime(2011, 1, 6)
                };

            var comment3 = new Comment
                {
                    Content = "Comment 3",
                    PublishDate = new DateTime(2011, 1, 7)
                };

            var comment4 = new Comment
                {
                    Content = "Comment 4",
                    PublishDate = new DateTime(2011, 1, 8)
                };

            var comment5 = new Comment
                {
                    Content = "Comment 5",
                    PublishDate = new DateTime(2011, 1, 9)
                };

            blog1.AddPost(post1);
            blog1.AddPost(post2);
            blog2.AddPost(post3);

            post1.AddComment(comment1);
            post1.AddComment(comment2);
            post1.AddComment(comment3);
            post2.AddComment(comment4);
            post2.AddComment(comment5);

            Repository.Insert(blog1, blog2);
            Repository.Insert(post1, post2, post3);
            Repository.Insert(comment1, comment2, comment3, comment4, comment5);

            var actualComments = Repository.Find<Comment>().Where(x => x.PublishDate >= new DateTime(2011, 1, 6))
                                           .OrderBy(x => x.PublishDate)
                                           .Join<BlogPost, Comment>(x => x.Comments, x => x.BlogPost)
                                           .Join<Blog, BlogPost>(x => x.Posts, x => x.Blog)
                                           .ExecuteList();

            // Since comment1 has a PublishDate before 2011-01-06 it will not be returned by the query.
            // Because of this the instance of post1 returned will not have comment1 in its Comments collection.
            // So, to make the asserts correct. That comment is removed from the original post1.
            post1.Comments.Remove(comment1);

            Assert.AreEqual(4, actualComments.Count);

            Assert.AreEqual(comment2, actualComments[0]);
            Assert.AreEqual(comment3, actualComments[1]);
            Assert.AreEqual(comment4, actualComments[2]);
            Assert.AreEqual(comment5, actualComments[3]);

            Assert.AreEqual(post1, actualComments[0].BlogPost);
            Assert.AreEqual(post2, actualComments[2].BlogPost);

            Assert.AreEqual(blog1, actualComments[0].BlogPost.Blog);

            Assert.AreSame(actualComments[0].BlogPost, actualComments[1].BlogPost);
            Assert.AreSame(actualComments[2].BlogPost, actualComments[3].BlogPost);

            Assert.AreSame(actualComments[0].BlogPost.Blog, actualComments[1].BlogPost.Blog);
            Assert.AreSame(actualComments[0].BlogPost.Blog, actualComments[2].BlogPost.Blog);
            Assert.AreSame(actualComments[0].BlogPost.Blog, actualComments[3].BlogPost.Blog);
        }
        public virtual void Multi_level_relationship_can_be_written_and_read_back_again_in_single_query_using_join()
        {
            Repository.DefaultConvention = new BlogConvention();

            var blog1 = new Blog
                {
                    Name = "Blog 1",
                };

            var blog2 = new Blog
                {
                    Name = "Blog 2",
                };

            var blog3 = new Blog
                {
                    Name = "Blog 3",
                };

            var blog4 = new Blog
                {
                    Name = "Blog 4",
                };

            var post1 = new BlogPost
                {
                    Title = "Blog post 1",
                    Content = "Post 1 content",
                    PublishDate = new DateTime(2011, 1, 1),
                };

            var post2 = new BlogPost
                {
                    Title = "Blog post 2",
                    Content = "Post 2 content",
                    PublishDate = new DateTime(2011, 1, 2)
                };

            var post3 = new BlogPost
                {
                    Title = "Blog post 3",
                    Content = "Post 3 content",
                    PublishDate = new DateTime(2011, 1, 3)
                };

            var post4 = new BlogPost
                {
                    Title = "Blog post 4",
                    Content = "Post 4 content",
                    PublishDate = new DateTime(2011, 1, 4)
                };

            var comment1 = new Comment
                {
                    Content = "Comment 1",
                    PublishDate = new DateTime(2011, 1, 5)
                };

            var comment2 = new Comment
                {
                    Content = "Comment 2",
                    PublishDate = new DateTime(2011, 1, 6)
                };

            var comment3 = new Comment
                {
                    Content = "Comment 3",
                    PublishDate = new DateTime(2011, 1, 7)
                };

            blog1.AddPost(post1);
            blog1.AddPost(post2);
            blog2.AddPost(post3);
            blog4.AddPost(post4);

            post1.AddComment(comment1);
            post2.AddComment(comment2);
            post2.AddComment(comment3);

            Repository.Insert(blog1, blog2, blog3, blog4);
            Repository.Insert(post1, post2, post3, post4);
            Repository.Insert(comment1, comment2, comment3);

            var actualBlogs = Repository.Find<Blog>().Where(x => x.Name == "Blog 1" || x.Name.EndsWith("3"))
                                        .OrderBy(x => x.Name)
                                        .OrderBy<BlogPost>(x => x.Title)
                                        .OrderBy<Comment>(x => x.Content)
                                        .Join<Blog, BlogPost>(x => x.Posts, x => x.Blog)
                                        .Join<BlogPost, Comment>(x => x.Comments, x => x.BlogPost)
                                        .ExecuteList();

            Assert.AreEqual(2, actualBlogs.Count);

            Assert.AreEqual(blog1, actualBlogs[0]);
            Assert.AreEqual(blog3, actualBlogs[1]);
        }
        public virtual void Many_to_one_relationship_can_be_written_and_read_back_again_in_single_query_using_join()
        {
            Repository.DefaultConvention = new BlogConvention();

            var blog1 = new Blog
                {
                    Name = "Blog 1",
                };

            var blog2 = new Blog
                {
                    Name = "Blog 2",
                };

            var post1 = new BlogPost
                {
                    Title = "Blog post 1",
                    Content = "Post 1 content",
                    PublishDate = new DateTime(2011, 1, 1),
                };

            var post2 = new BlogPost
                {
                    Title = "Blog post 2",
                    Content = "Post 2 content",
                    PublishDate = new DateTime(2011, 1, 2)
                };

            var post3 = new BlogPost
                {
                    Title = "Blog post 3",
                    Content = "Post 3 content",
                    PublishDate = new DateTime(2011, 1, 3)
                };

            blog1.AddPost(post1);
            blog1.AddPost(post2);
            blog2.AddPost(post3);

            Repository.Insert(blog1, blog2);
            Repository.Insert(post1, post2, post3);

            var actualBlog1 = Repository.Find<Blog>().Where(x => x.Name == "Blog 1")
                                        .Join<Blog, BlogPost>(x => x.Posts, x => x.Blog)
                                        .OrderBy<BlogPost>(x => x.Title)
                                        .Execute();

            Assert.AreEqual(blog1, actualBlog1);
        }
        public void Less_than_or_equal_and_greater_than_or_equal_can_be_used_for_filtering()
        {
            Repository.Convention = new BlogConvention();

            var myBlog = new Blog("My blog");

            var steve = new User("Steve", "password");

            var post1 = new BlogPost("Title 1", "Content 1 goes here") { Blog = myBlog, Author = steve, PublishDate = new DateTime(2011, 1, 1) };
            var post2 = new BlogPost("Title 2", "Content 2 goes here") { Blog = myBlog, Author = steve, PublishDate = new DateTime(2012, 1, 5) };
            var post3 = new BlogPost("Title 3", "Content 3 goes here") { Blog = myBlog, Author = steve, PublishDate = new DateTime(2012, 4, 1) };
            var post4 = new BlogPost("Title 4", "Content 4 goes here") { Blog = myBlog, Author = steve, PublishDate = new DateTime(2013, 1, 1) };
            var post5 = new BlogPost("Title 5", "Content 5 goes here") { Blog = myBlog, Author = steve, PublishDate = new DateTime(2013, 1, 2) };

            Repository.Insert(myBlog);
            Repository.Insert(steve);
            Repository.Insert(post1, post2, post3, post4, post5);

            var actualPosts = Repository.Find<BlogPost>()
                                        .Where(x => x.PublishDate >= new DateTime(2012, 1, 5) && x.PublishDate <= new DateTime(2013, 1, 1))
                                        .OrderBy(x => x.Title)
                                        .ExecuteList();

            Assert.AreEqual(3, actualPosts.Count);
            Assert.AreEqual("Title 2", actualPosts[0].Title);
            Assert.AreEqual("Title 3", actualPosts[1].Title);
            Assert.AreEqual("Title 4", actualPosts[2].Title);
        }
        public void Conjuncted_conditions_on_multiple_aliased_tables_can_be_specified_in_a_single_join_query()
        {
            Repository.IsEntityCachingEnabled = true;

            Repository.DefaultConvention = new BlogConvention();

            var blog1 = new Blog
                {
                    Name = "Blog 1",
                };

            var blog2 = new Blog
                {
                    Name = "Blog 2",
                };

            var post1 = new BlogPost
                {
                    Title = "Blog post 1",
                    Content = "Post 1 content",
                    PublishDate = new DateTime(2011, 4, 1),
                };

            var post2 = new BlogPost
                {
                    Title = "Blog post 2",
                    Content = "Post 2 content",
                    PublishDate = new DateTime(2011, 2, 2),
                };

            var post3 = new BlogPost
                {
                    Title = "Blog post 3",
                    Content = "Post 3 content",
                    PublishDate = new DateTime(2011, 3, 3),
                };

            var post4 = new BlogPost
                {
                    Title = "Blog post 4",
                    Content = "Post 4 content",
                    PublishDate = new DateTime(2011, 4, 4),
                };

            blog1.AddPost(post1);
            blog2.AddPost(post2);
            blog2.AddPost(post3);
            blog2.AddPost(post4);

            Repository.Insert(blog1, blog2);
            Repository.Insert(post1, post2, post3, post4);

            var actualPosts = Repository.Find<BlogPost>()
                                        .Where(x => x.PublishDate > new DateTime(2011, 3, 1))
                                        .AndWhere<Blog>(null, x => x.Name == "Blog 2")
                                        .OrderBy(x => x.Title)
                                        .Join<Blog, BlogPost>(x => x.Posts, x => x.Blog)
                                        .ExecuteList();

            Assert.AreEqual(2, actualPosts.Count);
            Assert.AreEqual(post3, actualPosts[0]);
            Assert.AreEqual(post4, actualPosts[1]);
        }
        public void Different_relations_on_same_entity_can_be_loaded_in_separate_queries_using_caching_repository()
        {
            Repository.IsEntityCachingEnabled = true;

            Repository.DefaultConvention = new BlogConvention();

            var blog1 = new Blog
                {
                    Name = "Blog 1",
                };

            var blog2 = new Blog
                {
                    Name = "Blog 2",
                };

            var post1 = new BlogPost
                {
                    Title = "Blog post 1",
                    Content = "Post 1 content",
                    PublishDate = new DateTime(2011, 1, 1),
                };

            var post2 = new BlogPost
                {
                    Title = "Blog post 2",
                    Content = "Post 2 content",
                    PublishDate = new DateTime(2011, 2, 2),
                };

            var comment1 = new Comment
                {
                    Content = "Comment 1",
                    PublishDate = new DateTime(2011, 1, 5)
                };

            var comment2 = new Comment
                {
                    Content = "Comment 2",
                    PublishDate = new DateTime(2011, 1, 6)
                };

            var comment3 = new Comment
                {
                    Content = "Comment 2",
                    PublishDate = new DateTime(2011, 1, 6)
                };

            var user = new User
                {
                    Username = "******",
                    Password = "******"
                };

            user.AddBlogPost(post1);
            user.AddBlogPost(post2);

            blog1.AddPost(post1);
            blog2.AddPost(post2);

            post1.AddComment(comment1);
            post1.AddComment(comment2);
            post2.AddComment(comment3);

            Repository.Insert(user);
            Repository.Insert(blog1, blog2);
            Repository.Insert(post1, post2);
            Repository.Insert(comment1, comment2, comment3);

            var actualPosts = Repository.Find<BlogPost>()
                                        .OrderBy(x => x.Title)
                                        .OrderBy<Comment>(x => x.Content)
                                        .Join<BlogPost, Comment>(x => x.Comments, x => x.BlogPost)
                                        .ExecuteList();

            var firstActualPost1 = Repository.Find<BlogPost>()
                                             .Join<Blog, BlogPost>(x => x.Posts, x => x.Blog)
                                             .Where(x => x.Id == post1.Id)
                                             .OrderBy(x => x.Title)
                                             .Execute();

            var secondActualPost1 = Repository.Find<BlogPost>()
                                              .Join<User, BlogPost>(x => x.BlogPosts, x => x.Author)
                                              .Where(x => x.Id == post1.Id)
                                              .Execute();

            // Set up the original entities according to the expected result
            post2.Author = null;
            post2.Blog = null;

            Assert.AreEqual(2, actualPosts.Count);

            Assert.AreEqual(post1, actualPosts[0]);
            Assert.AreEqual(post2, actualPosts[1]);

            Assert.AreSame(firstActualPost1, actualPosts[0]);
            Assert.AreSame(secondActualPost1, actualPosts[0]);
        }
        public void Order_by_clauses_can_be_specified_for_multiple_tables_in_a_join()
        {
            Repository.DefaultConvention = new BlogConvention();

            var blog1 = new Blog
                {
                    Name = "Blog 1",
                };
            var blog2 = new Blog
                {
                    Name = "Blog 2",
                };

            var post1 = new BlogPost
                {
                    Title = "Blog post 1",
                    Content = "Post 1 content",
                    PublishDate = new DateTime(2011, 2, 3),
                };
            var post2 = new BlogPost
                {
                    Title = "Blog post 2",
                    Content = "aaa",
                    PublishDate = new DateTime(2011, 2, 2),
                };
            var post3 = new BlogPost
                {
                    Title = "Blog post 3",
                    Content = "zzz",
                    PublishDate = new DateTime(2011, 2, 2),
                };
            var post4 = new BlogPost
                {
                    Title = "Blog post 4",
                    Content = "Post 4 content",
                    PublishDate = new DateTime(2011, 2, 1),
                };

            // Add the posts in the expected sort order to enable comparisons to be made correctly during the asserts
            blog1.AddPost(post2);
            blog1.AddPost(post3);
            blog1.AddPost(post1);

            blog2.AddPost(post4);

            Repository.Insert(blog1, blog2);
            Repository.Insert(post1, post2, post3, post4);

            var actualBlogs = Repository.Find<Blog>()
                                        .OrderByDescending(x => x.Name)
                                        .OrderBy<BlogPost>(x => x.PublishDate, x => x.Content)
                                        .Join<Blog, BlogPost>(x => x.Posts, x => x.Blog)
                                        .ExecuteList();

            Assert.AreEqual(2, actualBlogs.Count);
            Assert.AreEqual(blog2, actualBlogs[0]);
            Assert.AreEqual(blog1, actualBlogs[1]);

            Assert.AreEqual(1, actualBlogs[0].Posts.Count);
            Assert.AreEqual(post4, actualBlogs[0].Posts[0]);

            Assert.AreEqual(3, actualBlogs[1].Posts.Count);
            Assert.AreEqual(post2, actualBlogs[1].Posts[0]);
            Assert.AreEqual(post3, actualBlogs[1].Posts[1]);
            Assert.AreEqual(post1, actualBlogs[1].Posts[2]);
        }
Пример #18
0
        private static void CreateBlogTestData()
        {
            Repository.DefaultConvention = new BlogConvention();

            var myBlog = new Blog("My blog");

            var steve = new User("Steve", "password");

            var post1 = new BlogPost("Title 1", "Content 1 goes here") { Blog = myBlog, Author = steve, PublishDate = new DateTime(2011, 1, 1) };
            var post2 = new BlogPost("Title 2", "Content 2 goes here") { Blog = myBlog, Author = steve, PublishDate = new DateTime(2012, 1, 5) };
            var post3 = new BlogPost("Title 3", "Content 3 goes here") { Blog = myBlog, Author = steve, PublishDate = new DateTime(2012, 4, 1) };
            var post4 = new BlogPost("Title 4", "Content 4 goes here") { Blog = myBlog, Author = steve, PublishDate = new DateTime(2013, 1, 1) };

            using (var repository = new Repository())
            {
                using (var transaction = repository.BeginTransaction())
                {
                    repository.Insert(myBlog);
                    repository.Insert(steve);
                    repository.Insert(post1, post2, post3, post4);

                    transaction.Commit();
                }
            }
        }
        public void Partial_selects_can_be_specified_for_multiple_tables_in_a_join()
        {
            Repository.DefaultConvention = new BlogConvention();

            var blog1 = new Blog
                {
                    Name = "Blog 1",
                };

            var post1 = new BlogPost
                {
                    Title = "Blog post 1",
                    Content = "Post 1 content",
                    PublishDate = new DateTime(2011, 2, 2),
                };

            blog1.AddPost(post1);

            Repository.Insert(blog1);
            Repository.Insert(post1);

            var actualBlogs = Repository.Find<Blog>()
                                        .Select(x => x.Id)
                                        .Select<BlogPost>(x => x.Id, x => x.Title)
                                        .Join<Blog, BlogPost>(x => x.Posts, x => x.Blog)
                                        .ExecuteList();

            Assert.AreEqual(1, actualBlogs.Count);
            Assert.AreEqual(blog1.Id, actualBlogs[0].Id);
            Assert.IsNull(actualBlogs[0].Name);

            Assert.AreEqual(1, actualBlogs[0].Posts.Count);

            var actualPost = actualBlogs[0].Posts[0];

            Assert.AreEqual(post1.Id, actualPost.Id);
            Assert.AreEqual("Blog post 1", actualPost.Title);
            Assert.IsNull(actualPost.Content);
            Assert.AreEqual(new DateTime(), actualPost.PublishDate);
            Assert.IsNull(actualPost.Author);
            Assert.AreSame(actualBlogs[0], actualPost.Blog);
            CollectionAssert.IsEmpty(actualPost.Comments);
        }
Пример #20
0
 public void AddBlogPost(BlogPost blogPost)
 {
     BlogPosts.Add(blogPost);
     blogPost.Author = this;
 }
        public void Properties_which_already_have_values_are_not_set_to_null_if_another_query_is_run_with_caching_enabled_where_those_properties_are_not_loaded()
        {
            Repository.Convention = new BlogConvention();
            Repository.IsEntityCachingEnabled = true;

            var user1 = new User("kalle", "password");
            var user2 = new User("pelle", "password");
            var blog1 = new Blog("blog 1");
            var blog2 = new Blog("blog 2");

            Repository.Insert(blog1, blog2);
            Repository.Insert(user1, user2);

            var post1 = new BlogPost("title 1", "content 1") { Blog = blog1, Author = user1 };
            var post2 = new BlogPost("title 2", "content 2") { Blog = blog2, Author = user2 };

            Repository.Insert(post1, post2);

            var actualPosts = Repository.Find<BlogPost>()
                                        .OrderBy<BlogPost>(x => x.Title)
                                        .Join(x => x.Blog)
                                        .ExecuteList();

            Repository.Find<BlogPost>()
                      .Select(x => x.Id)
                      .Select<User>(x => x.Id, x => x.Username)
                      .Join(x => x.Author)
                      .ExecuteList();

            Assert.AreEqual(2, actualPosts.Count);
            Assert.AreEqual("title 1", actualPosts[0].Title);
            Assert.AreEqual("title 2", actualPosts[1].Title);
            Assert.IsNotNull(actualPosts[0].Blog);
            Assert.IsNotNull(actualPosts[1].Blog);
            Assert.IsNotNull(actualPosts[0].Author);
            Assert.IsNotNull(actualPosts[1].Author);
            Assert.AreEqual("kalle", actualPosts[0].Author.Username);
            Assert.AreEqual("pelle", actualPosts[1].Author.Username);
            Assert.AreEqual("blog 1", actualPosts[0].Blog.Name);
            Assert.AreEqual("blog 2", actualPosts[1].Blog.Name);
        }
        public void Conditions_on_multiple_tables_are_combined_with_the_expected_operator_precedence_when_specifying_conditions_alternating_between_tables()
        {
            Repository.IsEntityCachingEnabled = true;

            Repository.DefaultConvention = new BlogConvention();

            var blog1 = new Blog
                {
                    Name = "Blog 1",
                };

            var blog2 = new Blog
                {
                    Name = "Blog 2",
                };

            var blog3 = new Blog
                {
                    Name = "Blog 3",
                };

            var post1 = new BlogPost
                {
                    Title = "Blog post 1",
                    Content = "Post 1 content",
                    PublishDate = new DateTime(2011, 2, 2),
                };

            var post2 = new BlogPost
                {
                    Title = "Blog post 2",
                    Content = "Post 2 content",
                    PublishDate = new DateTime(2011, 2, 2),
                };

            var post3 = new BlogPost
                {
                    Title = "Blog post 3",
                    Content = "Post 3 content",
                    PublishDate = new DateTime(2011, 3, 3),
                };

            var post4 = new BlogPost
                {
                    Title = "Blog post 4",
                    Content = "Post 4 content",
                    PublishDate = new DateTime(2011, 2, 2),
                };

            var post5 = new BlogPost
                {
                    Title = "Blog post 5",
                    Content = "Post 5 content",
                    PublishDate = new DateTime(2011, 5, 5),
                };

            blog1.AddPost(post1);
            blog2.AddPost(post2);
            blog2.AddPost(post3);
            blog3.AddPost(post4);
            blog1.AddPost(post5);

            Repository.Insert(blog1, blog2, blog3);
            Repository.Insert(post1, post2, post3, post4);

            var actualBlogs = Repository.Find<Blog>()
                                        .Where(x => x.Name == "Blog 1")
                                        .AndWhere<BlogPost>(x => x.PublishDate == new DateTime(2011, 2, 2))
                                        .OrWhere(x => x.Name == "Blog 2")
                                        .OrderBy(x => x.Name)
                                        .OrderBy<BlogPost>(x => x.Title)
                                        .Join<Blog, BlogPost>(x => x.Posts, x => x.Blog)
                                        .ExecuteList();

            // Change the original objects to the expected state for the retrieved objects, so that
            // we can use Equals to compare the actual result with what is expected
            blog1.Posts.Remove(post5);

            Assert.AreEqual(2, actualBlogs.Count);
            Assert.AreEqual(blog1, actualBlogs[0]);
            Assert.AreEqual(blog2, actualBlogs[1]);
        }
        public void Setting_the_convention_on_one_repository_instance_does_not_change_the_convention_of_another_repository_instance()
        {
            var bookRepository = CreateRepository(new BookConvention());
            var blogRepository = CreateRepository(new BlogConvention());

            var book = new Book
                {
                    Isbn = "1",
                    AuthorName = "Author Name",
                    Title = "Title 1",
                    PageCount = 100,
                };

            var user = new User
                {
                    Id = Guid.NewGuid(),
                    Username = "******",
                    Password = "******"
                };

            var blog = new Blog { Name = "Blog" };
            var post = new BlogPost
                {
                    Title = "Blog post 1",
                    Content = "Post 1 content",
                    PublishDate = new DateTime(2011, 1, 1),
                };

            blog.AddPost(post);

            blogRepository.Insert(blog);
            blogRepository.Insert(post);
            bookRepository.Insert(book);
            Repository.Insert(user);

            var actualUser = Repository.Find<User>().Where(x => x.Id == user.Id).Execute();
            var actualBook = bookRepository.Find<Book>().Where(x => x.Isbn == book.Isbn).Execute();
            var actualPost = blogRepository.Find<BlogPost>().Where(x => x.Id == post.Id).Execute();

            Assert.AreEqual(book, actualBook);
            Assert.AreEqual(post.Id, actualPost.Id); // Only check id to avoid equals comparison of Blog
            Assert.AreEqual(user, actualUser);
        }
Пример #24
0
        private BlogPost CreatePost(int index, Blog blog, IList<User> users, Random random)
        {
            var month = 12 - (index * 2) / 28;
            var day = 28 - (index * 2) % 28;
            var title = blog.Name + " Post " + index;
            var user = users[random.Next(users.Count)];

            var hour = random.Next(23);
            var minute = random.Next(59);
            var second = random.Next(59);

            var post = new BlogPost
                {
                    Title = title,
                    PublishDate = new DateTime(2011, month, day, hour, minute, second),
                    Author = user,
                    Blog = blog
                };

            for (int i = 0; i < random.Next(10); i++)
            {
                var comment = new Comment
                    {
                        Content = "Comment " + i,
                        PublishDate = new DateTime(2011, month, day, random.Next(23), random.Next(59), random.Next(59))
                    };

                post.AddComment(comment);
            }

            post.Content = post.Title + " content";

            return post;
        }
Пример #25
0
 public void AddPost(BlogPost post)
 {
     Posts.Add(post);
     post.Blog = this;
 }