Beispiel #1
0
        /// <summary>
        ///     Add any new product. Automatically determines the type of item and insert accordingly.
        /// </summary>
        /// <param name="product"></param>
        public void AddProduct(Product product)
        {
            using (var scope = new TransactionScope())
            {
                using (var connection = Connector.GetConnection())
                {
                    var productDal = new ProductDal(connection);
                    productDal.Insert(product is Item ? ((Item)product).Model : product.Name, product.ProductType);
                    var id = productDal.GetLastInsertId();
                    product.Id = id;
                    if (product.Id == null)
                    {
                        throw new ArgumentNullException(nameof(product.Id), "Product Id is null");
                    }

                    if (product is Item)
                    {
                        var item    = (Item)product;
                        var itemDal = new ItemDal(connection);
                        itemDal.Insert(id, item.StockQty, item.UnitPrice);

                        if (item is Alloywheel)
                        {
                            var alloywheel    = (Alloywheel)item;
                            var alloywheelDal = new AlloywheelDal(connection);
                            alloywheelDal.Insert(id, alloywheel.Brand, alloywheel.Dimension);
                        }

                        if (item is Tyre)
                        {
                            var tyre    = (Tyre)item;
                            var tyreDal = new TyreDal(connection);
                            tyreDal.Insert(id, tyre.Brand, tyre.Dimension, tyre.Country);
                        }

                        if (item is Battery)
                        {
                            var battery    = (Battery)item;
                            var batteryDal = new BatteryDal(connection);
                            batteryDal.Insert(id, battery.Brand, battery.Capacity, battery.Voltage);
                        }
                    }
                }
                scope.Complete();
            }
        }