Beispiel #1
0
        public static Drink OrderDrink(string type, bool hasMilk, bool hasSugar)
        {
            
            // Logger to help during development and when deployed to production
            ILog logger = LogManager.GetLogger(typeof(SmallCafe));

           

            // Moved the creation of drinks to a central location
            var drinkRepository = new DrinkRepository();
            Drink drink = drinkRepository.GetDrink(type);

            try
            
            {

                if (drink == null)
                    throw new System.InvalidOperationException("Drink type not yet available.");
               
                // Ingredients and Toppings properties
                if (hasMilk)
                    drinkRepository.AddIngredient(ref drink, "Milk");

                if (hasSugar)
                    drinkRepository.AddIngredient(ref drink, "Sugar");

                // Validators are good for keeping things tidy
                // Ideally placed in an IOC Container
                // and resolved where needed.
                DrinkValidator validator = new DrinkValidator(); 
                ValidationResult results = validator.Validate(drink);

                if (!results.IsValid)
                {
                    foreach (var failure in results.Errors)
                    {
                        // Let the barista know of any drinks issues
                        IConsoleLogger consoleLogger = new ConsoleLogger();
                        consoleLogger.WriteLine("Property " + failure.PropertyName + " failed validation. Error was: " + failure.ErrorMessage);
                    
                    }

                    drink = null;
                }
                else
                {

                    drink.Prepare();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("We are unable to prepare your drink.");
                // Log Exception
                logger.FatalFormat(ex.Message, ex);
            }

            return drink;
        }
Beispiel #2
0
 public DrinkTest()
 {
     _drink = new Drink()
     {
         BeerId   = "1",
         UserName = "******"
     };
     _validator = new DrinkValidator();
 }
Beispiel #3
0
        public DrinkValidatorTest()
        {
            IDrinkInfoQuery drinkInfoQuery = new DrinkInfoQuery(new DummyDrinkDataSource());

            _drinkValidator = new DrinkValidator(drinkInfoQuery);
        }