ASP.Net MVC without using 'The Framework' part 1
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.
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.
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).
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 https://apparch.codeplex.com/Wiki/View.aspx?title=App%20Pattern%20-%20Three-Tier%20Web%20Application%20Scenario%20(Domain%20Entity).
Keep posting good articles, Ron : )
@Fajar Endra Nusa Thanks for the link. It's awesome. I've actually read that through at some point ;)
[...] more than a year ago, I wrote two posts on opting for MVC pattern on Asp.net Webform (Asp.Net MVC without using ’˜the framework part1 and part 2: a good example of unit testing). A few months ago, I heard about Tatham Oddie's [...]