asp.net mvc cooike 购物车 如何实现
先上代码:
1. ShoppingCartService 类
using System;
using System.Collections.Generic;
using System.Linq;
using LinFx;
using LinFx.Data;
using LinFx.Security;
using LinFx.Web;
using YLSPay.Data.Entity; namespace YLSPay.Data.Service
{
public class ShoppingCartService : IShoppingCartService
{
private readonly IWorkContext _context;
private readonly IRepository<ShoppingCart> _repository; public ShoppingCartService(
IWorkContext context,
IRepository<ShoppingCart> repository)
{
_context = context;
_repository = repository;
} /// <summary>
/// 加入购物车
/// </summary>
/// <param name="user">用户</param>
/// <param name="productVariant">商品</param>
/// <param name="qty">数量</param>
/// <param name="attributes">属性</param>
public void AddToShoppingCart(IUser user, ProductVariant productVariant, int qty, string attributes)
{
if (productVariant == null)
throw new ArgumentNullException("productVariant"); //购物车保存至数据库
ShoppingCart cartItem; var query = _repository.Table.Where(p => p.ProductVariantId == productVariant.Id && p.Attributes == attributes);
if (user == null)
{
var recordId = GetRecordId(null);
cartItem = query.SingleOrDefault(p => p.RecordId == recordId) ?? CreateShoppingCart(recordId);
}
else
{
cartItem = query.SingleOrDefault(p => p.UserId == user.Id) ?? CreateShoppingCart(null, user);
} cartItem.Attributes = attributes;
cartItem.ProductVariantId = productVariant.Id;
cartItem.Quantity += qty;
cartItem.UpdateTime = DateTime.Now; _repository.Save();
} public string GetRecordId(string username)
{
const string name = "recordId"; //if (_context.HttpContext.Response.Cookies[name] == null)
//{
// var cookie = new System.Web.HttpCookie(name)
// {
// Expires = DateTime.Now.AddMinutes(30),
// Value = _context.User != null ? _context.User.UserName : Guid.NewGuid().ToString()
// };
// _context.HttpContext.Response.Cookies.Add(cookie);
// return cookie.Value;
//}
//return _context.HttpContext.Response.Cookies[name].Value; if (_context.HttpContext.Session[name] == null)
{
if(string.IsNullOrEmpty(username))
_context.HttpContext.Session[name] = Guid.NewGuid().ToString();
else
_context.HttpContext.Session[name] = username;
}
return _context.HttpContext.Session[name].ToString();
} }
}
2. IWorkContext
using System.Web;
using LinFx.Security; namespace LinFx.Web
{
public interface IWorkContext
{
IUser User { get; set; }
HttpContextBase HttpContext { get; }
}
}
using System.Web;
using LinFx.Security; namespace LinFx.Web
{
public class WorkContext : IWorkContext
{
public IUser User { get; set; }
//private readonly HttpContextBase _httpContext = new HttpContextWrapper(System.Web.HttpContext.Current); public HttpContextBase HttpContext
{
get { return new HttpContextWrapper(System.Web.HttpContext.Current); }
} //public WorkContext(HttpContextBase contextBase)
//{
// _httpContext = contextBase;
//} //public HttpContextBase HttpContext
//{
// get { return _httpContext; }
//}
}
}
3. Ninject 注入
using LinFx.Caching;
using LinFx.Data;
using LinFx.Index;
using LinFx.Plugin.Search.Services;
using LinFx.Security;
using LinFx.Web;
using YLSPay.Data;
using YLSPay.Data.Service; [assembly: WebActivator.PreApplicationStartMethod(typeof(YLSPay.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(YLSPay.App_Start.NinjectWebCommon), "Stop")] namespace YLSPay.App_Start
{
using System;
using System.Web; using Microsoft.Web.Infrastructure.DynamicModuleHelper; using Ninject;
using Ninject.Web.Common;
using System.Data.Entity; public static class NinjectWebCommon
{
static readonly Bootstrapper bootstrapper = new Bootstrapper(); /// <summary>
/// Starts the application
/// </summary>
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
} /// <summary>
/// Stops the application.
/// </summary>
public static void Stop()
{
bootstrapper.ShutDown();
} /// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>(); RegisterServices(kernel);
return kernel;
} /// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<HttpContext>().ToMethod(ctx => HttpContext.Current).InRequestScope();
kernel.Bind<HttpContextBase>().ToMethod(ctx => new HttpContextWrapper(HttpContext.Current)).InRequestScope(); kernel.Bind<IWorkContext>().To<WorkContext>().InSingletonScope(); kernel.Bind(typeof(IRepository<>)).To(typeof(Repository<>)).InRequestScope(); }
}
}
问题:
一。如果实现 方步 GetRecordId() 从 cooike 取 ?
二。_context.HttpContext 是会每次都新生成?
每次 new
ShoppingCartService 时就 new 一个 httpcontext ?
各位兄弟,有漏洞吗
cooike 要保存什么东西?
一个guid值? 然后 购物车 数据 保存 数据库存?
asp.net mvc cooike 购物车 如何实现的更多相关文章
- 在ASP.NET MVC实现购物车,尝试一种不同于平常的购物车显示方式
通常,我们看到的购物车是这样的: 虽然这种购物车显示方式被广泛运用,但我个人觉得不够直观.如果换成这样呢? 本篇的源码放在了:https://github.com/darrenji/ShoppingC ...
- 【Pro ASP.NET MVC 3 Framework】.学习笔记.7.SportsStore:购物车
3 创建购物车 每个商品旁边都要显示Add to cart按钮.点击按钮后,会显示客户已经选中的商品的摘要,包括总金额.在购物车里,用户可以点击继续购物按钮返回product目录.也可以点击Check ...
- ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第一章:创建基本的MVC Web站点
在这一章中,我们将学习如何使用基架快速搭建和运行一个简单的Microsoft ASP.NET MVC Web站点.在我们马上投入学习和编码之前,我们首先了解一些有关ASP.NET MVC和Entity ...
- 自学MVC看这里——全网最全ASP.NET MVC 教程汇总
MVC架构已深得人心,微软也不甘落后,推出了Asp.net MVC.小编特意整理博客园乃至整个网络最具价值的MVC技术原创文章,为想要学习ASP.NET MVC技术的学习者提供一个整合学习入口.本文从 ...
- ASP.NET MVC 5 05 - 视图
PS: 唉,这篇随笔国庆(2015年)放假前几天开始的,放完假回来正好又赶上年底,公司各种破事儿. 这尼玛都写跨年了都,真服了.(=_=#) 好几次不想写了都. 可是又不想浪费这么多,狠不下心删除.没 ...
- ASP.NET MVC学前篇之扩展方法、链式编程
ASP.NET MVC学前篇之扩展方法.链式编程 前言 目的没有别的,就是介绍几点在ASP.NETMVC 用到C#语言特性,还有一些其他琐碎的知识点,强行的划分一个范围的话,只能说都跟MVC有关,有的 ...
- ASP.NET MVC学前篇之Ninject的初步了解
ASP.NET MVC学前篇之Ninject的初步了解 1.介绍 废话几句,Ninject是一种轻量级的.基础.NET的一个开源IoC框架,在对于MVC框架的学习中会用到IoC框架的,因为这种IoC开 ...
- [ASP.NET MVC 小牛之路]02 - C#知识点提要
本人博客已转移至:http://www.exblr.com/liam 本篇博文主要对asp.net mvc开发需要撑握的C#语言知识点进行简单回顾,尤其是C# 3.0才有的一些C#语言特性.对于正在 ...
- [ASP.NET MVC 小牛之路]04 - 依赖注入(DI)和Ninject
本人博客已转移至:http://www.exblr.com/liam 为什么需要依赖注入 在[ASP.NET MVC 小牛之路]系列的理解MVC模式文章中,我们提到MVC的一个重要特征是关注点分离( ...
随机推荐
- 30款jQuery常用网页焦点图banner图片切换 下载 (转)
1.jquery 图片滚动特效制作 slide 图片类似窗帘式图片滚动 查看演示 2.jquery幻灯片插件带滚动条的圆形立体图片旋转滚动 查看演示 3.jQuery图片层叠旋转类似洗牌翻转图片幻灯片 ...
- chrome Provisional headers are shown错误提示
1.一般出现这个错误是请求没有发送成功 可能原因:在上传文件或ajax上传时指定的timeout,过时时间小 其他资料: http://www.duanzhihe.com/575.html http: ...
- 'swap file "xx" exists' linux
solution: 1)swap to another tty, kill processes using 'sudo kill -9 pid' 2)'Recover' the 'warn-openn ...
- csms发布步骤
1.wcf发布 通过点击 CSMS2.Application,右键发布按钮,将文件拷贝 2.打包文件 CSMS2.Resources 中 update.config 修改为对应 ServerUrl地址 ...
- zepto源码研究 - fx_methods.js
简要:依赖fx.js,主要是针对show,hide,fadeIn,fadeOut的封装. 源码如下: // Zepto.js // (c) 2010-2015 Thomas Fuchs // Zept ...
- 向Dialog中添加一个新的Menu
1.创建一个新的Menu,在资源管理视图中,右键Menu-->传入Menu 2.设计新Menu,ID为IDR_MENU1 3.在该Dialog的源文件中,找到CTest001Dlg::OnIni ...
- php判断是移动端还是pc
//判断是否是手机 function is_mobile() { $agent = strtolower($_SERVER['HTTP_USER_AGENT']); $is_pc = (strpos( ...
- Css3案例
<!DOCTYPE html> <html> <meta charset=utf-> <head> <style> body{ backgo ...
- iScroll 下拉刷新
<!doctype html> <html> <head> <meta charset="utf-8"> <script ty ...
- 基于python的接口测试学习笔记一(初出茅庐)
第一次写博客笔记,讲一下近来学习的接口自动化测试.网上查阅了相关资料,最后决定使用python语言写接口测试,使用的是python的第三方库requests.虽然python本身标准库中的 urlli ...