如何跟踪多个项目中的购物车? [英] How can I keep track of a shopping cart across multiple projects?

查看:83
本文介绍了如何跟踪多个项目中的购物车?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我们将创建一个新的 eGov应用程序。最终,公民可以申请许可证并支付许可证,以及在线支付水电费和停车票。我们的愿景是拥有一个购物车,因此一个人可以在一次交易中为多个商品付款。为了使事情井井有条,我们将每个部分分成一个不同的项目。这也使我可以在一个项目上工作,而另一个开发人员在另一个项目上工作。付款人可以是注册用户,也可以保持未注册状态。我们觉得来自我们管辖范围之外的人可能不想注册,只是为了支付停车票或支付一次性营业执照。

First some background, we are creating a new "eGov" application. Eventually, a citizen can request permits and pay for licenses along with pay their utility bills and parking tickets online. Our vision has a shopping cart so a person can pay for multiple items in one transaction. To keep things organized better, we are going to break each section into a different project. This also allows me to work on one project while the other developer works another. The person making a payment could be a registered user or could remain unregistered. We feel a person from out of our jurisdiction probably doesn't want to register just to pay their parking ticket or pay for a one-time business license.

该项目将在Windows Server 2008和IIS7上,并使用ASP.NET MVC3。我们可能会使用单个域(可能是egov.domain.gov)和多个子目录(/ cart,/ permit,/ billing等),尽管那样尚未100%决定。

This project will be on Windows Server 2008 and IIS7 and using ASP.NET MVC 3. We will probably use a single domain (maybe egov.domain.gov) and in multiple sub directories (/cart, /permit, /billing, etc) though that is not 100% decided yet.

现在是问题所在。我们如何跟踪多个项目中的购物车?有人谈论使用在某个时间点到期的cookie或使用状态机。我们不确定使用会话ID是否可行。如果我们使用状态机,则我从未使用过它,而只是从概念上理解它(它可在多台机器上工作,因此SO会使用它)。

Now the problem. How do we track a shopping cart across multiple projects? There was talk of using a cookie that expires at a certain point or using a state machine. We are uncertain if using a session id would work. If we use a state machine, I have never used that and only understand it in concept (it works across multiple machines and SO uses it).

另外一个说明,我们将会在VMWare服务器上构建它,因此将来有可能在多个服务器上运行它。

One other note, we are going to be building this on a VMWare server, so the possibility of having this run across multiple servers is a possibility in the future.

您将使用什么?为什么?

What would you use and why?

更新:似乎很多人建议将购物车存储在HttpContext中。

Update: It appears like many seem to recommend storing the cart in HttpContext. Is this the same across multiple applications?

推荐答案

首先,您需要将SQL Server设置为接受会话状态连接。

First you need to setup your SQL Server to accept session state connections.

  • Article 1
  • Article 2

然后将以下内容添加到您的Web.config文件:

Then add the following to your Web.config file:

<sessionState mode="SQLServer" sqlConnectionString="Server=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=ASPState;Application Name=name" timeout="20" allowCustomSqlDatabase="true" />` within `<system.web>

然后我创建了一个包含两个类的类库:购物车 CartItem

I then created a class library that has two classes: Cart and CartItem.

CartItem 已定义容纳每个购物车商品

CartItem defined to hold each individual shopping cart item

[Serializable]
public class CartItem
{
    [Key]
    public int RecordId { set; get; }
    public string ItemNumber { set; get; }
    public string Description { set; get; }
    public DateTime DateTimeCreated { set; get; }
    public decimal Cost { get; set; }
}

购物车有效

public class Cart
{
    HttpContextBase httpContextBase = null;
    public const string CartSessionKey = "shoppingCart";

    /// <summary>
    /// Initializes a new instance of the <see cref="ShoppingCart"/> class.
    /// </summary>
    /// <param name="context">The context.</param>
    public Cart(HttpContextBase context)
    {
        httpContextBase = context;
    }

    /// <summary>
    /// Gets the cart items.
    /// </summary>
    /// <returns></returns>
    public List<CartItem> GetCartItems()
    {
        return (List<CartItem>)httpContextBase.Session[CartSessionKey];
    }

    /// <summary>
    /// Adds to cart.
    /// </summary>
    /// <param name="cartItem">The cart item.</param>
    public void AddToCart(CartItem cartItem)
    {
        var shoppingCart = GetCartItems();

        if (shoppingCart == null)
        {
            shoppingCart = new List<CartItem>();
        }

        cartItem.RecordId = shoppingCart.Count + 1;
        cartItem.DateTimeCreated = DateTime.Now;
        shoppingCart.Add(cartItem);

        httpContextBase.Session[CartSessionKey] = shoppingCart;
    }

    /// <summary>
    /// Removes from cart.
    /// </summary>
    /// <param name="id">The id.</param>
    public void RemoveFromCart(int id)
    {
        var shoppingCart = GetCartItems();
        var cartItem = shoppingCart.Single(cart => cart.RecordId == id);
        shoppingCart.Remove(cartItem);
        httpContextBase.Session[CartSessionKey] = shoppingCart;
    }

    /// <summary>
    /// Empties the cart.
    /// </summary>
    public void EmptyCart()
    {
        httpContextBase.Session[CartSessionKey] = null;
    }

    /// <summary>
    /// Gets the count.
    /// </summary>
    /// <returns></returns>
    public int GetCount()
    {
        return GetCartItems().Count;
    }

    /// <summary>
    /// Gets the total.
    /// </summary>
    /// <returns></returns>
    public decimal GetTotal()
    {
        return GetCartItems().Sum(items => items.Cost);
    }
}

要对此进行测试,请首先在我的购物车项目中我的家用控制器执行了以下操作:

To test this, first in my shopping cart project in my home controller I did the following:

    public ActionResult Index()
    {
        var shoppingCart = new Cart(this.HttpContext);
        var cartItem = new CartItem
        {
            Description = "Item 1",
            ItemNumber = "123"
            Cost = 20,
            DateTimeCreated = DateTime.Now
        };

        shoppingCart.AddToCart(cartItem);

        cartItem = new CartItem
        {
            Description = "Item 2",
            ItemNumber = "234"
            Cost = 15,
            DateTimeCreated = DateTime.Now
        };

        shoppingCart.AddToCart(cartItem);

        var viewModel = new ShoppingCartViewModel
        {
            CartItems = shoppingCart.GetCartItems(),
            CartTotal = shoppingCart.GetTotal()
        };

        return View(viewModel);
    }

在第二个项目的家庭控制器中,我添加了以下内容:

In my second project's home controller, I added the following:

    public ActionResult Index()
    {
        var shoppingCart = new Cart(this.HttpContext);
        var cartItem = new CartItem
        {
            Description = "Item 3",
            ItemNumber = "345"
            Cost = 55,
            DateTimeCreated = DateTime.Now
        };

        shoppingCart.AddToCart(cartItem);

        return View();
    }

这似乎对我有用。

这篇关于如何跟踪多个项目中的购物车?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