コード例 #1
0
ファイル: OtherDbPost.cs プロジェクト: ralescano/castle
		public OtherDbPost(OtherDbBlog blog, String title, String contents, String category)
		{
			_blog = blog;
			_title = title;
			_contents = contents;
			_category = category;
		}
コード例 #2
0
        public void SimpleCase()
        {
            Blog blog = new Blog();

            blog.Name   = "hammett's blog";
            blog.Author = "hamilton verissimo";
            blog.Save();

            SqlConnection conn = CreateSqlConnection();

            using (conn)
            {
                conn.Open();

                using (new DifferentDatabaseScope(conn))
                {
                    blog.Create();
                }
            }

            Blog[] blogs = Blog.FindAll();
            Assert.IsNotNull(blogs);
            Assert.AreEqual(1, blogs.Length);

            OtherDbBlog[] blogs2 = OtherDbBlog.FindAll();
            Assert.IsNotNull(blogs2);
            Assert.AreEqual(1, blogs2.Length);
        }
コード例 #3
0
        public void MoreThanOneConnectionWithinTheSessionScope()
        {
            using (var conn = CreateSqlConnection())
                using (var conn2 = CreateSqlConnection2())
                    using (new SessionScope())
                    {
                        foreach (var connection in new [] { conn, conn2 })
                        {
                            connection.Open();

                            using (new DifferentDatabaseScope(connection))
                            {
                                var blog = new Blog {
                                    Name = "hammett's blog", Author = "hamilton verissimo"
                                };
                                blog.Create();
                            }
                        }
                    }

            using (new SessionScope()) {
                var blogs2 = OtherDbBlog.FindAll().ToArray();
                Assert.IsNotNull(blogs2);
                Assert.AreEqual(1, blogs2.Length);

                var blogs = Blog.FindAll().ToArray();
                Assert.IsNotNull(blogs);
                Assert.AreEqual(1, blogs.Length);
            }
        }
コード例 #4
0
 public OtherDbPost(OtherDbBlog blog, String title, String contents, String category)
 {
     _blog     = blog;
     _title    = title;
     _contents = contents;
     _category = category;
 }
コード例 #5
0
        public void SimpleCase()
        {
            var blog = new Blog {
                Name = "hammett's blog", Author = "hamilton verissimo"
            };

            using (new SessionScope()) {
                blog.Save();
            }

            using (var conn = CreateSqlConnection2())
            {
                conn.Open();

                using (new DifferentDatabaseScope(conn))
                {
                    blog.Create();
                }
            }

            using (new SessionScope()) {
                var blogs = Blog.FindAll().ToArray();
                Assert.IsNotNull(blogs);
                Assert.AreEqual(1, Blog.Count());

                var blogs2 = OtherDbBlog.FindAll().ToArray();
                Assert.IsNotNull(blogs2);
                Assert.AreEqual(1, OtherDbBlog.Count());
            }
        }
コード例 #6
0
        public void MoreThanOneConnectionWithinTheSessionScope()
        {
            SqlConnection conn  = CreateSqlConnection();
            SqlConnection conn2 = CreateSqlConnection2();

            using (new SessionScope())
            {
                foreach (SqlConnection connection in new SqlConnection[] { conn, conn2 })
                {
                    connection.Open();

                    using (new DifferentDatabaseScope(connection))
                    {
                        Blog blog = new Blog();
                        blog.Name   = "hammett's blog";
                        blog.Author = "hamilton verissimo";
                        blog.Create();
                    }
                }
            }

            OtherDbBlog[] blogs2 = OtherDbBlog.FindAll();
            Assert.IsNotNull(blogs2);
            Assert.AreEqual(1, blogs2.Length);

            Blog[] blogs = Blog.FindAll();
            Assert.IsNotNull(blogs);
            Assert.AreEqual(1, blogs.Length);
        }
コード例 #7
0
        public void UsingTransactionScopeWithRollback()
        {
            SqlConnection conn = CreateSqlConnection2();
            ISession      session1, session2;
            ITransaction  trans1, trans2;

            using (TransactionScope scope = new TransactionScope())
            {
                Blog blog = new Blog {
                    Name = "hammett's blog", Author = "hamilton verissimo"
                };
                blog.Save();

                session1 = blog.GetCurrentSession();
                trans1   = blog.GetCurrentSession().Transaction;
                Assert.IsNotNull(session1);
                Assert.IsNotNull(session1.Transaction);
                Assert.IsFalse(session1.Transaction.WasCommitted);
                Assert.IsFalse(session1.Transaction.WasRolledBack);

                blog.Evict();
                conn.Open();

                using (new DifferentDatabaseScope(conn))
                {
                    blog.Create();

                    session2 = blog.GetCurrentSession();
                    trans2   = blog.GetCurrentSession().Transaction;
                    Assert.IsNotNull(session2);

                    Assert.IsFalse(Object.ReferenceEquals(session1, session2));

                    Assert.IsNotNull(session2.Transaction);
                    Assert.IsFalse(session2.Transaction.WasCommitted);
                    Assert.IsFalse(session2.Transaction.WasRolledBack);

                    scope.VoteRollBack();
                }
            }

            Assert.IsFalse(session1.IsOpen);
            Assert.IsFalse(session2.IsOpen);
            Assert.IsFalse(trans1.WasCommitted);
            Assert.IsTrue(trans1.WasRolledBack);
            Assert.IsFalse(trans2.WasCommitted);
            Assert.IsTrue(trans2.WasRolledBack);

            using (new SessionScope()) {
                var blogs = Blog.FindAll().ToArray();
                Assert.IsNotNull(blogs);
                Assert.AreEqual(0, Blog.Count());

                var blogs2 = OtherDbBlog.FindAll().ToArray();
                Assert.IsNotNull(blogs2);
                Assert.AreEqual(0, OtherDbBlog.Count());
            }
        }
