示例#1
0
        internal static Blog BuildAnyBlogWithRelations(Fixture f)
        {
            var blog = new Blog()
            {
                Author = f.Create<string>(),
                Description = f.Create<string>(),
                Name = f.Create<string>(),

                Posts = f.Build<Post>()
                    .OmitAutoProperties()
                    .With(p => p.Date, f.Create<string>())
                    .With(p => p.Text, f.Create<string>())
                    .CreateMany()
                    .ToList(),

                Followers = f.Build<Follower>()
                    .OmitAutoProperties()
                    .With(p => p.Name, f.Create<string>())
                    .CreateMany()
                    .ToList(),
            };

            // The Url must be unique, since its an alternate key on the post
            foreach (var p in blog.Posts)
            {
                p.Url = Guid.NewGuid().ToString();
            }

            return blog;
        }
        public void CanRetrieveAndAllNavigationPropertiesArePopulated()
        {
            var blog = new Blog()
            {
                Author = Fixture.Create<string>(),
                Name = Fixture.Create<string>(),
                Posts = new System.Collections.Generic.List<Post>()
                {
                    new Post() { Date = "20150909", Text = Fixture.Create<string>(), Url = Guid.NewGuid().ToString()},
                    new Post() { Date = "20150909", Text = Fixture.Create<string>(), Url =  Guid.NewGuid().ToString()}
                }
            };

            using (var db = new EF7BloggContext())
            {
                db.ChangeTracker.TrackGraph(blog, c => c.Entry.State = EntityState.Added);

                db.SaveChanges();
            };

            using (var db = new EF7BloggContext())
            {
                var retrievedBlog = db
                    .Blogs.Include(b => b.Posts)
                    .Single(b => b.Id == blog.Id);
            };
        }
示例#3
0
        public void CanCreate3()
        {
            Post post;
            using (var uow = UoWFactory.Create())
            {
                var rep = uow.Create();
                var blogww = Fixture.Create<Blog>();
                post = Fixture.Create<Post>();

                var k = rep.Add<Post>(post);

                uow.Commit();
            }

            var blog = new Blog();
            blog.Name = Fixture.Create<string>();
            post.Text = Fixture.Create<string>();
            post.Blog = blog;
            blog.Posts.Add(post);

            post = new Post();
            post.Text = Fixture.Create<string>();
            post.Url = Guid.NewGuid().ToString();
            post.Blog = blog;
            blog.Posts.Add(post);

            using (var uow = UoWFactory.Create())
            {
                var repository = uow.Create();

                var updatedBlog = repository.UpdateGraph(blog);
                uow.Commit();
            }
        }
示例#4
0
        public void CanRetrieveBusinessObject()
        {
            using (var uow = UoWFactory.Create())
            {
                #region Arrange

                var rep = uow.Create();
                var blog = new Blog
                {
                    Name = Fixture.Create<string>()
                };

                var createdBlog = rep.Add<Blog>(blog);

                uow.Commit();

                #endregion Arrange

                #region Act

                //var retrievedBlog = rep.RetrieveById<Blog>(createdBlog.Id);

                #endregion Act

                #region Assert

                // TODO

                #endregion Assert
            }
        }
示例#5
0
        public void CanAddBusinessObject()
        {
            #region Arrange

            var blog = new Blog
            {
                Name = Fixture.Create<string>()
            };

            #endregion Arrange

            #region Act

            using (var uow = UoWFactory.Create())
            {
                var rep = uow.Create();

                var persistedBlog = rep.Add<Blog>(blog);

                uow.Commit();
            }

            #endregion Act

            #region Assert

            // TODO.

            #endregion Assert
        }
示例#6
0
        public void CanRetrieveBlogNonGeneric()
        {
            #region Arrange

            Blog createdBlog;
            Post post;
            using (var uow = UoWFactory.Create())
            {
                var rep = uow.Create();
                var blog = new Blog
                {
                    Name = Fixture.Create<string>()
                };

                post = new Post();
                post.Text = Fixture.Create<string>();
                post.Date = Fixture.Create<string>();
                post.Url = Guid.NewGuid().ToString();
                post.Blog = blog;

                blog.Posts.Add(post);

                createdBlog = rep.AddWithRelations(blog);

                uow.Commit();
            }

            #endregion Arrange

            #region Act

            Blog retrievedBlog = null;
            using (var uow = UoWFactory.Create())
            {
                var rep = uow.Create();
                retrievedBlog = rep.RetrieveBlogNonGeneric(createdBlog.Id);
            }

            #endregion Act

            #region Assert

            Assert.IsNotNull(retrievedBlog);

            #endregion Assert
        }
