ASP.NET MVC Framework and Session State via StateValue from WCSF
by David Hayden, ASP.NET Web Developer
I mentioned in the following post:
that for kicks I have been using the Composite Web Application Block in the Web Client Software Factory to provide various infrastructure level services like dependency injection, business module discovery and initalization, etc for use in my ASP.NET MVC Tutorials. I could just as easily use Castle Windsor or SpringFramework as my IoC Container as mentioned in this post:
And you can bet that I will discuss using the the ASP.NET MVC Framework with the Unity Dependency Injection Container with or without Enterprise Library 4.0 when it is released. I feel pretty comfortable with all these tools and recommend everyone have some knowledge of each of them.
My Online Green Tea Store Demo At South Florida Code Camp
At the South Florida Code Camp and Day of Patterns & Practices I presented a demo application of the ASP.NET MVC Framework using a online green tea store website complete with product catalog and shopping cart. Here are some screenshots of the store using the default site master and styles for ASP.NET MVC:



Shopping Cart Controller and Session State
Pretty simple. The shopping cart holds various kinds of green tea, like Gyokuro, Sencha, and Matcha, purchased by the user. The CartController is responsible for handling Shopping Cart Actions. Here is the sample code:
public class CartController : Controller
{
[SessionStateKey("Cart")]
public StateValue<Cart> Cart;
[ControllerAction]
public void Index()
{
Cart cart = GetCart();
RenderView("Index", cart);
}
[ControllerAction]
public void AddItem()
{
CartItem item = new CartItem();
item.UpdateFrom(Request.Form);
Cart cart = GetCart();
cart.Items.Add(item);
RedirectToAction("Index");
}
[ControllerAction]
public void DeleteItem(int productId)
{
Cart cart = GetCart();
cart.RemoveItem(productId);
RedirectToAction("Index");
}
private Cart GetCart()
{
if (Cart.Value == null)
Cart.Value = new Cart();
return Cart.Value;
}
}
One public field on this Controller might be foreign to you if you don't use the Web Client Software Factory -
[SessionStateKey("Cart")]
public StateValue<Cart> Cart;
That is essentially a wrapper around the ASP.NET Session State by default, unless you have created you own custom SessionState Object that implements IHttpSessionState and SessionStateLocatorService that serves up this object and uses a database, XML file, or other data store. When seeing this field, the CWAB will inject this field with the value in the ASP.NET SessionState that has the key of “Cart” as denoted by the SessionStateKey Attribute.
Why is this cool?
- Completely abstracts out the session state datastore, allowing me to change it to a database or something else if I need to later.
- Allows me to write cleaner and more maintainable code.
- I can now better unit test this code because I am not reliant on ASP.NET.
Patterns & Practices Screencasts
If this type of code interests you, I recommend visiting PnPGuidance, which has numerous screencasts on the Web Client Software Factory and Enterprise Library. I will be adding screencasts for the ASP.NET MVC Framework in the near future. Here are the screencasts to date:
Conclusion
I could see the MVC Toolkit getting a similar SessionState Wrapper :) Hope this helps!
Author: David Hayden, ASP.NET Web Developer
Site: http://www.davidhayden.com/