希望实现的效果是:对购物车中所有商品的总价,实现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 }
            );

最新文章

  1. 从EF的使用中探讨业务模型能否脱离单一存储层完全抽象存在
  2. ASP------ActioinResult之多种返回值
  3. python 函数式编程工具
  4. iOS面试题及答案2015.6.7
  5. OC中字典:NSDictionary类是如何使用的
  6. 加解密算法二:非对称加解密及RSA算法的实现
  7. ubuntu卸载qq2012
  8. 仿建行JS键盘
  9. webpack学习笔记一(入门)
  10. 一个想法照进现实-《IT连》创业项目:直觉型面试招聘的漏洞
  11. mysql数据库-------基础
  12. LINUX文档管理命令
  13. 【洛谷P4144】大河的序列
  14. CMake for MFC example
  15. oracle生成AWR报告方法
  16. Oracle常用表和常见操作命令
  17. ES中文分词器之精确短语匹配(解决了match_phrase匹配不全的问题)
  18. 转 WiFi的STA和AP模式指什么?
  19. fabric 安装
  20. [py][mx]django项目-让系统用自定义的users表认证

热门文章

  1. TDictionary 与 TObjectDictionary
  2. CCF CSP 201412-3 集合竞价
  3. Kaldi 安装
  4. 【BZOJ】1336: [Balkan2002]Alien最小圆覆盖
  5. [CodeForces - 678F] Lena and Queries 线段树维护凸包
  6. 牛客练习赛3 B - 贝伦卡斯泰露
  7. 浅谈 Java 中的枚举
  8. [ 转载 ] Java中常用的设计模式
  9. springBoot application.properties 基础配置
  10. Xshell6和Xftp下载地址,rzsz的使用