ASP.Net MVC without using ‘The Framework’ part 1

ScottGu and his team are working on MVC framework for ASP.net, which I really like.
Learning how to use the framework reminds me when I first started learning about the MVC pattern a couple of years back.
The following technique of implementing MVC on Webform was passed on by Nigel Thorne and Craig Ambrose when we were working for Open Windows.
Since then I have decided to call it Model View Presenter/Supervising Controller due to the emphasis on the role of the View delegating events to the Controller.


Please note that I am using the term Controller and Presenter interchangeably. However, from this point forward I will refer the Controller as Presenter to signify that it is a part of a MVP triad.
  • View is composed by web form or user control.
  • View *at most* only contains UI logic.
  • View propagate each page events to the Presenter.
  • View uses Data Binding for hydration.
  • Presenter contains complex application logic.
  • Presenter interacts with models and views.
  • Presenter access the View through an intermediary which act as a Gateway
  • Model contains data and business logic.

Versus MVC Framework

+ still have page events (depending on how one look at it, this could be a negative too)

+ lower learning curve

+ not relying on pre-released framework


– View still have a huge role in application logic

– a more intimate bind between View and Presenter than in MVC framework

Example
[edit 13-April-2009] The example below is using Model View Presenter/Passive View, where the view doesn’t know anything about the model.
Notice that I’m passing all the bindings using native types and calling the appropriate properties in the view.

View

// this interface will be adapted by the webform and testing stub/mock
public interface IShowMonkeyView
{
	string Id {get;}
	string Name {set;}
}

// webform
public partial class ShowMonkeyView : Page, IShowMonkeyView
{
	private MonkeyPresenter presenter;

	//constructor
	public ShowMonkeyView()
	{
		// inject the view to the presenter
		presenter = new MonkeyPresenter(this);
	}
 
	//page event
	public void ButtonClicked()
	{
		// propagate page event
		Presenter.ShowMonkey();
	}
	
	public string Id { return Request.QueryString("Id"); }

	// intermediary - databinding gateway
	public string Name { txtboxName.Value = value; }
}

Presenter

public class MonkeyPresenter
{
 
	private IShowMonkeyView view;
 
	//inversion of control constructor
	public MonkeyPresenter(IShowMonkeyView _view)
	{
		view = _view;
	}
 
	public void ShowMonkey()
	{
		// interact with Models
		IMonkey daMonkey = Monkey.Find(view.Id);

		// hydrate view
		view.Name = daMonkey.Name;
	}
}

References
Martin Fowler. Supervising Controller.

  • Anonymous

    That is a very concise definition Ron, with an easy to understand example. If only i was presented with this when i first learn of it :).

    What you are missing i suppose is a clear example of the benefits of this design pattern. But of course this is but part 1.

  • ronaldwidha

    Hey Xian! Thanks man. Glad you enjoyed it.

    > What you are missing i suppose is a clear example of the benefits of this design pattern.

    I didn’t want to cover what’s already been stated elsewhere. If you notice, unlike most blogs, my posts contains (hopefully) enough links to keep one busy for a while.

    The link on the reference: Martin Fowler’s Supervising Controller pattern discuss the very topic that you are asking (which you probably already knew, but still can enjoy).

  • Anonymous

    Developers used to create his/her own MVC approach for ASP.NET based web development, hehe.

    Anyway, Microsoft P&P provides an Application Architecture Pocket Guide for web app (pdf version). We can simply download it from http://apparch.codeplex.com/Wiki/View.aspx?title=App%20Pattern%20-%20Three-Tier%20Web%20Application%20Scenario%20(Domain%20Entity).

    Keep posting good articles, Ron : )

  • Anonymous

    @Fajar Endra Nusa Thanks for the link. It’s awesome. I’ve actually read that through at some point ;)

  • Pingback: Ronald Widha » Blog Archive » Model View Presenter on Asp.Net using WebFormsMVP framework