Web Client Software Factory Session State StateValue for Unit Testing - Patterns and Practices

Web Client Software Factory Session State StateValue<T> for Unit Testing - Patterns and Practices

by David Hayden ( Microsoft MVP C# ), Filed: Web Client Software Factory

 

When unit testing Application Controller Classes, for example, that depend on session state to run, you don't have access to session state while testing. This makes it difficult to unit test the class without introducing a session state wrapper that can be mocked during testing. The Web Client Software Factory offers such a wrapper, called StateValue<T>, that helps you with unit testing classes that rely on session state variables to function.

 

StateValue<T>

The StateValue<T> Class gets special treatment by the Web Client Software Factory. Whenever a class is created by ObjectBuilder, which is a part of the Web Client Software Factory, ObjectBuilder looks for Public Fields of Type StateValue<T>. Whenever it finds one, it looks in Session State to find a corresponding value for that variable, and automatically sets the variable to the value found in Session State.

There are a couple of important points mentioned above to get the StateValue<T> Class to work appropriately in your ASP.NET Web Applications.

  • Classes Created by ObjectBuilder - ObjectBuilder has to create the classes in order for the session state values to get injected. It is ObjectBuilder that reflects on the class to find Public Fields of type StateValue<T> and does the injection. It won't work if you manually create the classes yourself.
  • Public Fields of StateValue<T> - The documentation insists on Public Fields.

 

If we have a ReportsController Class that needs access to the name of the current report stored in a session variable, we can add a public field of StateValue<string> to the ReportsController as such:

 

public class ReportsController
{
    [SessionStateKey("CurrentReportName")]
    public StateValue<string> CurrentReportName;
    
    public ReportsController() {}
}

 

The above specifies that the value of the Session State Variable CurrentReportName will be injected into the CurrentReportName Public Field when the ReportsController Class is created by ObjectBuilder. Notice I said injected. We don't need to manually access Session State and populate the field itself.

As mentioned above, however, ObjectBuilder must create this class for us to do the injection. CurrentReportName will always be null if we create it ourselves like:

 

ReportsController controller = new ReportsController();

 

The ReportsController Class has to be created by ObjectBuilder for automatic population of the session variable into the public field like:

 

public class ViewReportsPresenter : Presenter<IViewReports>
{
    private ReportsController _controller;
    
    public ViewReportsPresenter([CreateNew] ReportsController
controller) { _controller
= controller; } // ... }

 

The [CreateNew] Attribute in the constructor tells ObjectBuilder to create and inject an instance of ReportsController into the ViewReportsPresenter when the presenter class is created. During the creation of the ReportsController Class, ObjectBuilder will notice the CurrentReportName Public Field and inject it with the value of the Session State Variable CurrentReportName.

 

Conclusion

As mentioned above, StateValue<T> is a nice little gem in the Web Client Software Factory that helps loosely-couple your classes from session state as well as assists you with Unit Testing classes that depend on session state. See the Web Client Software Documentation for more information on this StateValue<T> class.

 

Web Client Software Factory Tutorials

You can read all my Web Client Software Factory Tutorials as well as join me at the Orlando Code Camp on March 24th, 2007 where I will be presenting the Web Client Software Factory.

Source: David Hayden ( Microsoft MVP C# ), Filed: Web Client Software Factory

 

posted on Saturday, February 24, 2007 5:00 PM

My Links

Post Categories

Article Categories

Archives

Loose-Leaf Tea