public void CreateOrder_should_not_execute_more_than_one_query() { Customer customer; using (var session = new SessionProvider().GetSession()) using (var transaction = session.BeginTransaction()) { customer = new Customer { Name = Guid.NewGuid().ToString(), }; session.Save(customer); transaction.Commit(); } using (var session = new SessionProvider().GetSession()) using (var transaction = session.BeginTransaction()) { var makeOrder = new MakeOrder(session); var makeOrderCommand = new MakeOrderCommand { CustomerId = customer.Id, Description = Guid.NewGuid().ToString(), Price = 20 }; using (3.Queries()) { makeOrder.Handle(makeOrderCommand); transaction.Commit(); } } }
static void Main() { PopulateDb(); NHibernateProfiler.Initialize(); using (var session = new SessionProvider().GetSession()) using (var transaction = session.BeginTransaction()) { Console.WriteLine("Get Belgian customers from the DB along with their total order price"); Console.WriteLine("--------------------"); var query = new GetCustomersFromBelgiumWithTotalOrderPrice(session); var customers = query.List().ToList(); foreach (var customer in customers) { Console.WriteLine("Customer: " + customer.Name + " total order price: " + customer.TotalOrderPrice); } Console.WriteLine("--------------------"); Console.WriteLine(); Console.WriteLine("Create a new order for the first Belgian customer"); Console.WriteLine("--------------------"); var makeOrder = new MakeOrder(session); var makeOrderCommand = new MakeOrderCommand { CustomerId = customers.First().Id, Description = Guid.NewGuid().ToString(), Price = 20 }; makeOrder.Handle(makeOrderCommand); Console.WriteLine("Done"); Console.WriteLine("--------------------"); Console.WriteLine(); Console.WriteLine("Change the billing address for the first Belgian customer"); Console.WriteLine("--------------------"); var changeBillingAddress = new ChangeBillingAddress(session); var changeBillingAddressCommand = new ChangeBillingAddressCommand { CustomerId = customers.First().Id, NewBillingAddress = new Address { Street = "Veldkant 33A", City = "Kontich", Country = "Belgie" } }; changeBillingAddress.Handle(changeBillingAddressCommand); Console.WriteLine("Done"); Console.WriteLine("--------------------"); Console.WriteLine(); Console.ReadLine(); transaction.Commit(); } }
public void Handle(MakeOrderCommand command) { var customer = _session.Get<Customer>(command.CustomerId); customer.MakeOrder(command.Description, command.Price); }