//Func is a C# builit in delegate that takes (many - 16, arguments and returns a value - out) //in the use below, we pass in a List of ProductModel, the subTotal and we expect an out (return value). //and we name (give an alias - not too sure is saying alias is correct, but doing so for my understanding) so this Func<T> is easier to use. //I also use the type Action. Also a built in C# Delegate but an Action dosn't have a return type. //Tim Corey mentions that normally, Func and Action would not be used in this way, but this is a Delegate Demo App and so lets show C# Delegates in use. public decimal GenerateTotal(MentionDiscount mentionSubTotal, Func <List <ProductModel>, decimal, decimal> calculateDiscountedTotal, Action <string> tellUserWeAreDiscounting) { //throw new NotImplementedException(); //Use delegate. decimal subTotal = Items.Sum(x => x.Price); mentionSubTotal(subTotal); tellUserWeAreDiscounting("We are applying your discount"); ////Calculate Discount depending on amount of purchase ////These hard code values are bad practice. It leads to violating the Open Close Principal //if(subTotal > 100) //{ // return subTotal * 0.80M; //} //else if(subTotal > 50) //{ // return subTotal * 0.85M; //} //else if(subTotal > 10) //{ // return subTotal * 0.90M; //} //else //{ // return subTotal; //} decimal total = calculateDiscountedTotal(Items, subTotal); return(total); }
public decimal GenerateTotal(MentionDiscount mentionDiscount, DiscountTotal calculateDiscount, MessageShow tellUserWeAreDiscounting) { decimal subTotal = Items.Sum(x => x.Price); mentionDiscount(subTotal); tellUserWeAreDiscounting("We are applying discount"); return(calculateDiscount(Items, subTotal)); }
public decimal GenerateTotal(MentionDiscount mentionDiscount, Func <List <ProductModel>, decimal, decimal> calculateDiscount, Action <string> tellUserWeAreDiscounting) { decimal subTotal = Items.Sum(x => x.Price); mentionDiscount(subTotal); tellUserWeAreDiscounting("We are applying your discount"); return(calculateDiscount(Items, subTotal)); }
public decimal GenerateTotal(MentionDiscount mentionSubtotal, Func <List <ProductModel>, decimal, decimal> calculateDiscountedTotal, Action <string> tellUserWeAreDiscounting) { decimal subTotal = Items.Sum(item => item.Price); mentionSubtotal(subTotal); tellUserWeAreDiscounting("We are applying discount."); return(calculateDiscountedTotal(Items, subTotal)); }
public decimal GenerateTotal(MentionDiscount mentionSubtotal, Func <List <ProductModel>, decimal, decimal> calculateDiscountedTotal, Action <string> mentionToUserDiscount) { decimal subTotal = Items.Sum(x => x.Price); mentionSubtotal(subTotal); mentionToUserDiscount("Discounting..."); return(calculateDiscountedTotal(Items, subTotal)); }
public decimal TotalItems(MentionDiscount mentionSubtotal, Func <List <ProductModel>, decimal, decimal> calculateDiscountedTotal, Action <string> mentionDiscounting) { decimal subTotal = Items.Sum(x => x.Price); mentionSubtotal(subTotal); mentionDiscounting("We are applying your discount"); return(calculateDiscountedTotal(Items, subTotal)); }
public decimal GenerateTotal(MentionDiscount mentionDiscount, Func <List <ProductModel>, decimal, decimal> calculateDiscountTotal, Action <string> showDiscounting) { decimal subtotal = Items.Sum(x => x.Price); mentionDiscount(subtotal); showDiscounting("We are applying discounts"); return(calculateDiscountTotal(Items, subtotal)); }
//passing in the arbitrary delegate method public decimal GenerateTotal(MentionDiscount mentionSubtotal, Func <List <ProductModel>, decimal, decimal> calculateDiscountetTotal, //Func returns a value Action <string> tellUserWeAreDiscounting) //Action returns a void { decimal subTotal = Items.Sum(x => x.Price); mentionSubtotal(subTotal); // calling the method mentionDiscount and passing in the subTotal (loosly coupled, because genrateTotal doesnt know anything about mentionDiscount). tellUserWeAreDiscounting("We are applying your discount."); return(calculateDiscountetTotal(Items, subTotal)); }
public decimal GenerateTotal(MentionDiscount mentionSubtotal, Func <List <ProductModel>, decimal, decimal> calculateDiscountedTotal, Action <string> tellUserWeAreDicounting) { var subtotal = Items.Sum(i => i.Price); mentionSubtotal(subtotal); tellUserWeAreDicounting("Aplicando desconto"); return(calculateDiscountedTotal(Items, subtotal)); }
public decimal GenerateTotal(MentionDiscount mentionSubtotal, /* * Func has 16 overloads. In Func we can pass 16 different variables, if that's the case you are back to creating a delegate. It is one of the limitations * for this simplicity model. We cannot name these things we just have to pass in the types. So, Func takes in 16 parameters and the last parameter is * always the output. Func is a method that has a return value other than void. Func does not work with out parameters. If you have to do that you have to * use a delegate. */ Func <List <ProductModel>, decimal, decimal> calculateDiscountedTotal, /* * Action does not returns a value, it returns a void. */ Action <string> tellUserWeAreDiscounting) { /* * The Sum method below is actually taking a Func of ProductModel and decimal. So, it is using a delegate called Func. And, it is one delegate where * x is our ProductModel and we are passing in and saying take the price for each of them and add them together. */ decimal subTotal = Items.Sum(x => x.Price); /* * Something is not good with this method because we have a whole bunch of hard-coded items and that's not really wise. Because this library may be used * by different applications in your company. If you have to recompile this library with new code that means you have to change all your applications by * recompiling them and redeploying them and that's really not efficient. * * if (subTotal > 100) * { * return subTotal * 0.80M; * } * else if (subTotal > 50) * { * return subTotal * 0.85M; * } * else if (subTotal > 10) * { * return subTotal * 0.95M; * } * else * { * return subTotal; * } */ mentionSubtotal(subTotal); tellUserWeAreDiscounting("We are applying your discount."); return(calculateDiscountedTotal(Items, subTotal)); }
/// <summary> /// Generate total sum /// * Creating anonymous methods: anonymous Delegate /// * Creating anonymous methods: anonymous Func /// * Creating anonymous methods: anonymous Action /// </summary> /// <param name="mentionSubtotal"></param> /// <param name="calculateDiscountedTotal"></param> /// <param name="tellUserWeAreDiscounting"></param> /// <returns></returns> public decimal GenerateTotal(MentionDiscount mentionSubtotal, Func <List <ProductModel>, decimal, decimal> calculateDiscountedTotal, Action <string> tellUserWeAreDiscounting) { decimal subTotal = Products.Sum(product => product.Price); // Use delegate method mentionSubtotal?.Invoke(subTotal); // Use Action method tellUserWeAreDiscounting?.Invoke("We are applying your discount."); // Use Func method return(calculateDiscountedTotal?.Invoke(Products, subTotal) ?? 0); }
public decimal GenerateTotal(MentionDiscount mentionDiscount, Func <List <ProductModel>, decimal, decimal> calculateDiscountTotal, Action <string> informUserApplyDiscount) { decimal subtotal = Items.Sum(x => x.Price); decimal returnVal; mentionDiscount(subtotal); informUserApplyDiscount("Applying discount..."); returnVal = calculateDiscountTotal(Items, subtotal); mentionDiscount(subtotal - returnVal); return(returnVal); }
// ""Func<> method"" takes "List<ProductModel>" and one "decimal" value as input, // last one "decimal" is output. // With using Func<> we dont need to use signature like we did for MentionDiscount public decimal GenerateTotal(MentionDiscount mentionSubtotal, Func <List <ProductModel>, decimal, decimal> calculateDiscountedTotal, Action <string> tellUserWeAreDiscounting) { decimal subTotal = Items.Sum(x => x.Price); mentionSubtotal(subTotal); tellUserWeAreDiscounting("applying discount..."); // Only send two paramater. last paramater of calculateDiscountedTotal // is output(decimal) decimal total = calculateDiscountedTotal(Items, subTotal); return(total); // Func is a delegate that points to a method that accepts one or more // arguments and returns a value. Action is a delegate that points to a method // which in turn accepts one or more arguments but returns no value. In other words, // you should use Action when your delegate points to a method that returns void. }
public decimal GenerateTotal(MentionDiscount mentionDiscount) { decimal subTotal = items.Sum(x => x.Price); mentionDiscount(subTotal); if (subTotal > 100) { return(subTotal * 0.80M); } else if (subTotal > 50) { return(subTotal * 0.85M); } else if (subTotal > 10) { return(subTotal * 0.90M); } else { return(subTotal); } }