Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

TDD Approach to ASP.NET WebForm Development

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

Introduction

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.

Background

From Dino Esposito article:

http://msdn.microsoft.com/en-us/magazine/dd942838.aspx

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

    Example:

    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.

    Example:

    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

    Example:

    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.

    Example:

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

    Example:

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

    You can add more datasource to a controller.

    Example:

    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:

    Example:

    Message message = Create<Message>(); 

    Methods:

    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

    Example:

    [Test]
    public void HelloWorld()
    {
    HelloWorldController controller = 
    	ControllerFactory.Instance.Get<HelloWorldController>();
    controller.AddDataSource(new MessageDataSourceStub());
    Label label = new Label();
    controller.WriteMessage(label);
    Assert.IsTrue(label.Text == "This is a UT");
    }

History

  • 10th February, 2010: Initial post

License

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

Share

About the Author

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

Comments and Discussions

 
GeneralMy vote of 1 PinmemberMember 263856915-Feb-10 2:19 
GeneralMy vote of 1 Pinmemberbabakzawari12-Feb-10 9:11 
Bad Article
GeneralRe: My vote of 1 PinmemberSitary12-Feb-10 14:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.8.140916.1 | Last Updated 10 Feb 2010
Article Copyright 2010 by Max Civino
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid