6.1.3 Creating Chains of Dependency
parent
656c976807
commit
02b80da83f
|
@ -18,9 +18,16 @@ namespace NinjectDemo
|
||||||
|
|
||||||
public class LinqValueCalculator : IValueCalculator
|
public class LinqValueCalculator : IValueCalculator
|
||||||
{
|
{
|
||||||
|
private IDiscountHelper discounter;
|
||||||
|
|
||||||
|
public LinqValueCalculator(IDiscountHelper discountParam)
|
||||||
|
{
|
||||||
|
discounter = discountParam;
|
||||||
|
}
|
||||||
|
|
||||||
public decimal ValueProducts(params Product[] products)
|
public decimal ValueProducts(params Product[] products)
|
||||||
{
|
{
|
||||||
return products.Sum(p => p.Price);
|
return discounter.ApplyDiscount(products.Sum(p => p.Price));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -14,6 +14,7 @@ namespace NinjectDemo
|
||||||
{
|
{
|
||||||
IKernel ninjectKernel = new StandardKernel();
|
IKernel ninjectKernel = new StandardKernel();
|
||||||
ninjectKernel.Bind<IValueCalculator>().To<LinqValueCalculator>();
|
ninjectKernel.Bind<IValueCalculator>().To<LinqValueCalculator>();
|
||||||
|
ninjectKernel.Bind<IDiscountHelper>().To<DefaultDiscountHelper>();
|
||||||
|
|
||||||
// get the interface implementation
|
// get the interface implementation
|
||||||
IValueCalculator calcImp1 = ninjectKernel.Get<IValueCalculator>();
|
IValueCalculator calcImp1 = ninjectKernel.Get<IValueCalculator>();
|
||||||
|
@ -23,4 +24,17 @@ namespace NinjectDemo
|
||||||
Console.WriteLine("Total: {0:c}", cart.CalculateStockValue());
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue