コード例 #1
0
        public Order CreateOrder(Order entity)
        {
            var header = new Order();

            using (var transaction = Uow.GetTransaction())
            {
                try
                {
                    header.CustomerID     = entity.CustomerID;
                    header.EmployeeID     = entity.EmployeeID;
                    header.OrderDate      = entity.OrderDate;
                    header.RequiredDate   = entity.RequiredDate;
                    header.ShipVia        = entity.ShipVia;
                    header.Freight        = entity.Freight;
                    header.ShipName       = entity.ShipName;
                    header.ShipAddress    = entity.ShipAddress;
                    header.ShipCity       = entity.ShipCity;
                    header.ShipRegion     = entity.ShipRegion;
                    header.ShipPostalCode = entity.ShipPostalCode;
                    header.ShipCountry    = entity.ShipCountry;

                    Uow.OrderRepository.Add(header);

                    Uow.CommitChanges();

                    foreach (var summary in entity.OrderSummaries)
                    {
                        var detail = new OrderDetail();

                        var product = Uow.ProductRepository.Get(new Product(summary.ProductID));

                        if (product == null)
                        {
                            throw new NullEntityException(String.Format("The product with ID : '{0}' is not exists in database.", detail.ProductID));
                        }

                        if (product.Discontinued == true)
                        {
                            throw new DiscontinuedProductException(String.Format("The product with ID : '{0}' is discontinued.", product.ProductID));
                        }

                        detail.OrderID   = header.OrderID;
                        detail.ProductID = summary.ProductID;
                        detail.Quantity  = summary.Quantity;
                        detail.UnitPrice = product.UnitPrice;
                        detail.Discount  = (Single)summary.Discount;

                        Uow.OrderDetailRepository.Add(detail);

                        product.UnitsInStock -= detail.Quantity;
                        product.UnitsOnOrder += detail.Quantity;

                        Uow.ProductRepository.Update(product);
                    }

                    Uow.CommitChanges();

                    entity.OrderID = header.OrderID;

                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();

                    throw ex;
                }
            }

            return(header);
        }