Exemplo n.º 1
0
        public static void InsertSomeData(ISessionFactory sessionFactory)
        {
            using (var session = sessionFactory.OpenSession())
            using (var tx = session.BeginTransaction())
            {
                var israel = new Country { Name = "Israel" };
                var telaviv = new City { Name = "TelAviv" };
                israel.AddCity(telaviv);

                session.Save(israel);

                var address = new Address { City = telaviv, Number = 100, PostalCode = 101, Street = "200" };
                var matan = new Person
                {
                    Gender = Gender.Male,
                    Name = "Matan",
                    HomeAddress = address
                };

                session.Save(matan);

                var store = new Store { Contact = matan, Name = "Main Office" };
                var branch1 = new Branch
                {
                    Address = address,
                    Manager = matan,
                    Name = "branch1",
                    Location = (Point)Default.GeometryFactory.CreatePoint(new Coordinate(32, 34))
                };

                var branch2 = new Branch
                {
                    Address = address,
                    Manager = matan,
                    Name = "branch2",
                    Location = (Point)Default.GeometryFactory.CreatePoint(new Coordinate(32.5, 34.5))
                };

                store.AddBranch(branch1);
                store.AddBranch(branch2);

                var milk = new Product { Name = "milk", UnitPrice = 10, Description = "moo" };
                var bread = new Product { Name = "bread", UnitPrice = 8, Description = "poof" };

                session.Save(milk);
                session.Save(bread);

                branch1.AddProductToInventory(milk, 10);
                branch1.AddProductToInventory(bread, 15);
                branch2.AddProductToInventory(milk, 20);

                session.Save(store);
                tx.Commit();
            }
        }
Exemplo n.º 2
0
        public virtual void AddProductToInventory(Product product, int amount)
        {
            Validate.ThatArgumentNotNull(() => product);

            var item = _inventory.Where(ii => ii.Product.Id == product.Id).FirstOrDefault();

            if (item == null)
                _inventory.Add(new InventoryItem { Amount = amount, Product = product, Branch = this });
            else
                item.Amount += amount;
        }
Exemplo n.º 3
0
        public void product_exists_in_db_use_existing_product()
        {
            var p = new Product
                        {
                            Description = "desc",
                            Name = "name",
                            UnitPrice = 10,
                        };

            Session.Save(p);
            Session.Flush();
            Session.Clear();

            var dto = Mapper.Map<Product, ProductDto>(p);
            dto.name = "newName";
            var entity = Mapper.Map<ProductDto, Product>(dto);
            Session.Flush();
            Session.Clear();

            var result = Session.Get<Product>(p.Id);
            Assert.That(dto.CategoryDescription, Is.EqualTo("desc"));
            Assert.That(result.Name, Is.EqualTo("newName"));
            Assert.That(result.Description, Is.EqualTo("desc"));
        }