博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
asp.net mvc cooike 购物车 如何实现
阅读量:5202 次
发布时间:2019-06-13

本文共 6073 字,大约阅读时间需要 20 分钟。

先上代码:

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
_repository; public ShoppingCartService( IWorkContext context, IRepository
repository) { _context = context; _repository = repository; } ///
/// 加入购物车 /// ///
用户 ///
商品 ///
数量 ///
属性 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();        ///         /// Starts the application        ///         public static void Start()         {            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));            bootstrapper.Initialize(CreateKernel);        }                ///         /// Stops the application.        ///         public static void Stop()        {            bootstrapper.ShutDown();        }                ///         /// Creates the kernel that will manage your application.        ///         /// 
The created kernel.
private static IKernel CreateKernel() { var kernel = new StandardKernel(); kernel.Bind
>().ToMethod(ctx => () => new Bootstrapper().Kernel); kernel.Bind
().To
(); RegisterServices(kernel); return kernel; } ///
/// Load your modules or register your services here! /// ///
The kernel. private static void RegisterServices(IKernel kernel) { kernel.Bind
().ToMethod(ctx => HttpContext.Current).InRequestScope(); kernel.Bind
().ToMethod(ctx => new HttpContextWrapper(HttpContext.Current)).InRequestScope(); kernel.Bind
().To
().InSingletonScope(); kernel.Bind(typeof(IRepository<>)).To(typeof(Repository<>)).InRequestScope(); } }}

  

 

问题: 

一。如果实现 方步 GetRecordId() 从 cooike 取 ?

二。_context.HttpContext  是会每次都新生成?

     每次 new 

ShoppingCartService  时就 new 一个 httpcontext ?

各位兄弟,有漏洞吗

 

 可能思路不对
cooike 要保存什么东西? 
 
 未登录时情况
一个guid值? 然后 购物车 数据 保存 数据库存? 

转载于:https://www.cnblogs.com/LinFx/p/3141374.html

你可能感兴趣的文章
Maximum Product Subarray
查看>>
easyui dialog 表单提交,弹框初始化赋值,dialog实现
查看>>
记忆化搜索算法(MemorySearch)显示路径的问题
查看>>
div样式调整小结
查看>>
shell 默认变量
查看>>
solr相关配置翻译
查看>>
房天下爬虫
查看>>
通过beego快速创建一个Restful风格API项目及API文档自动化(转)
查看>>
JAVA-面向对象编程(上册)一、二章总结
查看>>
解决DataSnap支持的Tcp长连接数受限的两种方法
查看>>
Synchronous/Asynchronous:任务的同步异步,以及asynchronous callback异步回调
查看>>
ASP.NET MVC5 高级编程-学习日记-第二章 控制器
查看>>
在HTML中使用JavaScript(浏览器对js的加载机制分析)
查看>>
获取字符串中出现最多的字符 (HashMap()储存)
查看>>
如何选择适合自己的云管理平台(一)
查看>>
Hibernate中inverse="true"的理解
查看>>
不同版本(2.3,2.4,2.5,3.0)的Servlet web.xml 头信息
查看>>
Java的String中的subString()方法
查看>>
selenium +chrome headless Adhoc模式渲染网页
查看>>
高级滤波
查看>>