日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > c/c++ >内容正文

c/c++

在MVC项目中使用Ninject

發(fā)布時(shí)間:2023/12/6 c/c++ 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在MVC项目中使用Ninject 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

項(xiàng)目結(jié)構(gòu)圖:

      

App_start文件夾中的文件是VS自己創(chuàng)建的,其中NinjectWebCommon類在創(chuàng)建之初并不存在。后面會(huì)再次提到!

添加一個(gè)Home控制器。代碼如下:

using EssentialTools.Models; using Ninject; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc;namespace EssentialTools.Controllers {public class HomeController : Controller{private IValueCalculator calc;Product[] products ={new Product{Name="Kayak",Category="Watersports",Price=275M},new Product{Name="LifeJacket",Category="Watersports",Price=48.95M},new Product{Name="Soccer Ball",Category="Soccer",Price=19.50M},new Product{Name="Corner Flag",Category="Soccer",Price=34.95M}};public HomeController(IValueCalculator calcParam){calc = calcParam;}public ActionResult Index(){ //IKernel ninjectKernel = new StandardKernel();//ninjectKernel.Bind<IValueCalculator>().To<LinqValueCalculator>();//LinqValueCalculator calc = new LinqValueCalculator();//return View(calc.ValueProducts(products));ShoppingCart cart = new ShoppingCart(calc) { Products = products };decimal totalValue = cart.CalculateProductTotal();return View(totalValue);}} } HomeController.cs

為控制器中的Index方法添加視圖。代碼如下:

@model decimal @{ViewBag.Title = "Index";Layout = null; }<div> Total value is $@Model</div> Index.cshtml

創(chuàng)建Infrastructure文件夾,在該文件夾下創(chuàng)建Ninject的依賴解析器。代碼如下:

using EssentialTools.Models; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Ninject;namespace EssentialTools.Infrastructure {public class NinjectDependencyResolver : IDependencyResolver{private IKernel kernel;public NinjectDependencyResolver(IKernel knernelParam){kernel = knernelParam;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>();}} } NinjectDependencyResolver.cs

在Models文件夾中攢關(guān)鍵1個(gè)接口,3個(gè)類。代碼如下:

using System; using System.Collections.Generic; using System.Linq; using System.Web;namespace EssentialTools.Models {public interface IValueCalculator{decimal ValueProducts(IEnumerable<Product> products);} } IValueCalculator.cs using System; using System.Collections.Generic; using System.Linq; using System.Web;namespace EssentialTools.Models {public class LinqValueCalculator : IValueCalculator{public decimal ValueProducts(IEnumerable<Product> products){return products.Sum(p => p.Price);}} } LinqValueCalculator.cs using System; using System.Collections.Generic; using System.Linq; using System.Web;namespace EssentialTools.Models {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; }} } Product.cs using System; using System.Collections.Generic; using System.Linq; using System.Web;namespace EssentialTools.Models {public class ShoppingCart{IValueCalculator calc;public ShoppingCart(IValueCalculator calcParam){calc = calcParam;}public IEnumerable<Product> Products { get; set; }public decimal CalculateProductTotal(){return calc.ValueProducts(Products);}} } ShoppingCart.cs

使用nuget安裝Ninject

工具→庫(kù)程序包管理器→程序包管理器控制臺(tái)

安裝ninject內(nèi)核包:
install-package Ninject -version 3.0.1.10
安裝ninject內(nèi)核包的拓展包:
install-package Ninject.Web.Common -version 3.0.0.7
對(duì)MVC3的引用(在mvc5中仍能用到)
install-package ninject.mvc3 -version 3.0.0.6

版本號(hào)最好帶上,不帶版本號(hào),可能會(huì)出錯(cuò)!

安裝好了之后NinjectWebCommon.cs文件就會(huì)出現(xiàn)。這時(shí)候需要為該類中的RegisterServices方法添加代碼(注冊(cè)依賴解析器)

RegisterServices方法代碼如下:

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

?

對(duì)瀏覽器發(fā)出請(qǐng)求到控制器處理請(qǐng)求這段時(shí)間發(fā)生的事!

1、瀏覽器向MVC框架發(fā)送一個(gè)請(qǐng)求Home的URL,MVC框架推測(cè)出該請(qǐng)求意指Home控制器,于是會(huì)創(chuàng)建HomeController類實(shí)例。

2、MVC框架在創(chuàng)建HomeController類實(shí)例過(guò)程中會(huì)發(fā)現(xiàn)其構(gòu)造器有一個(gè)對(duì)IValueCalculator接口的依賴項(xiàng),于是會(huì)要求依賴項(xiàng)解析器對(duì)此依賴項(xiàng)進(jìn)行解析,?將該接口指定為依賴項(xiàng)解析器中的GetService方法所使用的類型參數(shù)。

3、依賴項(xiàng)解析器會(huì)將傳遞過(guò)來(lái)的類型參數(shù)交給TryGet方法,要求Ninject創(chuàng)建一個(gè)新的HomeController接口實(shí)例。

4、Ninect會(huì)檢測(cè)到HomeController構(gòu)造器與其實(shí)現(xiàn)類LilnqValueCalculator具有綁定關(guān)系,于是為該接口創(chuàng)建一個(gè)LinqValueCalculator類實(shí)例,并將其回遞給依賴項(xiàng)解析器。

5、依賴項(xiàng)解析器將Ninject所返回的LilnqValueCalculator類作為IValueCalculator接口實(shí)現(xiàn)類實(shí)例回遞給MVC框架

6、MVC框架利用依賴項(xiàng)解析器返回的接口類實(shí)例創(chuàng)建HomeController控制器實(shí)例,并使用該控制器實(shí)例對(duì)請(qǐng)求進(jìn)行服務(wù)。

?

為已經(jīng)能夠正常運(yùn)行的程序添加功能:為購(gòu)物車內(nèi)的東西打折。

在Models文件夾內(nèi)添加一個(gè)叫做Discount的類

?

using System; using System.Collections.Generic; using System.Linq; using System.Web;namespace EssentialTools.Models {public interface IDiscountHelper{decimal ApplyDiscount(decimal totalParam);}public class DefaultDiscounter : IDiscountHelper{public decimal DiscountSize { get; set; }public decimal ApplyDiscount(decimal totalParam){return (totalParam - (DiscountSize / 100m * totalParam));}} } Discount.cs

?

這個(gè)類里面包涵了一個(gè)接口,沒有讓接口和類進(jìn)行分離(當(dāng)然,這不是重點(diǎn))。

?

更改后的計(jì)算價(jià)格的類LinqValueCalculator

using System; using System.Collections.Generic; using System.Linq; using System.Web;namespace EssentialTools.Models {public class LinqValueCalculator : IValueCalculator{private IDiscountHelper discounter;public LinqValueCalculator(IDiscountHelper discountParam){discounter = discountParam;}public decimal ValueProducts(IEnumerable<Product> products){return discounter.ApplyDiscount(products.Sum(p => p.Price));}} } LinqValueCalculator.cs

最后更改依賴項(xiàng)解析器類中的AddBindings方法

private void AddBindings(){kernel.Bind<IValueCalculator>().To<LinqValueCalculator>();kernel.Bind<IDiscountHelper>().To<DefaultDiscounter>().WithPropertyValue("DiscountSize", 50M);}

在Discount類中,有一個(gè)DiscountSize的屬性,上面方法中使用了WithPropertyValue方法為這個(gè)屬性賦了初始值。

?

轉(zhuǎn)載于:https://www.cnblogs.com/vichin/p/8595371.html

總結(jié)

以上是生活随笔為你收集整理的在MVC项目中使用Ninject的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。