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.