コード例 #8
0
        public void UsingTransactionScope()
        {
            SqlConnection conn = CreateSqlConnection();
            ISession      session1, session2;
            ITransaction  trans1, trans2;

            using (new TransactionScope())
            {
                Blog blog = new Blog();
                blog.Name   = "hammett's blog";
                blog.Author = "hamilton verissimo";
                blog.Save();

                session1 = blog.CurrentSession;
                trans1   = blog.CurrentSession.Transaction;
                Assert.IsNotNull(session1);
                Assert.IsNotNull(session1.Transaction);
                Assert.IsFalse(session1.Transaction.WasCommitted);
                Assert.IsFalse(session1.Transaction.WasRolledBack);

                conn.Open();

                using (new DifferentDatabaseScope(conn))
                {
                    blog.Create();

                    session2 = blog.CurrentSession;
                    trans2   = blog.CurrentSession.Transaction;
                    Assert.IsNotNull(session2);

                    Assert.IsFalse(Object.ReferenceEquals(session1, session2));

                    Assert.IsNotNull(session2.Transaction);
                    Assert.IsFalse(session2.Transaction.WasCommitted);
                    Assert.IsFalse(session2.Transaction.WasRolledBack);
                }
            }

            Assert.IsFalse(session1.IsOpen);
            Assert.IsFalse(session2.IsOpen);
            Assert.IsTrue(trans1.WasCommitted);
            Assert.IsFalse(trans1.WasRolledBack);
            Assert.IsTrue(trans2.WasCommitted);
            Assert.IsFalse(trans2.WasRolledBack);

            Blog[] blogs = Blog.FindAll();
            Assert.IsNotNull(blogs);
            Assert.AreEqual(1, blogs.Length);

            OtherDbBlog[] blogs2 = OtherDbBlog.FindAll();
            Assert.IsNotNull(blogs2);
            Assert.AreEqual(1, blogs2.Length);
        }
コード例 #9
0
        public void UsingSessionScope()
        {
            ISession session1, session2;

            using (new SessionScope())
            {
                Blog blog = new Blog();
                blog.Name   = "hammett's blog";
                blog.Author = "hamilton verissimo";
                blog.Save();

                session1 = blog.CurrentSession;
                Assert.IsNotNull(session1);

                SqlConnection conn = CreateSqlConnection();

                using (conn)
                {
                    conn.Open();

                    using (new DifferentDatabaseScope(conn))
                    {
                        blog.Create();

                        session2 = blog.CurrentSession;
                        Assert.IsNotNull(session2);

                        Assert.IsFalse(Object.ReferenceEquals(session1, session2));
                    }
                }
            }

            Blog[] blogs = Blog.FindAll();
            Assert.IsNotNull(blogs);
            Assert.AreEqual(1, blogs.Length);

            OtherDbBlog[] blogs2 = OtherDbBlog.FindAll();
            Assert.IsNotNull(blogs2);
            Assert.AreEqual(1, blogs2.Length);
        }
コード例 #10
0
        public void UsingSessionScope()
        {
            ISession session1, session2;

            using (var conn = CreateSqlConnection2()) {
                conn.Open();

                using (new SessionScope()) {
                    Blog blog = new Blog {
                        Name = "hammett's blog", Author = "hamilton verissimo"
                    };
                    blog.Save();

                    session1 = blog.GetCurrentSession();
                    Assert.IsNotNull(session1);
                    blog.Evict();


                    using (new DifferentDatabaseScope(conn)) {
                        blog.Create();

                        session2 = blog.GetCurrentSession();
                        Assert.IsNotNull(session2);

                        Assert.IsFalse(Object.ReferenceEquals(session1, session2));
                    }
                }
            }

            using (new SessionScope()) {
                var blogs = Blog.FindAll().ToArray();
                Assert.IsNotNull(blogs);
                Assert.AreEqual(1, Blog.Count());

                var blogs2 = OtherDbBlog.FindAll().ToArray();
                Assert.IsNotNull(blogs2);
                Assert.AreEqual(1, OtherDbBlog.Count());
            }
        }
