} // end ListProducts

        public ProductData GetProduct(string productNumber)
        {
            // Create a reference to a ProductData object
            ProductData productData = null;

            try
            {
                // Connect to AdventureWorks database by using the Entity Framework
                using (AdventureWorksEntities database = new AdventureWorksEntities())
                {
                    // Find the first  product that matches the specified product number
                    Product matchingProduct = database.Products.First(
                        p => String.Compare(p.ProductNumber, productNumber) == 0);

                    productData = new ProductData()
                    {
                        Name = matchingProduct.Name,
                        ProductNumber = matchingProduct.ProductNumber,
                        Color = matchingProduct.Color,
                        ListPrice = matchingProduct.ListPrice
                    };  // end productData
                   
                }
            }
            catch
            {
                // Ignore exceptions in this implementation
            }

            // Return the product
            return productData;
        } // end GetProduct
Example #2
0
        public void ChangePrice(string productNumber, decimal price)
        {
            // Modify the price of the selected product
            // If the update is successful then return true, otherwise return false.
            Product product = null;

            try
            {
                // Connect to the AdventureWorks database by using the Entity Framework
                using (AdventureWorksEntities database = new AdventureWorksEntities())
                {
                    if (!ProductExists(productNumber, database))
                        return;
                    else
                    {
                        // Find the specified product
                        product = (from p in database.Products
                                   where String.Compare(p.ProductNumber, productNumber) == 0
                                   select p).First();

                        // Change the price for the product
                        product.ListPrice = price;

                        // Save the change back to the database
                        database.SaveChanges();
                    }
                }
            }
            catch
            {
                // If an exception occurs, return false to indicate failure
                return;
            }

            // Notify the client that the price has been changed successfully
            ProductData productData = new ProductData()
                {
                    ProductNumber = product.ProductNumber,
                    Name = product.Name,
                    ListPrice = product.ListPrice,
                    Color = product.Color
                };

            raisePriceChangedEvent(productData);
        }
Example #3
0
 private void raisePriceChangedEvent(ProductData product)
 {
     subscribers.AsParallel().ForAll(callback =>
         {
             if (((ICommunicationObject)callback).State == CommunicationState.Opened)
             {
                 callback.OnPriceChanged(product);
             }
             else
             {
                 subscribers.Remove(callback);
             }
         }
     );
 }
Example #4
0
        public ProductData GetProduct(string productNumber)
        {
            // Create a reference to a ProductData object
            // This object will be instantiated if a matching product is found
            ProductData productData = null;

            try
            {
                // Connect to the AdventureWorks database by using the Entity Framework
                using (AdventureWorksEntities database = new AdventureWorksEntities())
                {
                    // Check that the specified product exists
                    if (ProductExists(productNumber, database))
                    {
                        // Find the first product that matches the specified product number
                        Product matchingProduct = database.Products.First(
                            p => String.Compare(p.ProductNumber, productNumber) == 0);

                        productData = new ProductData()
                        {
                            Name = matchingProduct.Name,
                            ProductNumber = matchingProduct.ProductNumber,
                            Color = matchingProduct.Color,
                            ListPrice = matchingProduct.ListPrice
                        };
                    }
                }
            }
            catch
            {
                // Ignore exceptions in this implementation
            }

            // Return the product
            return productData;
        }