public class HomeController : Controller { public ActionResult Index() { //核心对象 IKernel ninjectKernel = new StandardKernel(); ninjectKernel.Bind().To (); //方案1:获取接口实例 IValueCaculator calImpl = ninjectKernel.Get (); //方案2:自身绑定 //ShoppingCart cart = ninjectKernel.Get (); //cart1.Products = new List () { new Product() { Category = "food", Price = 12 }, new Product() { Category = "books", Price = 30 }, new Product() { Category = "wine", Price = 40 } }; ShoppingCart cart = new ShoppingCart(calImpl) { Products = new List { new Product() { Category = "food", Price = 12 }, new Product() { Category = "books", Price = 30 }, new Product() { Category = "wine", Price = 40 } } }; var sum=cart.Cal(); return View(); } public ActionResult About() { return View(); } } public class Product { public int ProductId { get; set; } public string Name { get; set; } public string Description { get; set; } public decimal Price { get; set; } public string Category { get; set; } } public interface IValueCaculator { decimal ValueProduct(IEnumerable products); } public class LinqValueCalcalator : IValueCaculator { public decimal ValueProduct(IEnumerable products) { return products.Sum(a => a.Price); } } public class ShoppingCart { protected IValueCaculator valueCaculator; public ShoppingCart(IValueCaculator caculator) { valueCaculator = caculator; } public IEnumerable Products { get; set; } public decimal Cal() { return valueCaculator.ValueProduct(Products); } }