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的一个重要特征是关注点分离( ...
随机推荐
- FileUpload 简单上传+小预览
页面代码 : <form id="form1" runat="server"> <div> <asp:FileUpload ID= ...
- linux性能分析命令top
发布时间: 2013-12-14浏览次数:154分类: 服务器 top是linux最常用的性能分析工具了,它是个交互式工具,提供系统的整体性能,如正在执行的进程信息包括进程ID,内存占用率,CPU占用 ...
- iOS之使用QLPreviewController打开文件,处理txt文件出现乱码的情况
iOS之使用QLPreviewController打开文件,处理txt文件出现乱码的情况 主要代码: - (id <QLPreviewItem>)previewController:(QL ...
- JavaScript 本地对象、内置对象、宿主对象
首先解释下宿主环境:一般宿主环境由外壳程序创建与维护,只要能提供js引擎执行的环境都可称之为外壳程序.如:web浏览器,一些桌面应用系统等.即由web浏览器或是这些桌面应用系统早就的环境即宿主环境. ...
- 在Mvc中创建WebApi是所遇到的问题
1.提示"The 'ObjectContent`1' type failed to serialize the response body for content type 'applica ...
- JS-商品图片点击轮换
//小图预览区域图片轮换键const LIWIDTH=62;var moveCount=0;document.getElementById("btForward").onclick ...
- 转载:Java连接MySQL 数据库的正确操作流程
转载网址:http://www.bitscn.com/pdb/mysql/201005/186551.html 以下的文章主要介绍的是Java连接MySQL 数据库(以MySQL数据库为例 ...
- JQUERY1.9学习笔记 之基本过滤器(一) 动态选择器
动态选择器:animated Selector 描述:当选择器运行时,选择动态进程中的所有元素.(对动态进程起作用) jQuery( ":animated" ) 注释::anima ...
- 文成小盆友python-num11-(2) python操作Memcache Redis
本部分主要内容: python操作memcache python操作redis 一.python 操作 memcache memcache是一套分布式的高速缓存系统,由LiveJournal的Brad ...
- jquery keyup 在IOS设备上输入中文时不触发
今天做一个异步查询功能的时候发现在IOS设备上查询中文时keyup没有触发,在其他设备上时可以的,后来在stackoverflow上找到下面这种解决方法,贴出来算是抛砖引玉了. $h_input.on ...