コード例 #1
0
        public void Can_remove_product_by_id()
        {
            Product[] list =
            {
                CreateProduct("Car yellow",         5, 15000),
                CreateProduct("car blue",           7, 20000),
                CreateProduct("apple oneType",      5,    40),
                CreateProduct("Apple anotherType",  5,    37),
                CreateProduct("apple",             25, 40)
            };
            CreateInitialData(list);
            var product    = list.Last();
            var repository = ProductRepository;

            repository.Delete(product.Id);
            var notExistId = product.Id + 1000;

            Sessionfactory.GetCurrentSession().Flush();

            using (var session = Sessionfactory.OpenSession())
            {
                var anObject = session.Get <Product>(product.Id);
                Assert.IsNull(anObject);
                repository.Delete(notExistId);
            }
        }
コード例 #2
0
        public void Can_update_product()
        {
            var product = new Product {
                Name = "Car yellow", Quantity = 5, Price = 15000
            };

            CreateInitialData(new List <Product> {
                product
            });
            var expected = new Product {
                Id = product.Id, Name = "Car blue", Quantity = 3, Price = 20000
            };
            var repository = ProductRepository;

            repository.Update(expected);

            Sessionfactory.GetCurrentSession().Transaction.Commit();
            Sessionfactory.GetCurrentSession().Flush();
            using (var session = Sessionfactory.OpenSession())
            {
                var actual = session.Get <Product>(product.Id);
                Assert.AreEqual(expected.Name, actual.Name);
                Assert.AreEqual(expected.Quantity, actual.Quantity);
                Assert.AreEqual(expected.Price, actual.Price);
            }
        }
コード例 #3
0
        public void Delete(int id)
        {
            var product = Get(id);

            if (product == null)
            {
                return;
            }

            using (var session = Sessionfactory.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    try
                    {
                        session.Delete(product);
                        transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        Log.Error($"Exception occured when system tried to delete the object by id= {id}", e);
                        try
                        {
                            transaction.Rollback();
                        }
                        catch (HibernateException exception)
                        {
                            Log.Error("Exception occurred when system tried to roll back transaction", exception);
                        }
                        throw;
                    }
                }
            }
        }
コード例 #4
0
        public void Can_update_product()
        {
            var customer = new Customer {
                Name = "Bob", Email = "*****@*****.**", Card = "555555"
            };

            CreateInitialData(new List <Customer> {
                customer
            });
            var expected = new Customer {
                Id = customer.Id, Name = "Clark", Email = "*****@*****.**", Card = "111111"
            };
            var repository = CustomerRepository;

            repository.Update(expected);

            Sessionfactory.GetCurrentSession().Transaction.Commit();
            Sessionfactory.GetCurrentSession().Flush();
            using (var session = Sessionfactory.OpenSession())
            {
                var actual = session.Get <Customer>(customer.Id);
                Assert.AreEqual(expected.Name, actual.Name);
                Assert.AreEqual(expected.Email, actual.Email);
                Assert.AreEqual(expected.Card, actual.Card);
            }
        }
コード例 #5
0
        private void CreateInitialData(IList <Product> products)
        {
            var initialList = products;

            using (var session = Sessionfactory.OpenSession())
                using (var transaction = session.BeginTransaction())
                {
                    foreach (var product in initialList)
                    {
                        session.Save(product);
                    }
                    transaction.Commit();
                }
        }
コード例 #6
0
        private void CreateInitialData(IList <Customer> customers)
        {
            var initialList = customers;

            using (var session = Sessionfactory.OpenSession())
                using (var transaction = session.BeginTransaction())
                {
                    foreach (var customer in initialList)
                    {
                        session.Save(customer);
                    }
                    transaction.Commit();
                }
        }
コード例 #7
0
        public void Can_create_new_product()
        {
            var expected   = CreateProduct("Car");
            var repository = ProductRepository;

            repository.Create(expected);
            Sessionfactory.GetCurrentSession().Transaction.Commit();
            Sessionfactory.GetCurrentSession().Flush();
            using (var session = Sessionfactory.OpenSession())
            {
                var actual = session.Get <Product>(expected.Id);

                Assert.IsNotNull(actual);
                Assert.AreNotSame(expected, actual);
                CompareProducts(expected, actual);
            }
        }
コード例 #8
0
        public void SetupContext()
        {
            var repository = OrderRepository;
            var list       = repository.List();

            if (list == null)
            {
                return;
            }
            using (var session = Sessionfactory.OpenSession())
                using (var transaction = session.BeginTransaction())
                {
                    foreach (var order in list)
                    {
                        session.Delete(order);
                    }
                    transaction.Commit();
                }
        }
コード例 #9
0
        public void Can_create_new_customer()
        {
            var expected = new Customer {
                Name = "Bob", Email = "*****@*****.**", Card = "555555"
            };
            var repository = CustomerRepository;

            repository.Create(expected);
            Sessionfactory.GetCurrentSession().Transaction.Commit();
            Sessionfactory.GetCurrentSession().Flush();

            using (var session = Sessionfactory.OpenSession())
            {
                var actual = session.Get <Customer>(expected.Id);

                Assert.IsNotNull(actual);
                Assert.AreNotSame(expected, actual);
                CompareCustomers(expected, actual);
            }
        }
コード例 #10
0
        public void Can_create_order()
        {
            var expected = new Order {
                Total = 20000, SaleDate = DateTime.Now, LineItems = new List <LineItem> {
                    new LineItem {
                        ProductId = 2, Name = "Car", Price = 10000, Quantity = 2
                    }
                }
            };
            var repository = OrderRepository;

            repository.Create(expected);
            Sessionfactory.GetCurrentSession().Transaction.Commit();
            Sessionfactory.GetCurrentSession().Flush();
            using (var session = Sessionfactory.OpenSession())
            {
                var actual = session.Get <Order>(expected.Id);

                Assert.IsNotNull(actual);
                Assert.AreNotSame(expected, actual);
                CompareOrders(expected, actual);
            }
        }
コード例 #11
0
        public Product GetByName(string name)
        {
            Product product;

            if (string.IsNullOrEmpty(name))
            {
                return(null);
            }
            using (var session = Sessionfactory.OpenSession())
            {
                try
                {
                    product = session.QueryOver <Product>().Where(m => m.Name == name).SingleOrDefault();
                }
                catch (Exception e)
                {
                    Log.Error(
                        "Exception occured when system tried to get the product by name", e);
                    throw;
                }
            }
            return(product);
        }
コード例 #12
0
 public void SetupContext()
 {
     Sessionfactory.OpenSession().CreateQuery("DELETE FROM Product ").ExecuteUpdate();
 }