Home .NET Using MVP pattern in classic ASP.NET

Using MVP pattern in classic ASP.NET

by admin

Have you ever encountered long-running ASP.NET projects?
Maybe you are currently working on such a project?
If so, you’ve most likely encountered a complex intertwined set of events, logic, and validations within page classes.
This article talks about how you can simplify life on such projects using the same Model-View-Presenter template.
I assume that you’re familiar with the MVC template and I won’t go into detail about it in this article.
I will try to match MVC with the features that classic ASP.NET provides:
Routing – request processing
The site folder tree can serve the same purpose in most cases.
2.View and PartialView – data representation
The Page class and UserControl are great for this purpose.
3. model – data
Model classes are most likely present in your system in some form.
4. Presenter – glue between data and presentation
Unfortunately, ASP.NET doesn’t provide this type of object.
This is what we are going to do now.
The purpose of the presenter is to prepare the data to be shown to the user and to process the data entered by the user.
The purpose of the presenter is to show data to the user and receive data from the user.
There are different opinions on how clever a presentation and presenter can be.
Since there is no consensus, I prefer to base it on the effectiveness of the implementation, which depends on the environment. In this case, the available ASP.NET library, which has a wide range of quite smart controls that I’d like to use to the full extent, since such a possibility is present.
Finally, let’s get down to implementation.
Create two classes PageView and UserControlView:

public abstract class PageView<T> : Page where T : class{private T _presenter;public T Presenter{get { return _presenter ?? (_presenter = CreatePresenter()); }}protected abstract T CreatePresenter();}public class UserControlView<T> : UserControl{public T Presenter { get; set; }}

The PageView class is abstract and assumes that the presenter is created independently in the descendant.
The UserControlView class has a single property, Presenter, which is initialized by the parent on which the user element is located.
It is assumed that the page presenter contains presenters of its custom elements.
Example implementation of a list and its element :
List.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="List.aspx.cs" Inherits="WebApplication1.Client.List" EnableViewState="false" %><%@ Register TagPrefix="ctl" TagName="Item" Src="Details.ascx" %><asp:Content ContentPlaceHolderID="MainContent" runat="server"><asp:Repeater ID="ctlList" runat="server" ><ItemTemplate><ctl:Item runat="server" Presenter="<%# Container.DataItem %> " /></ItemTemplate></asp:Repeater></asp:Content>

List.aspx.cs

public partial class List : PageView<ListPresenter>{protected override ListPresenter CreatePresenter(){return new ListPresenter();}protected override void OnLoad(System.EventArgs e){base.OnLoad(e);ctlList.DataSource = Presenter.Items;ctlList.DataBind();}}

Details.aspx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Details.ascx.cs" Inherits="WebApplication1.Client.Details" %><asp:TextBox runat="server" Text="<%# Presenter.Model.FirstName %> " OnTextChanged="OnFirstNameChanged" /><asp:TextBox runat="server" Text="<%# Presenter.Model.LastName %> " OnTextChanged="OnLastNameChanged" /><asp:Button runat="server" Text="Save" OnClick="OnSave" /><br />

Detalis.aspx.cs

public partial class Details : UserControlView<DetailsPresenter>{protected void OnFirstNameChanged(object sender, System.EventArgs e){Presenter.Model.FirstName = ((TextBox) sender).Text;}protected void OnLastNameChanged(object sender, System.EventArgs e){Presenter.Model.LastName = ((TextBox)sender).Text;}protected void OnSave(object sender, System.EventArgs e){Presenter.Save();}}

By the way, please note that ViewState is turned off on the pages.
The full example can be downloaded here
The merits of this approach :
1. The code of the page becomes much simpler
2. You can write modular tests on presenter classes
3. you can use the prezenters to implement different interfaces (web, win, mobile)
4. You can use in conjunction with noodlecode and upgrade the application gradually
I hope my experience will be helpful to you.

You may also like