From 02b80da83f9b5adac379415501e05d2fe6b77b61 Mon Sep 17 00:00:00 2001 From: Jason Zhu Date: Tue, 10 Aug 2021 22:39:57 +1000 Subject: [PATCH] 6.1.3 Creating Chains of Dependency --- .../NinjectDemo/Product.cs | 9 ++++++++- .../NinjectDemo/Program.cs | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/chap6_essential_tools_for_mvc/NinjectDemo/Product.cs b/chap6_essential_tools_for_mvc/NinjectDemo/Product.cs index 8145d39..f642c71 100644 --- a/chap6_essential_tools_for_mvc/NinjectDemo/Product.cs +++ b/chap6_essential_tools_for_mvc/NinjectDemo/Product.cs @@ -18,9 +18,16 @@ namespace NinjectDemo public class LinqValueCalculator : IValueCalculator { + private IDiscountHelper discounter; + + public LinqValueCalculator(IDiscountHelper discountParam) + { + discounter = discountParam; + } + public decimal ValueProducts(params Product[] products) { - return products.Sum(p => p.Price); + return discounter.ApplyDiscount(products.Sum(p => p.Price)); } } } \ No newline at end of file diff --git a/chap6_essential_tools_for_mvc/NinjectDemo/Program.cs b/chap6_essential_tools_for_mvc/NinjectDemo/Program.cs index 8668f4e..83f5923 100644 --- a/chap6_essential_tools_for_mvc/NinjectDemo/Program.cs +++ b/chap6_essential_tools_for_mvc/NinjectDemo/Program.cs @@ -14,6 +14,7 @@ namespace NinjectDemo { IKernel ninjectKernel = new StandardKernel(); ninjectKernel.Bind().To(); + ninjectKernel.Bind().To(); // get the interface implementation IValueCalculator calcImp1 = ninjectKernel.Get(); @@ -23,4 +24,17 @@ namespace NinjectDemo Console.WriteLine("Total: {0:c}", cart.CalculateStockValue()); } } + + public interface IDiscountHelper + { + decimal ApplyDiscount(decimal totalParam); + } + + public class DefaultDiscountHelper : IDiscountHelper + { + public decimal ApplyDiscount(decimal totalParam) + { + return (totalParam - (10m / 100m * totalParam)); + } + } }