Using NInject Do DI(3) In Asp.net MVC4
1.Install NInject , open Nuget
2.Add CheckoutInterface & Implementation:
ICheckout.cs
CustomerCheckout.cs
EmployeeCheckout.cs
Codes :
public interface ICheckout
{
dynamic GetTotal(dynamic prices);
string DiscountName { get; }
}
public classCustomerCheckout:ICheckout
{
private IDiscount _discount;
public CustomerCheckout(IDiscountdiscount)
{
_discount = discount;
}
public dynamic GetTotal(dynamic prices)
{
dynamic total=0;
for (int i = 0; i <prices.Length; i++)
{
total += prices[i];
}
return_discount.WithDiscount(total);
}
public string DiscountName
{
get { return_discount.GetType().FullName; }
}
}
public class EmployeeCheckout : ICheckout
{
private IDiscount _discount;
public EmployeeCheckout(IDiscountdiscount)
{
_discount = discount;
}
/// <summary>
/// Employee enjoy half Price
/// </summary>
/// <paramname="prices"></param>
/// <returns></returns>
public dynamic GetTotal(dynamic prices)
{
dynamic total = 0;
for (int i = 0; i <prices.Length; i++)
{
total += prices[i];
}
return _discount.WithDiscount(total* 0.5);
}
public string DiscountName { get {return _discount.GetType().FullName; } }
}
3.Add Discount Interface & Implementations :
IDiscount.cs
DiscountNormalPrice.cs
DiscountHalf.cs
Codes :
public interface IDiscount
{
dynamic WithDiscount(dynamic price);
}
public classDiscountNormalPrice:IDiscount
{
public dynamic WithDiscount(dynamicprice)
{
return price;
}
}
public classDiscountHalf : IDiscount
{
public dynamic WithDiscount(dynamicprice)
{
return price * 0.5;
}
}
4.Add DI Resolver
Implementation :
public class DIResolver :IDependencyResolver
{
private IKernel kernel;
public DIResolver()
{
kernel = new StandardKernel();
ServeCustomerDiscountSeason();
//ServeCustomerNoDiscount();
//ServeEmployeeDiscountSeason();
// ServeEmployeeNoDiscount();
}
public object GetService(Type serviceType)
{
return kernel.TryGet(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
return kernel.GetAll(serviceType);
}
private void ServeCustomerDiscountSeason()
{
kernel.Bind<ICheckout>().To<CustomerCheckout>();
kernel.Bind<IDiscount>().To<DiscountHalf>();
}
private void ServeCustomerNoDiscount()
{
kernel.Bind<ICheckout>().To<CustomerCheckout>();
kernel.Bind<IDiscount>().To<DiscountNormalPrice>();
}
private void ServeEmployeeDiscountSeason()
{
kernel.Bind<ICheckout>().To<EmployeeCheckout>();
kernel.Bind<IDiscount>().To<DiscountHalf>();
}
private void ServeEmployeeNoDiscount()
{
kernel.Bind<ICheckout>().To<EmployeeCheckout>();
kernel.Bind<IDiscount>().To<DiscountNormalPrice>();
}
}
For Demonstration , will apply these bindings one by one later .
5.In HomeController.cs :
public string Index()
{
var oriPrice = new []{15,16,14,20};
var total =_checkout.GetTotal(oriPrice);
return string.Format(
"Checkout Strategy : {0} , DiscountStrategy : {1} , Total : {2}",
_checkout.GetType().FullName,_checkout.DiscountName,total);
}
6.Open Global.asax
In Application_Start , Apply Dependency Resolver :
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
DependencyResolver.SetResolver(new DIResolver());
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
}
7.Apply bindings in DIResolver.cs one by one willget the follow results :
Summary :
1. The MVC Framework received the request and figured out that the request is
intended for the Home controller .
2. The MVC Framework asked our custom dependency resolver class to create a
new instance of the Home Controller class, specifying the class using the Type
parameter of the GetService method.
3. Our dependency resolver asked Ninject to create a new Home Controller class by
passing on the Type object to the TryGet method.
4. Ninject inspected the Home Controller constructor and found that it requires an
IValueCalculator implementation, which it knows that it has a binding for.
5. Ninject creates an instance of the LinqValueCalculatorclass and uses it to
create a new instance of the HomeController class.
6. Ninject passes the newly-created HomeController instance to the custom
dependency resolver, which returns it to the MVC Framework.The MVC
Framework uses the controller instance to service the request.
相關文章
- [ASP.NET MVC 小牛之路]04 - 依賴注入(DI)和NinjectASP.NETMVC依賴注入
- HMI 10.1 DO DI 的測試。
- [ASP.NET MVC 小牛之路]05 - 使用 NinjectASP.NETMVC
- to debug asp.net mvc4ASP.NETMVC
- 使用 Ninject and ASP.Net Web API 實現Domain EventsASP.NETWebAPIAI
- ASP.NET MVC4 入門簡介ASP.NETMVC
- ASP.NET Core依賴注入(DI)ASP.NET依賴注入
- ASP.NET Core 依賴注入(DI)ASP.NET依賴注入
- 《Asp.Net Core3 + Vue3入坑教程》 - 3.AutoMapper & Restful API & DIASP.NETVueAPPRESTAPI
- ASP.NET MVC4 亂七八糟羅列ASP.NETMVC
- Asp.Net MVC4 系列--基礎篇(1)ASP.NETMVC
- Asp.Net MVC4系列---基礎篇(5)ASP.NETMVC
- Asp.Net MVC4系列---基礎篇(4)ASP.NETMVC
- Asp.Net MVC4 + Oracle + EasyUI 學習 序章ASP.NETMVCOracleUI
- Asp.Net MVC4系列--進階篇之AJAXASP.NETMVC
- Asp.Net MVC4 系列--進階篇之ViewASP.NETMVCView
- using update panel do sync job , update UI control asyncUI
- Asp.Net MVC4 系列--進階篇之Model(1)ASP.NETMVC
- Asp.Net MVC4 系列--進階篇之Model(2)ASP.NETMVC
- Asp.Net MVC4 系列-- 進階篇之路由(1)ASP.NETMVC路由
- Asp.Net MVC4 系列--進階篇之路由 (2)ASP.NETMVC路由
- Asp.Net MVC4系列--進階篇之Helper(1)ASP.NETMVC
- Asp.Net MVC4 系列--進階篇之Helper(2)ASP.NETMVC
- ASP.NET MVC4 捆綁(Bundle)技術下的 JavaScriptASP.NETMVCJavaScript
- ASP.NET MVC4中呼叫WEB API的四個方法ASP.NETMVCWebAPI
- Asp.Net MVC4 + Oracle + EasyUI 學習 第二章ASP.NETMVCOracleUI
- Asp.Net MVC4 系列--進階篇之Controller(2)ASP.NETMVCController
- Asp.Net MVC4 + Oracle + EasyUI 學習 第一章ASP.NETMVCOracleUI
- 輕量級IOC框架:Ninject框架
- ASP.NET MVC4中用 BundleCollection使用問題手記ASP.NETMVC
- Core官方DI解析(3)-ServiceCallSite.md
- 淺談ASP.NET Core中IOC與DI的理解和使用ASP.NET
- ASP.NET Core中的依賴注入(2):依賴注入(DI)ASP.NET依賴注入
- Ninject(一)——手動依賴注入依賴注入
- Assign Shortcut key for ASP.Net Page Using JavascriptASP.NETJavaScript
- .net 溫故知新【13】:Asp.Net Core WebAPI 使用依賴注入DIASP.NETWebAPI依賴注入
- ASP.NET MVC不可或缺的部分——DI及其本質工作分析ASP.NETMVC
- JSON Web Token in ASP.NET Web API 2 using OwinJSONWebASP.NETAPI