示例#7
0
        public void CanUpdateSelectedProperties()
        {
            #region Arrange

            var blog = new Blog();
            blog.Name = Fixture.Create<string>();
            using (var uow = UoWFactory.Create())
            {
                var repository = uow.Create();

                var updatedBlog = repository.UpdateGraph(blog);

                uow.Commit();
            }

            #endregion Arrange

            #region Act

            blog.Name = Fixture.Create<string>();
            blog.Author = Fixture.Create<string>();

            using (var uow1 = UoWFactory.Create())
            {
                var repository = uow1.Create();

                // Only name should be updated
                var updatedBlog = repository.Update(blog, p => p.Name);

                uow1.Commit();
            }

            #endregion Act

            #region Assert

            var uow2 = UoWFactory.Create();
            var r = uow2.Create();

            #endregion Assert
        }
        public void CanSelectDynamic()
        {
            using (var db = new EF7BloggContext())
            {
                var authorToSerachFor = Fixture.Create<string>();
                var blog = new Blog() { Name = Fixture.Create<string>(), Description = Fixture.Create<string>() };

                db.AddRange(new[] { blog });

                db.SaveChanges();

                var retrievedBlog = db.Set<Blog>()
                    .Include(p => p.Posts)
                    .ToList();
                //.Select(p => new { p.Author })
                //.ToList();
                //.SelectDynamic(new[] { nameof(Blog.Author) });

                foreach (var blog1 in retrievedBlog)
                {
                    Assert.IsNotNull(blog1);
                }
            }
        }
示例#9
0
        public void CanCreate6()
        {
            var blog = new Blog();
            blog.Name = Fixture.Create<string>();
            using (var uow = UoWFactory.Create())
            {
                var repository = uow.Create();

                var persistedBlog = repository.Add(blog);

                uow.Commit();
            }

            var post = new Post();
            post.Text = Fixture.Create<string>();
            post.Url = Guid.NewGuid().ToString();

            using (var uow = UoWFactory.Create())
            {
                var repository = uow.Create();

                var persistedPost = repository.Add(post);

                uow.Commit();
            }

            var post1 = new Post();
            post1.Text = Fixture.Create<string>();
            post1.Url = Guid.NewGuid().ToString();
            blog.Posts.AddRange(new List<Post>() { post, post1 });

            using (var uow = UoWFactory.Create())
            {
                var repository = uow.Create();

                var updatedBlog = repository.UpdateGraph(blog);

                uow.Commit();
            }
        }
示例#10
0
        public void CanRetrieveBusinessObjectDiconnected()
        {
            Blog createdBlog = null;

            #region Arrange

            using (var uow = UoWFactory.Create())
            {
                var rep = uow.Create();
                var blog = new Blog
                {
                    Name = Fixture.Create<string>()
                };

                createdBlog = rep.Add<Blog>(blog);

                uow.Commit();
            }

            #endregion Arrange

            #region Act

            using (var uow = UoWFactory.Create())
            {
                var rep = uow.Create();
                //var retrievedBlog = rep.Retrieve<Blog>(createdBlog.Id);
            }

            #endregion Act
        }
示例#11
0
        public void CanRetrieveByIdSelectedPropertiesFromBusinessObject()
        {
            using (var uow = UoWFactory.Create())
            {
                #region Arrange

                var rep = uow.Create();
                var blog = new Blog
                {
                    Name = Fixture.Create<string>(),
                    Posts = new List<Post>()
                    {
                        new Post() { Text =  Fixture.Create<string>(), Url = Fixture.Create<string>()}
                    }
                };

                var createdBlog = rep.AddWithRelations(blog);

                uow.Commit();

                #endregion Arrange

                #region Act

                var projector = rep.PropertySelectBuilder(blog)
                    .Select(p => p.Name)
                    .Include<Post>(m => m.Posts, p => p.Url)
                    .Build();

                var retrievedBlog = rep.RetrieveById<Blog>(createdBlog.Id, projector);

                #endregion Act

                #region Assert

                // TODO

                #endregion Assert
            }
        }
示例#12
0
        public void CanRetrieveBusinessObjectSelectedIntProperty()
        {
            #region Arrange

            Blog createdBlog;

            var blog = new Blog
            {
                Name = Fixture.Create<string>(),
                Description = Fixture.Create<string>()
            };

            #endregion Arrange

            #region Act

            using (var uow = UoWFactory.Create())
            {
                var rep = uow.Create();

                createdBlog = rep.Add(blog);

                uow.Commit();
            }

            #endregion Act

            #region Assert

            using (var uow = UoWFactory.Create())
            {
                var rep = uow.Create();

                var projector = rep.PropertySelectBuilder(blog)
                    .Select(p => p.Id)
                    .Build();

                var retrievedBlog = rep.RetrieveById(createdBlog.Id, projector);
            }

            #endregion Assert
        }
示例#13
0
        public void CanAddManyBusinessObjectWithoutRoundtrips()
        {
            #region Arrange

            var blog1 = new Blog() { Name = Fixture.Create<string>() };
            var blog2 = new Blog() { Name = Fixture.Create<string>() };
            var blog3 = new Blog() { Name = Fixture.Create<string>() };

            #endregion Arrange

            #region Act

            using (var uow = UoWFactory.Create())
            {
                var rep = uow.Create();

                // This will generate one batch sql command to the database.
                var persistedBlog = rep.Add<Blog>(new[] { blog1, blog2, blog3 });

                uow.Commit();
            }

            #endregion Act

            #region Assert

            // TODO

            #endregion Assert
        }
        public void CanAddWithRelations()
        {
            #region Arrange

            var blog = new Blog();
            blog.Name = Fixture.Create<string>();

            var post = new Post();
            post.Text = Fixture.Create<string>();
            post.Url = Guid.NewGuid().ToString();
            blog.Posts.Add(post);

            #endregion Arrange

            #region Act

            using (var uow = UoWFactory.Create())
            {
                var repository = uow.Create();

                var updatedBlog = repository.AddWithRelations(blog);
                uow.Commit();
            }

            #endregion Act

            #region Assert

            // TODO!

            #endregion Assert
        }
        public void CanAddWithRelations_Inconclusive()
        {
            Assert.Inconclusive("This implementation of retrieve is not going to be used");
            Post post;
            Blog createdBlog;
            using (var uow = UoWFactory.Create())
            {
                var rep = uow.Create();
                var blog = new Blog
                {
                    Name = Fixture.Create<string>()
                };

                post = new Post();
                post.Text = Fixture.Create<string>();
                post.Date = Fixture.Create<string>();
                post.Url = Guid.NewGuid().ToString();
                post.Blog = blog;

                blog.Posts.Add(post);

                post = new Post();
                post.Text = Fixture.Create<string>();
                post.Date = Fixture.Create<string>();
                post.Url = Guid.NewGuid().ToString();
                post.Blog = blog;

                blog.Posts.Add(post);

                createdBlog = rep.AddWithRelations(blog);

                uow.Commit();
            }

            using (var uow = UoWFactory.Create())
            {
                var rep = uow.Create();

                var retrievedBlogWithPosts = rep.RetrieveObsolete<Blog, dynamic>(
                    createdBlog.Id, p => new { ff = p.Name, ffff = p.Description, fff = p.Posts.Select(pp => pp.Text) });
            }
        }
        public void CanAddWithRelations2()
        {
            #region Arrange

            Blog createdBlog;
            Post post;

            var blog = new Blog
            {
                Name = Fixture.Create<string>(),
                Description = Fixture.Create<string>()
            };

            post = new Post()
            {
                Text = Fixture.Create<string>(),
                Date = Fixture.Create<string>(),
                Url = Guid.NewGuid().ToString(),
                Blog = blog,
            };

            var follower = new Follower()
            {
                Name = Fixture.Create<string>(),
                Blog = blog
            };

            blog.Posts.Add(post);
            blog.Followers.Add(follower);

            post = new Post();
            post.Text = Fixture.Create<string>();
            post.Date = Fixture.Create<string>();
            post.Url = Guid.NewGuid().ToString();
            post.Blog = blog;

            blog.Posts.Add(post);

            #endregion Arrange

            #region Act

            using (var uow = UoWFactory.Create())
            {
                var rep = uow.Create();

                createdBlog = rep.AddWithRelations(blog);

                uow.Commit();
            }

            #endregion Act

            #region Assert

            using (var uow = UoWFactory.Create())
            {
                var rep = uow.Create();

                var pp = rep.PropertySelectBuilder(blog)
                    .Select(m => m.Id, m => m.Name, m => m.Description)
                    .Include<Post>(p => p.Posts, p => p.Text, p => post.Date)
                    .Include<Follower>(m => m.Followers, p => p.Id, p => p.Name)
                    .Build();

                var retrievedBlog = rep.RetrieveById(createdBlog.Id, pp);
            }

            #endregion Assert
        }