MVC扩展控制器工厂,通过继承DefaultControllerFactory来决定使用哪个接口实现,使用Ninject
希望实现的效果是:对购物车中所有商品的总价,实现9折或8折:
当点击"9折":
当点击"8折":
□ 思路
8折或9折是打折接口的不同实现,关键是:由什么条件决定使用哪种打折方式?
--当点击8折或9折链接的时候,把参数放在路由中,然后在自定义控制器工厂中根据参数的不同选择使用哪种打折方式。
□ model
public class CartLine
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public int Quantity { get; set; }
}
□ 接口
using MvcApplication2.Models;
namespace MvcApplication2
{
public interface IDiscount
{
decimal GetFinalPrice(List<CartLine> cartLines);
}
}
□ 接口的2种实现
using System.Collections.Generic;
namespace MvcApplication2.implementation
{
public class NineDiscount : IDiscount
{
public decimal GetFinalPrice(List<Models.CartLine> cartLines)
{
decimal result = 0.0M;
foreach (var item in cartLines)
{
result += item.Price*item.Quantity;
}
return result*(90M/100M);
}
}
}
using System.Collections.Generic;
namespace MvcApplication2.implementation
{
public class EightDiscount : IDiscount
{
public decimal GetFinalPrice(List<Models.CartLine> cartLines)
{
decimal result = 0.0M;
foreach (var item in cartLines)
{
result += item.Price * item.Quantity;
}
return result * (80M / 100M);
}
}
}
□ HomeController
using System.Collections.Generic;
using System.Web.Mvc;
using MvcApplication2.Models;
namespace MvcApplication2.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
List<CartLine> cartLines = new List<CartLine>()
{
new CartLine(){Id = 1, Name = "Product1", Price = 80M, Quantity = 2},
new CartLine(){Id = 2, Name = "Product2", Price = 100M, Quantity = 3},
};
Session["cart"] = cartLines;
return View(cartLines);
}
}
}
□ Home/Index.cshtml
把不同的打折方式放在路由中传递。
@model List<MvcApplication2.Models.CartLine>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
<table>
<tr style="background-color: #e3e3e3;">
<td>产品</td>
<td>价格</td>
<td>数量</td>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@item.Name</td>
<td>@string.Format("{0:C}", item.Price)</td>
<td>@item.Quantity</td>
</tr>
}
</table>
<p>
@Html.ActionLink("9折购买", "Index", "Shop", new {policy = "Nine"},new {})
</p>
<p>
@Html.ActionLink("8折购买", "Index", "Shop", new {policy = "Eight"},new {})
</p>
□ 自定义控制器工厂,使用Ninject,根据路由参数policy的不同,决定选择具体的打折接口实现
using System;
using System.Web.Mvc;
using System.Web.Routing;
using MvcApplication2.implementation;
using Ninject;
namespace MvcApplication2.Extension
{
public class NinjectControllerFactory : DefaultControllerFactory
{
IKernel ninjectKernel;
string policy = "";
public NinjectControllerFactory()
{
ninjectKernel = new StandardKernel();
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
if (requestContext.RouteData.Values["policy"] != null)
{
policy = requestContext.RouteData.Values["policy"].ToString();
}
AddBindings();
return controllerType == null ? null : (IController) ninjectKernel.Get(controllerType);
}
private void AddBindings()
{
switch (policy)
{
case "Eight":
ninjectKernel.Rebind<IDiscount>().To<EightDiscount>();
break;
case "Nine":
ninjectKernel.Rebind<IDiscount>().To<NineDiscount>();
break;
default:
ninjectKernel.Rebind<IDiscount>().To<NineDiscount>();
break;
}
}
}
}
□ 自定义控制器工厂全局注册
ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
□ ShopController中使用打折接口方法
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using MvcApplication2.Models;
namespace MvcApplication2.Controllers
{
public class ShopController : Controller
{
public IDiscount _Discount;
public ShopController(IDiscount discount)
{
this._Discount = discount;
}
public ActionResult Index(string policy)
{
List<CartLine> cartLines = new List<CartLine>();
if (Session["cart"] != null)
{
cartLines = (List<CartLine>)Session["cart"];
}
ViewData["total"] = String.Format("{0:C}",_Discount.GetFinalPrice(cartLines));
return View();
}
}
}
□ Shop/Index.cshtml
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
打折后的价格为: @ViewData["total"]
□ 自定义路由
为了让url更直观,符合controller/action/paramter:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{policy}",
defaults: new { controller = "Home", action = "Index", policy = UrlParameter.Optional }
);
MVC扩展控制器工厂,通过继承DefaultControllerFactory来决定使用哪个接口实现,使用Ninject的更多相关文章
- MVC扩展控制器工厂,通过实现IControllerFactory,根据action名称生成不同的Controller
关于控制器工厂的扩展,要么通过实现IControllerFactory接口,要么通过继承DefaultControllerFactory.本篇中,我想体验的是: 1.当请求经过路由,controlle ...
- MVC扩展控制器, 把部分视图转换成字符串(带验证信息), 并以json传递给前端视图
当我们使用jQuery异步提交表单数据的时候,需要把部分视图转换成字符串(带验证信息),以json的形式传递给前端视图. 使用jQuery异步加载部分视图,返回内容追加到页面某个div: jQ ...
- MVC项目实践,在三层架构下实现SportsStore-03,Ninject控制器工厂等
SportsStore是<精通ASP.NET MVC3框架(第三版)>中演示的MVC项目,在该项目中涵盖了MVC的众多方面,包括:使用DI容器.URL优化.导航.分页.购物车.订单.产品管 ...
- .NET/ASP.NET MVC Controller 控制器(IController控制器的创建过程)
阅读目录: 1.开篇介绍 2.ASP.NETMVC IControllerFactory 控制器工厂接口 3.ASP.NETMVC DefaultControllerFactory 默认控制器工厂 4 ...
- .NET/ASP.NET MVC Controller 控制器(深入解析控制器运行原理)
阅读目录: 1.开篇介绍 2.ASP.NETMVC Controller 控制器的入口(Controller的执行流程) 3.ASP.NETMVC Controller 控制器的入口(Controll ...
- 三、ASP.NET MVC Controller 控制器(二:IController控制器的创建过程)
阅读目录: 1.开篇介绍 2.ASP.NETMVC IControllerFactory 控制器工厂接口 3.ASP.NETMVC DefaultControllerFactory 默认控制器工厂 4 ...
- 二、ASP.NET MVC Controller 控制器(一:深入解析控制器运行原理)
阅读目录: 1.开篇介绍 2.ASP.NETMVC Controller 控制器的入口(Controller的执行流程) 3.ASP.NETMVC Controller 控制器的入口(Controll ...
- NET/ASP.NET MVC Controller 控制器(一:深入解析控制器运行原理)
阅读目录: 1.开篇介绍 2.ASP.NETMVC Controller 控制器的入口(Controller的执行流程) 3.ASP.NETMVC Controller 控制器的入口(Controll ...
- ASP.NET MVC 创建控制器类过程
MvcHandler.ProcessRequestInit()方法: 1.1获取控制器的名称string requiredString = this.RequestContext.RouteData. ...
随机推荐
- yum安装Mysql-5.6
MySQL yum库提供了一个简单的和方便的方法来安装和更新MySQL相关的软件包到最新版本. MySQL yum库文档说明:http://dev.mysql.com/doc/mysql-yum-re ...
- Baidu软件研发工程师笔试题整理
Hadoop Map/Reduce Hadoop Map/Reduce是一个使用简易的软件框架,基于它写出来的应用程序能够运行在由上千个商用机器组成的大型集群上,并以一种可靠容错的方式并行处理上T级别 ...
- Elasticsearch: 权威指南---基础入门
1.查看方式:GETURL:http://10.10.6.225:9200/?pretty pretty 在任意的查询字符串中增加pretty参数.会让Elasticsearch美化输出JSON结果以 ...
- Spring整合JDBC以及AOP管理事务
本节内容: Spring整合JDBC Spring中的AOP管理事务 一.Spring整合JDBC Spring框架永远是一个容器,Spring整合JDBC其实就是Spring提供了一个对象,这个对象 ...
- Spark(五)Spark任务提交方式和执行流程
一.Spark中的基本概念 (1)Application:表示你的应用程序 (2)Driver:表示main()函数,创建SparkContext.由SparkContext负责与ClusterMan ...
- linux 101 hacks 3null 改文件大小写 xargs
禁止标准输出和错误信息的输出 当我们调试 shell 脚本的时候,我们往往不希望看到标准输出和标准错误的信息.我们可以使用/dev/nulll 来禁止标准错误的信息. 将标准输出重定向到/dev/nu ...
- 基于Json.NET自己实现MVC中的JsonValueProviderFactory
写了博文ASP.NET MVC 3升级至MVC 5.1的遭遇:“已添加了具有相同键的项”之后,继续看着System.Web.Mvc.JsonValueProviderFactory的开源代码. 越看越 ...
- 关于谷歌浏览器62版本之后引用video.js不能自动播放的问题(Cross-origin plugin content from http://vjs.zencdn.net/swf/5.0.0-rc1/video-js.swf must have a visible size larger than 400 x 300 pixels, or it will be blocked.)
Cross-origin plugin content from http://vjs.zencdn.net/swf/5.0.0-rc1/video-js.swf must have a visibl ...
- 【小思考】Python的float转换精度损失所想到的
首先,为啥会要讨论这个问题. 我得为昨天拖了小组后腿深表歉意.其实程序逻辑很快就理通了的,但自己总是会因为各种各样的小问题束缚手脚,看接下来这个图片: 稍微有数据敏感性的同学就能看出,中间这么一大堆又 ...
- volatile 和锁的内存语义
一.volatile 的内存语义 1. volatile 的特性 volatile变量自身具有以下特性: 可见性 :对一个volatile变量的读,总是能看到(任意线程)对这个volatile变量最后 ...