51 lines
1.4 KiB
C#
Raw Normal View History

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