title: Shopping Cart in Pro ASP.NET MVC 4 by Adam Freeman
body: <p>In the <strong>Pro ASP.NET MVC 4</strong> *<em>4th</em>* Edition By <em>Adam Freeman</em> and in "<em>Building the Shopping Cart</em>" page 219 he defined a <em>Cart Entity</em>:</p>
  
  <pre><code> public class Cart
  {
      private List&lt;CartLine&gt; lineCollection = new List&lt;CartLine&gt;();
  
      public void AddItem(Product product, int quantity)
      {
          CartLine line = lineCollection
              .Where(p =&gt; p.Product.ProductId == product.ProductId)
              .FirstOrDefault();
  
          if (line == null)
          {
              lineCollection.Add(new CartLine { Product = product, Quantity = quantity });
          }
          else
          {
              line.Quantity += quantity;
          }
      }
        //Other codes
  
      public class CartLine {
          public Product Product { get; set; }
          public int Quantity { get; set; }
      }
  }
  </code></pre>
  
  <p>This model is calling from <strong>AddToCart</strong> action method:</p>
  
  <pre><code>public RedirectToRouteResult AddToCart(Cart cart, int productId, string returnUrl)
          {
              Product product = ctx.Products.FirstOrDefault(p =&gt; p.ProductId == productId);
  
              if (product != null)
              {
                  cart.AddItem(product, 1);
              }
  
              return RedirectToAction("Index", new { returnUrl });
          }
  </code></pre>
  
  <p>When for the fist time we add a product to shopping cart it adds to <strong><em>"lineCollection"</em></strong> list. But if we add this product again "<strong>line.Quantity</strong>" increases and "<strong><em>lineCollection</em></strong>" <em>gets update <strong>too</em></strong> ("<strong>Quantity</strong>" property of this product in "<strong><em>lineCollection</em></strong>" list increases <strong><em>too</em></strong>). And my question is how this update (increase in "<strong><em>lineCollection</em></strong>"'s product <strong>Quantity</strong>) happens? We did not change "<strong>lineCollection</strong>" directly? </p>
  
  <p>Sorry for:</p>
  
  <ul>
  <li>My bad English </li>
  <li>My messy question</li>
  </ul>