Click here to Skip to main content
12,078,090 members (46,674 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


9 bookmarked

TDD Approach to ASP.NET WebForm Development

, 10 Feb 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
An MVC pattern for WebForm development


From a general perspective, a testable application is an application that is loosely coupled enough to allow its independent parts to be tested in isolation. The approach taken in many architectures is to find the optimal amount of abstraction without creating too much overhead.

In many ASP.NET Web applications, the Web Pages and the User Controls contain not only the logic to render the data to the browser but even the business logic for the front end and the back end of the application. This is because the Web Page architecture in ASP.NET, the Page controller, doesn't encourage the separation of the application flow logic and the display logic. The bad result is to have a lot of not testable code.

The MVC is a paradigm that provides some architectural guidance by defining three layers for an application.

The new ASP.NET MVC framework was designed to allow developers to create testable applications, but most of the ASP.NET applications have a technical debt that can't be removed by introducing the new technology.

The purpose of this article is to offer a concrete example of separation of concerns in an ASP.NET WebForm scenario and to show how it is possible to write much more testable code.


From Dino Esposito article:

The MVC is a paradigm that provides some architectural guidance by defining three layers for an application.

The first part of the MVC pattern is the view, which contains logic for rendering data to a client. Originally, this was a user interface, but a client can be a Web browser, a Web service, client-side JavaScript, and so on. The view should be used only to render data for a consumer, and the amount of logic needed should be abstracted into different classes as much as possible.

The model represents the back end of an application. This part of the application, which is very loosely defined in the ASP.NET MVC implementation of the pattern, can take on many different forms, and it will very likely depend on the scope of your application. In a small application with little complex business logic, for example, this layer might simply be a set of business objects using the Active Record pattern that you interact with directly in the controller layer.

The controller orchestrates the flow of the application by taking data from the model and passing it to the appropriate view. Since this class is separate from the display logic, by employing a few techniques you should be able to instantiate this class in a unit test and have no dependencies on the ASP.NET runtime. This enables complete testing of the controller without having to run inside a Web server...

Using the Code

Add a reference of MVCWebForm class library to your WEB project and follow the steps:

  1. Create the View: it represents your WebForm or UserControl


    public partial class HelloWorldView : System.Web.UI.Page 
  2. Create the Presentation model for the View: it represents the entity model of your WebForm.

    Each entity must inherit from PresentationEntityBase.


    public class Message : PresentationEntityBase 
  3. Create the Controller for the View: it represents the controller associated to the View.

    Each controller must inherits from ControllerBase


    public class HelloWorldController: ControllerBase
  4. Create one or more DataSources: it represents the data access.

    You must implement the generic abstract class PresentationDataSource<T>.

    Each datasource is related to an entity of your model.


    public class MessageDataSource: PresentationDataSource<Message> 
  5. Create the Controller in the View with the ControllerFactory:


    controller = ControllerFactory.Instance.Get<HelloWorldController>
    					(new MessageDataSource()); 

    You can add more datasource to a controller.


    controller = ControllerFactory.Instance.Get<HelloWorldController>();
    controller.AddDataSource(new MessageDataSource());
  6. Access your datasource from the controller.

    The ControllerBase class provides standard methods to access a datasource:


    Message message = Create<Message>(); 


    TEntity Create<TEntity>() 
    TEntity Update<TEntity>(TEntity item) 
    void Delete<TEntity>(TEntity item) 
    IList<TEntity> FindAll<TEntity>() 
    IList<TEntity> FindBy<TEntity>(ICriteria criteria) 
  7. Unit Test the controller: you can create datasource stubs for your controller and create your Unit Test


    public void HelloWorld()
    HelloWorldController controller = 
    controller.AddDataSource(new MessageDataSourceStub());
    Label label = new Label();
    Assert.IsTrue(label.Text == "This is a UT");


  • 10th February, 2010: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Max Civino
Software Developer (Senior) SnowValley Ltd
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
babakzawari12-Feb-10 10:11
memberbabakzawari12-Feb-10 10:11 
Bad Article
GeneralRe: My vote of 1 Pin
Sitary12-Feb-10 15:11
memberSitary12-Feb-10 15:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160212.1 | Last Updated 10 Feb 2010
Article Copyright 2010 by Max Civino
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid