先上代码:
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值? 然后 购物车 数据 保存 数据库存?