当前位置: 首页 > 工具软件 > Ninject > 使用案例 >

Ninject使用例子

袁俊弼
2023-12-01

利用ninject 统计商品价格

1、创建一个MVC空白项目

2、程序包管理控件台引用

Install-Package Ninject -version 3.0.1.10

Install-Package Ninject.Web.Common -version 3.0.0.7

Install-Package Ninject.MVC3 -Version 3.0.0.6
3、创建商品类

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

    }

4、创建一个商品统计价格接口

 public interface IValueCalculator
    {
       decimal ValueProducts(IEnumerable<Product> products);
    }
5、创建接口实现方法

 public class LinqValueCalculator:IValueCalculator
    {
        public decimal ValueProducts(IEnumerable<Product> products)
        {
            return products.Sum(p => p.Price);
        }
    }

6、创建购物车统计

 public class ShoppingCart
    {
        private IValueCalculator calc;
        public ShoppingCart(IValueCalculator calcParam)
        {
            calc = calcParam;
        }
        public IEnumerable<Product> Products { get; set; }
        public decimal CalculateProductTotal()
        {
            return calc.ValueProducts(Products);
        }
    }

7、创建依靠容器NinjectDependencyResolver,

namespace NinjectDemo.Infrastructure
{
    public class NinjectDependencyResolver:IDependencyResolver
    {
        IKernel kernel;
        public NinjectDependencyResolver(IKernel kernelParam)
        {
            kernel = kernelParam;
            AddBindings();
        }
        public object GetService(Type serviceType)
        {
            return kernel.TryGet(serviceType);
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            return kernel.GetAll(serviceType);
        }
        private void AddBindings()
        {
            kernel.Bind<IValueCalculator>().To<LinqValueCalculator>();
        }
    }
}


8、在NinjectWebCommon文件中注册(文件位于app_start文件夹中)

 private static void RegisterServices(IKernel kernel)
        {
            System.Web.Mvc.DependencyResolver.SetResolver(new Infrastructure.NinjectDependencyResolver(kernel));
        }

9、在home控制器中实现

  public class HomeController : Controller
    {
        private Product[] products ={
                                       new Product{ Name="n1",Category="W1",Price=10M},
                                       new Product{Name="n2",Category="W1",Price=15.5M},
                                       new Product{Name="n3",Category="W2",Price=25.5M},
                                       new Product{Name="n4",Category="W3",Price=35.5M},
                                   };
                                 
        //
        // GET: /Home/
        private IValueCalculator calc;
        public HomeController(IValueCalculator calcParam)
        {
            calc = calcParam;
        }
        public ActionResult Index()
        {
            //IKernel ninjectKernel = new StandardKernel();
           // ninjectKernel.Bind<IValueCalculator>().To<LinqValueCalculator>();
           // IValueCalculator calc = ninjectKernel.Get<IValueCalculator>();
            ShoppingCart cart = new ShoppingCart(calc)
            {
                Products = products
            };
            decimal totalValue = cart.CalculateProductTotal();
            return View(totalValue);
        }



 类似资料: