예제 #1
0
 public void InspectTheObjectAndCalcualteAResult()
 {
     var shoppingCart = new WebShoppingCart();
     var discount     = Pattern.Match <ShoppingCart, decimal>(shoppingCart).
                        Case(cart => cart.Customer is ClubMember, cart => cart.OrderValue * 0.05m).
                        Case(cart => cart.Customer is FirstTimeCustomer, cart => cart.OrderValue * 0.04m).
                        Case(cart => cart.Customer is StandardCustomer, cart => cart.OrderValue * 0.02m).
                        Default(cart => cart.OrderValue).
                        Result;
 }
예제 #2
0
        public void ShopingCardTest()
        {
            ShoppingCart shoppingCart = new WebShoppingCart();

            //sub-type matching with conditions on the sub-type
            var specialFrontendOffers = Pattern.Match <ShoppingCart, decimal>(shoppingCart).
                                        Case <AppShoppingCart>(appShopingCart => appShopingCart.IsFirstRunExperience, 0.05m).      //5% off for the first time order with the app
                                        Case <WebShoppingCart>(webShopingCart => webShopingCart.PromoCode == "WebSpecial", 0.04m). //4% off for the newsletter promotion code (only supported by the web interface)
                                        Default(0.0m).
                                        Result;

            //matching on a condition and nested pattern matching
            var cartDiscounts = Pattern.Match <ShoppingCart, decimal>(shoppingCart).
                                Case(cart => cart.OrderValue > 100, cart => Pattern.Match <Customer, decimal>(cart.Customer).   // if the order value is bigger than 100 the discount depends on the customer status
                                     Case <ClubMember>(0.1m).                                                                   // Club member always get 10%
                                     Case <StandardCustomer>(standardCustomer => !standardCustomer.HasOutstandingDebts, 0.05m). // standardCustomers get 5% if there are no outstanding debts
                                     Default(0.0m).
                                     Result).
                                Case(cart => cart.OrderValue > 50, 0.02m). // between 50 and 100, the discount is 2% without further conditions
                                Default(0.0m).
                                Result;

            //after the first match all the other matches are ignored
            var shipping = Pattern.Match <Address, decimal>(shoppingCart.ShippingAddress).
                           Case(address => address.ShippingDistance > 1000, 7m).
                           Case(address => address.ShippingDistance > 500, 5m).
                           Case(address => address.ShippingDistance > 50, 3m).
                           Default(2m);

            var overallPrice = shoppingCart.OrderValue +
                               (shoppingCart.OrderValue * specialFrontendOffers) +
                               (shoppingCart.OrderValue * cartDiscounts) +
                               shipping;

            //Start check out process
            Pattern.Match(shoppingCart.PayMethod).
            Case <CreditCard>(creditCard => CheckoutPerCreditcard(shoppingCart, overallPrice)).
            Case <AdvancePayment>(advancePayment => CheckoutPerAdvancePayment(shoppingCart, overallPrice)).
            Default(() => { throw new NotSupportedException(); });
        }