51 lines
1.4 KiB
C#
51 lines
1.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
using Ninject;
|
|
|
|
namespace NinjectDemo
|
|
{
|
|
class Program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
IKernel ninjectKernel = new StandardKernel();
|
|
ninjectKernel.Bind<IValueCalculator>()
|
|
.To<LinqValueCalculator>();
|
|
ninjectKernel.Bind<IDiscountHelper>()
|
|
.To<DefaultDiscountHelper>()
|
|
.WithConstructorArgument("discounttParam", 50M);
|
|
|
|
// get the interface implementation
|
|
IValueCalculator calcImp1 = ninjectKernel.Get<IValueCalculator>();
|
|
// create the instance of ShoppingCart and inject the dependency
|
|
ShoppingCart cart = ninjectKernel.Get<ShoppingCart>();
|
|
// perform the calculation and write out the result
|
|
Console.WriteLine("Total: {0:c}", cart.CalculateStockValue());
|
|
}
|
|
}
|
|
|
|
public interface IDiscountHelper
|
|
{
|
|
decimal ApplyDiscount(decimal totalParam);
|
|
}
|
|
|
|
public class DefaultDiscountHelper : IDiscountHelper
|
|
{
|
|
private decimal discountRate;
|
|
|
|
public DefaultDiscountHelper(decimal discountParam)
|
|
{
|
|
discountRate = discountParam;
|
|
}
|
|
|
|
public decimal ApplyDiscount(decimal totalParam)
|
|
{
|
|
return (totalParam - (discountRate / 100m * totalParam));
|
|
}
|
|
}
|
|
}
|