protected override BlogApplicationCompositeRoot CreateCompositeRoot()
 {
     return(new BlogApplicationCompositeRoot
            (
                MicrosoftSqlServerRepository.Create(),
                new AttachmentArchiveService()
            ));
 }
        public void MicrosoftSqlServerRepositoryTest()
        {
            var repository = MicrosoftSqlServerRepository.Create();

            using (var connection = repository.OpenConnection(@"Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=master;Integrated Security=SSPI;"))
                using (var transaction = repository.BeginTransaction(connection))
                {
                    var objs = repository.Load <TableInfo>(connection,
                                                           transaction,
                                                           @"
                        WITH Query AS
                        (
	                        SELECT ROW_NUMBER() OVER(ORDER BY name DESC) AS RowNumber, name, object_id
	                        FROM sys.columns
                        )
                        SELECT RowNumber, name, object_id AS ObjectId
                        FROM Query
                        WHERE RowNumber BETWEEN 1 AND 10000

                    ", null);
                }
        }
        public void CanPerformAllCTDataFunctions()
        {
            var blogApplicationCompositeRoot = new BlogApplicationCompositeRoot
                                               (
                MicrosoftSqlServerRepository.Create()
                                               );

            blogApplicationCompositeRoot.SetConnectionStrings
            (
                _masterDbConnectionString,
                "Data Source=(LocalDb)\\MSSQLLocalDB;Initial Catalog=BlogDb2;Integrated Security=SSPI;"
            );

            var applicationPath             = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            var createDatabaseSqlScriptFile = Path.Combine(applicationPath, "000-BlogDatabase2.sql");
            var createDatabaseSql           = File.ReadAllText(createDatabaseSqlScriptFile);

            blogApplicationCompositeRoot.CreateDatabase(createDatabaseSql);
            blogApplicationCompositeRoot.SetupDatabase();

            var random = new Random();

            for (int b = 0; b < 100; b++)
            {
                var r       = random.Next(-1000, 1000);
                var newBlog = blogApplicationCompositeRoot.Blogs.CreateNewBlog
                              (
                    $"Blog Name {r}",
                    r < 0,
                    DateTime.Now.AddDays(r),
                    r > 0 ? BlogType.Personal : BlogType.Public,
                    r,
                    (decimal) new Random().NextDouble() * r
                              );

                for (int p = 0; p < 10; p++)
                {
                    r = random.Next(-1000, 1000);
                    var newPost = newBlog.Posts.CreateNewPost
                                  (
                        $"Post {r}",
                        $"Text post {r}"
                                  );

                    for (int c = 0; c < 5; c++)
                    {
                        r = random.Next(-1000, 1000);
                        newPost.Comments.CreateNewComment
                        (
                            $"Comment {r}"
                        );
                    }
                }
            }

            blogApplicationCompositeRoot.Blogs.SaveAll(true);
            Assert.IsTrue(blogApplicationCompositeRoot.Blogs.Blogs.Values.All
                          (
                              b => b.Id != 0 &&
                              b.Id != b.OriginalId &&
                              b.State == CompositeState.Unchanged
                          ));

            Assert.IsTrue(!blogApplicationCompositeRoot.Blogs.Blogs.Values.Any(b => b.Name == "CT Blog"));
            blogApplicationCompositeRoot.Blogs.LoadBlog("CT Blog");
            Assert.IsTrue(blogApplicationCompositeRoot.Blogs.Blogs.Values.Any(b => b.Name == "CT Blog" && b.State == CompositeState.Unchanged));

            var ctBlog = blogApplicationCompositeRoot.Blogs.Blogs.Values.Single(b => b.Name == "CT Blog");

            ctBlog.PublishDate = DateTime.Now;
            ctBlog.Rating      = 1;
            ctBlog.BlogType    = BlogType.Public;
            ctBlog.Earnings    = 0;

            foreach (var blog in blogApplicationCompositeRoot.Blogs.Blogs.Values)
            {
                blog.BlogType = BlogType.Personal;
            }

            blogApplicationCompositeRoot.Blogs.SaveAll(true);

            var repository = blogApplicationCompositeRoot.GetService <IMicrosoftSqlServerRepository>();

            using var connection = repository.OpenConnection(blogApplicationCompositeRoot.BlogDbConnectionString);
            var updatedBlogs = repository.Load(connection, null, @"SELECT * FROM Blog", null, () => new Blog());

            Assert.IsTrue(updatedBlogs.All(b => b.BlogType == BlogType.Personal));

            blogApplicationCompositeRoot.Blogs.Blogs.Values.Single(b => b.Name == "CT Blog").Remove();
            blogApplicationCompositeRoot.Blogs.SaveAll(true);
            var removedBlogsShouldBeEmpty = repository.Load(connection, null, @"SELECT * FROM Blog WHERE Name = 'CT Blog'", null, () => new Blog());

            Assert.IsTrue(removedBlogsShouldBeEmpty.Count() == 0);
        }