コード例 #11
0
        public void SequenceOfTransactions()
        {
            var      conn = CreateSqlConnection2();
            ISession session1, session2;

            ITransaction trans1, trans2;

            using (new SessionScope())
            {
                using (new TransactionScope())
                {
                    var blog = new Blog {
                        Name = "hammett's blog", Author = "hamilton verissimo"
                    };
                    blog.Save();

                    session1 = blog.GetCurrentSession();
                    Assert.IsNotNull(session1);
                    trans1 = session1.Transaction;
                    Assert.IsNotNull(trans1);
                    Assert.IsFalse(trans1.WasCommitted);
                    Assert.IsFalse(trans1.WasRolledBack);

                    conn.Open();

                    using (new DifferentDatabaseScope(conn))
                    {
                        blog = new Blog {
                            Name = "hammett's blog", Author = "hamilton verissimo"
                        };
                        blog.Save();

                        session2 = blog.GetCurrentSession();
                        Assert.IsNotNull(session2);
                        trans2 = session2.Transaction;
                        Assert.IsFalse(Object.ReferenceEquals(session1, session2));

                        Assert.IsNotNull(session2.Transaction);
                        Assert.IsFalse(session2.Transaction.WasCommitted);
                        Assert.IsFalse(session2.Transaction.WasRolledBack);
                    }

                    using (new DifferentDatabaseScope(conn))
                    {
                        blog = new Blog {
                            Name = "hammett's blog", Author = "hamilton verissimo"
                        };
                        blog.Save();

                        session2 = blog.GetCurrentSession();
                        Assert.IsNotNull(session2);

                        Assert.IsFalse(Object.ReferenceEquals(session1, session2));

                        Assert.IsNotNull(trans2);
                        Assert.IsFalse(trans2.WasCommitted);
                        Assert.IsFalse(trans2.WasRolledBack);
                    }
                }

                conn.Close();

                using (new TransactionScope())
                {
                    var blog = new Blog {
                        Name = "another blog", Author = "erico verissimo"
                    };
                    blog.Save();
                }
            }

            Assert.IsFalse(session1.IsOpen);
            Assert.IsFalse(session2.IsOpen);
            Assert.IsTrue(trans1.WasCommitted);
            Assert.IsFalse(trans1.WasRolledBack);
            Assert.IsTrue(trans2.WasCommitted);
            Assert.IsFalse(trans2.WasRolledBack);

            using (new SessionScope()) {
                var blogs = Blog.FindAll().ToArray();
                Assert.IsNotNull(blogs);
                Assert.AreEqual(2, Blog.Count());

                var blogs2 = OtherDbBlog.FindAll().ToArray();
                Assert.IsNotNull(blogs2);
                Assert.AreEqual(2, OtherDbBlog.Count());
            }
        }
コード例 #12
0
        public void UsingSessionAndTransactionScope()
        {
            ISession session1, session2;

            using (var conn = CreateSqlConnection2()) {
                conn.Open();
                using (new SessionScope()) {
                    using (new TransactionScope()) {
                        var blog = new Blog {
                            Name = "hammett's blog", Author = "hamilton verissimo"
                        };
                        blog.Save();

                        session1 = blog.GetCurrentSession();
                        Assert.IsNotNull(session1);
                        Assert.IsNotNull(session1.Transaction);
                        Assert.IsFalse(session1.Transaction.WasCommitted);
                        Assert.IsFalse(session1.Transaction.WasRolledBack);


                        using (new DifferentDatabaseScope(conn)) {
                            blog = new Blog {
                                Name = "hammett's blog", Author = "hamilton verissimo"
                            };
                            blog.Save();

                            session2 = blog.GetCurrentSession();
                            Assert.IsNotNull(session2);
                            Assert.IsFalse(Object.ReferenceEquals(session1, session2));

                            Assert.IsNotNull(session2.Transaction);
                            Assert.IsFalse(session2.Transaction.WasCommitted);
                            Assert.IsFalse(session2.Transaction.WasRolledBack);
                        }

                        using (new DifferentDatabaseScope(conn)) {
                            blog = new Blog {
                                Name = "hammett's blog", Author = "hamilton verissimo"
                            };
                            blog.Save();

                            session2 = blog.GetCurrentSession();
                            Assert.IsNotNull(session2);

                            Assert.IsFalse(Object.ReferenceEquals(session1, session2));

                            Assert.IsNotNull(session2.Transaction);
                            Assert.IsFalse(session2.Transaction.WasCommitted);
                            Assert.IsFalse(session2.Transaction.WasRolledBack);
                        }
                    }
                }
            }

            Assert.IsFalse(session1.IsOpen);
            Assert.IsFalse(session2.IsOpen);

            Assert.IsFalse(session1.Transaction.IsActive);
            Assert.IsFalse(session2.Transaction.IsActive);

            using (new SessionScope()) {
                var blogs = Blog.FindAll().ToArray();
                Assert.IsNotNull(blogs);
                Assert.AreEqual(1, Blog.Count());

                var blogs2 = OtherDbBlog.FindAll().ToArray();
                Assert.IsNotNull(blogs2);
                Assert.AreEqual(2, OtherDbBlog.Count());
            }
        }