public void Common_CanUpdate(IRepository<Product> db)
        {
            // Arrange

            var px = new Product
                {
                    ProductName = "Bumble Bee",
                    Category = "Autobots",
                    MinimumPrice = 8
                };

            px.PriceList =
                new List<ProductPrice>()
                {
                    new ProductPrice { Product = px, Price = 234, EffectiveDate = DateTime.Today },
                    new ProductPrice { Product = px, Price = 300, EffectiveDate = DateTime.Today.AddDays(100) }
                };
            db.SaveGraph(px);

            // int n = px.PriceList[0].ProductPriceId;

            // simulate web(i.e. stateless)

            int productPriceId = db.Get(px.ProductId).PriceList[0].ProductPriceId;

            var fromWeb =
                    new Product
                    {
                        ProductId = px.ProductId,
                        ProductName = "hahaha", // px.ProductName + "---" + Guid.NewGuid().ToString(),
                        Category = px.Category,
                        MinimumPrice = px.MinimumPrice,
                        RowVersion = db.Get(px.ProductId).RowVersion,
                        PriceList = new List<ProductPrice>()

                    };

            fromWeb.PriceList.Add(new ProductPrice { Product = fromWeb, ProductPriceId = productPriceId, Price = 767676, EffectiveDate = DateTime.Today });
            fromWeb.PriceList.Add(new ProductPrice { Product = fromWeb, Price = 2148, EffectiveDate = DateTime.Today });
            fromWeb.PriceList.Add(new ProductPrice { Product = fromWeb, Price = 2048, EffectiveDate = DateTime.Today });
            fromWeb.PriceList.Add(new ProductPrice { Product = fromWeb, Price = 222, EffectiveDate = DateTime.Today });

            /*fromWeb.PriceList.Add(new ProductPrice { Product = fromWeb, Price = 888, EffectiveDate = DateTime.Today });
            fromWeb.PriceList.Add(new ProductPrice { Product = fromWeb, Price = 222, EffectiveDate = DateTime.Today });*/

            // Act
            string expecting = "Bumble Bee Battle Mode hickhickhick";
            fromWeb.ProductName = expecting;
            db.SaveGraph(fromWeb);

            // Assert
            Assert.AreEqual(expecting, db.Get(fromWeb.ProductId).ProductName);
            // Assert.AreNotEqual(px.ProductName, db.Get(fromWeb.ProductId).ProductName);
        }
        Question Refactored_Common_SaveGraph_Arrange_Create(IRepository<Question> repo)
        {
            var importantQuestion = PopulateQuestion();

            repo.SaveGraph(importantQuestion);

            return importantQuestion;
        }
        void Common_CanSaveHeaderDetail_ThenHeaderOnly(IRepository<Product> db)
        {
            // NHibernate.ISession xxx = NhModelsMapper.GetSession(connectionString);

            // Arrange
            var px = new Product
            {
                ProductName = "Optimus",
                Category = "Autobots",
                MinimumPrice = 7
            };
            px.PriceList =
                new List<ProductPrice>()
                {
                    new ProductPrice { Product = px, Price = 777, EffectiveDate = DateTime.Today },
                    new ProductPrice { Product = px, Price = 888, EffectiveDate = DateTime.Today },
                    new ProductPrice { Product = px, Price = 999, EffectiveDate = DateTime.Today },
                    new ProductPrice { Product = px, Price = 222, EffectiveDate = DateTime.Today },

                };

            // Act
            db.SaveGraph(px);
            // xxx.Merge(px);

            byte[] rv = px.RowVersion;

            Assert.AreEqual(4, px.PriceList.Count());

            px = db.GetEager(px.ProductId);

            Assert.AreEqual("Optimus", px.ProductName);
            px.ProductName = px.ProductName + "?";
            px.PriceList[2].Price = 333;
            Assert.AreEqual(4, px.PriceList.Count);

            db.SaveGraph(px);

            Assert.AreNotEqual(0, px.ProductId);
            CollectionAssert.AreNotEqual(px.RowVersion, rv);

            // Assert

            Assert.AreEqual(px.ProductName, px.ProductName);
            Assert.AreNotEqual(0, px.ProductId);
            Assert.AreEqual(4, px.PriceList.Count);

            // Arrange

            Assert.AreNotEqual(0, px.ProductId);

            /* triggers concurrency exception
            byte[] x = px.RowVersion;
            x[0] += 1;
            px.RowVersion = x;
            */

            var py = new Product
            {
                ProductId = px.ProductId,
                ProductName = "Optimus",
                Category = "Autobotszx",
                MinimumPrice = 7,

                RowVersion = px.RowVersion

                // empty list is incompatible with cascade=all-delete-orphan
            };

            int pxid = px.ProductId;
            db.Save(py);

            Product pz = db.Get(px.ProductId);
            // throw new Exception(py.ProductId + " " + px.ProductId + " " + pxid);
            Assert.AreEqual(px.ProductId, py.ProductId);

            Assert.IsNotNull(pz.PriceList);
            Assert.AreEqual(4, pz.PriceList.Count);
        }
        void Common_Orm_SaveGraph_Can_Update(IRepository<Question> repo)
        {
            // Arrange
            Question importantQuestion = Refactored_Common_SaveGraph_Arrange_Create(repo);
            string expectedText = "number 9, number 9, number 9...";

            // Act
            int questionId = importantQuestion.QuestionId;
            byte[] rowVersion = importantQuestion.RowVersion;

            Question retrievedQuestion = repo.GetEager(questionId);

            int originalAnswersCount = retrievedQuestion.Answers.Count;
            // throw new Exception(retrievedQuestion.GetType().ToString());

            retrievedQuestion.Text = "Hello";
            retrievedQuestion.Answers.Single(x => x.Poster == "John").Text = expectedText;

            var z = retrievedQuestion.Answers.Single(x => x.Poster == "John");
            z.Text = expectedText;

            var a = retrievedQuestion.Answers.Single(x => x.Poster == "Paul");
            retrievedQuestion.Answers.Remove(a);

            repo.SaveGraph(retrievedQuestion); // save the whole object graph

            Question retrievedMergedQuestion = repo.GetEager(questionId);

            // Assert
            Assert.AreNotSame(importantQuestion, retrievedQuestion);
            Assert.AreNotSame(retrievedQuestion, retrievedMergedQuestion);

            Assert.AreEqual(expectedText, retrievedMergedQuestion.Answers.Single(x => x.Poster == "John").Text);
            Assert.AreEqual("Hello", retrievedMergedQuestion.Text);

            Assert.AreEqual(3, originalAnswersCount);
            Assert.AreEqual(2, retrievedMergedQuestion.Answers.Count);
        }
        void Common_CanSaveHeaderDetail(IRepository<Product> db)
        {
            // Arrange
            var px = new Product
            {
                ProductName = "Optimus",
                Category = "Autobots",
                MinimumPrice = 7
            };
            px.PriceList =
                new List<ProductPrice>()
                {
                    new ProductPrice { Product = px, Price = 777, EffectiveDate = DateTime.Today },
                    new ProductPrice { Product = px, Price = 888, EffectiveDate = DateTime.Today },
                    new ProductPrice { Product = px, Price = 999, EffectiveDate = DateTime.Today },
                    new ProductPrice { Product = px, Price = 222, EffectiveDate = DateTime.Today },

                };

            // Act
            db.SaveGraph(px);

            Assert.AreEqual(4, px.PriceList.Count());

            px = db.GetEager(px.ProductId);

            Assert.AreEqual("Optimus", px.ProductName);
            px.ProductName = px.ProductName + "!";
            px.PriceList[2].Price = 333;
            Assert.AreEqual(4, px.PriceList.Count);

            db.SaveGraph(px);

            // Assert

            Assert.AreEqual(px.ProductName, px.ProductName);
            Assert.AreEqual(px.ProductId, px.ProductId);
            Assert.AreEqual(4, px.PriceList.Count);
        }