How to: Use Session State with Unit Testing

Typically, the unit tests you write for your Web application do not execute in a Web server. This means there is no session state available when the unit tests execute. When you require your application to store information in the session state, you can use the Composite Web Application Block class StateValue to store the information. This class supports code that runs both in a Web server environment (the session is available, and data is stored there) and outside of a Web server environment (no session is available, such as when you run your unit tests, and the data is stored in memory).
The steps in this topic explain how to use the StateValue class to manage state across requests.

Note:
The latest version of the Composite Web Application Block introduces the StateDependency attribute. You can use this attribute to inject a StateValue object into constructors, methods, and properties. For more details, see the section “StateDependency Attribute”.

Prerequisites

This topic assumes that you have an existing Web client solution. For information about how to create a Web client solution, see How to: Create a Web Client Solution. Frequently, you manage state in a business module. For information about how to create a business module, see How to: Create a Business Module.

Steps

The following procedure describes how to use the StateValue class in a module controller to manage state across requests.

To use the StateValue class to manage state across requests
  • Add a public field of type StateValue<T> to your module controller class, where T is the type of the object you want to persist across requests, as shown in the following code.
public StateValue<Customer> _currentCustomer; 

Note:
You must make the field public. When ObjectBuilder creates the module controller object, it reflects over the module controller class and injects the StateValue object. This means that you do not have to manually query the Session object of ASP.NET for the value; ObjectBuilder automatically injects the value for you.
  • To access the value, use the Value property of the StateValue class as shown in the following code.
// Set the value
_currentCustomer.Value = new Customer();

// Get the value
Customer customer = _currentCustomer.Value; 

By default, the application block combines the name of the class that declares the field with the field name to generate the key name of the session item (for example, "CustomersController;_currentCustomer"). This means that two fields with the same name but defined in different classes will be stored in different locations in the session.
If you want to share a session values across different classes (for example, you want two different presenters to access the same value for the current customer), or if you want to access a value at a known location the session, you can use the SessionStateKey attribute to specify the session item key, as shown in the following code.

[SessionStateKey("Customer")]
public StateValue<Customer> _currentCustomer; 


Note:
To allow ObjectBuilder to inject the StateValue objects into the module controller, you must use ObjectBuilder to create your module controller instance.

StateDependency Attribute

The latest release of the Composite Web Application Block application introduces a StateDependency attribute. You can use this attribute to inject a StateValue object into constructors, methods, and properties (in the previous version you could only inject StateValue objects into public fields), as shown in the following code.

class SampleClass
{
  private StateValue<string> _myStringValue;
  public SampleClass([StateDependency("stringKey")] StateValue<string> myStringValue)
  {
    _myStringValue = myStringValue;
  }
  public string MyStringValue
  {
    get { return _myStringValue.Value; }
  }
} 


Note:
When using the StateDependency attribute, the StateValue<T> field does not need to be public.

Outcome

You will have classes that use the ASP.NET session when available and that can be unit tested without a Web server.

Last edited Nov 20, 2007 at 1:32 PM by ejadib, version 2

Comments

No comments yet.