6.1.3 Creating Chains of Dependency

chap6
Jason Zhu 2021-08-10 22:39:57 +10:00
parent 656c976807
commit 02b80da83f
2 changed files with 22 additions and 1 deletions

View File

@ -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));
}
}
}

View File

@ -14,6 +14,7 @@ namespace NinjectDemo
{
IKernel ninjectKernel = new StandardKernel();
ninjectKernel.Bind<IValueCalculator>().To<LinqValueCalculator>();
ninjectKernel.Bind<IDiscountHelper>().To<DefaultDiscountHelper>();
// get the interface implementation
IValueCalculator calcImp1 = ninjectKernel.Get<IValueCalculator>();
@ -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));
}
}
}