This project is read-only.

Building an ASP.NET using NMVP

Topics: Developer Forum, User Forum
Feb 18, 2007 at 12:40 PM
I think you guys have done a truly amazing job with this framework. I came across MVP awhile back and read a lot on it from Martin Fowler and the hoard of material on the web. I just downloaded the entire set of NMVP code and documentation but have no idea from where to start. Can someone help me out here please?

I am specifically looking at :
- building a composite controls in many web pages
- Setting up triads in the configuration file
- State persistence
- Loading web user controls dynamically (this seems to be supported)
- Ability to resume where the user left out (for instance in a page, if the user closes the browser, when he comes back, automatically come to this page)

Hmm.. quite a few. I think I just need a quickstart besides the login example that I found. Would greatly appreciate it! :)

Feb 22, 2007 at 3:01 AM
Well that´s a lot of ground to cover, but hey I´ll try my best. Maybe the guys using the framework can help me out here.
If you downloaded the source code there is a sample that covers Composite MVP (what you are trying to achieve).
I´ll answer your post one by one ok?

1) building a composite controls in many web pages
If I understand this one correctly what you looking at is a triad of Contract - View - Presenter that you can use in several pages. Even though this is a little unorthodox (usually the triad is only good in one scenario) I´ve used it in some cases. All you have to do is build an Interface that inherits from IContract, create a presenter class that inherits from Presenter<Place here the interface you built> and an user control that implements the interface you built.
Now drag the user control to the page and create a config file that tells the Resolver that the Presenter class you´ve created (full name of the presenter) is bound to the contract you´ve created. Make sure this page inherits from MvpHost, since MvpHost will help you implement the rest of the needed properties.

2) Setting up triads in the configuration file
Answered in previous post.

3) State persistence
I´m not sure what you mean here. If it´s ViewState support we just rely on what ASP.Net already provides. So if you have a contract (interface that implements IContract) with a property called Name, in the ASP.Net user control this property will be like this:
public string Name{
  get{
    return this.txtName.Text;
  }
  set{
    this.txtName.Text = value;
  }
}
This way you get state persistence between posts.
Now if you are talking about object persistence to a data source, this feature will only be available in NMVP 0.4.0.

4) Loading web user controls dynamically (this seems to be supported)
For this check out the Sample in the source code in the folder Project. Check out the ProjectMembersContainer.ascx user control. It loads members completely dinamically.
Ability to resume where the user left out (for instance in a page, if the user closes the browser, when he comes back, automatically come to this page)

5) Ability to resume where the user left out (for instance in a page, if the user closes the browser, when he comes back, automatically come to this page)
Now this one is tricky and I don´t think it´s in the scope of the MVP pattern to solve this. Anyway if you have any ideas that might lead this way I´m sure it´s a feature that every one will appreciate, but as of now you must implement this on your own. I´d suggest relying on the ASP.Net Profile system if you need this kind of behavior though.

I hope I clarified things a little. Please let me know if you need further assistance.

Bernardo Heynemann
Feb 22, 2007 at 2:39 PM
If I may offer some tidbits of advice based upon my recent project "adventures" :

I am fairly new to the Asp.Net scene myself, so I asked the same questions recently. I think most of these questions questions 3, a little of 4, and 5 actually concern ASP.Net istelf rather than the NMvp framework. presenter triads are designed to help the business logic of an application, whereas those questions focus on the context of the application, of which a presenter triad should be independent. That said, here are some things I learned:

State Persistence-
There are a few ways to persist state on postbacks and whatnot. All presenters should be stateless and have no reference to System.Web or System.Windows.Forms. Only the view and model should have state. Asp.Net has at least 3 state persistence mechanisms: ViewState, Session, and Application. There is also Cache, but I beleive that is a more complex method. Each mechanism has disadvantages and advantages, and I strongly suggest searching online for detailed explanations. For example, viewState is fast is good for storing simple data types betweens postBacks, whereas Session is better for moderate-sized data of some complexity, and can store data throughout multiple Page calls (PostBack or no PostBack), but is slower than ViewState. As Heynemann pointed out, ASP.Net uses viewState all the time to maitnain the state of controls on postBacks.

Loading Web Controls dynamically-
I had a recent debacle with this one. It is supported and the Sample project gives a great example of it. However, there is an issue. The isFirstCall argument in a presenter's Load() and Init() functions are based off of the "!IsPostBack" value. This is a very nice feature, but the "geniuses" at Microsoft decided that a userControl's IsPostBack property should be the same as the Page's IsPostBack property. So if you load userControls dynamically, the Page has already been loaded and the userControls' IsPostBack proeprty will always be true. To work around this, I created a "IsFirstCall" property (that is persisted through the ViewState) and is true only when the control loads for the first time. I cannot tell you how many present and future headaches this solved.

Ability to resume where the user has left out -
I believe this is not only out of the scope of the framework, but out of the scope of ASP.Net. It is up to the browser what pages to go to when it loads up. If however, you mean the user should be able to go to the page again and see the same data as when they left, this is a matter of issue 3, state persistence. You can store the state in a dtabase and also there are articles on the web that describe storing "User Settings" for web applicaitons. I myself am not familiar with the second technique. My company uses the first technique (but lets not get into the spaghetti code that is my company's website :-).

In addition to your questions, I strongly recommend learning the Asp.Net application lifecycle of event, if you have not already. Learning the event order has helped me immensely in knowing what I can call and when I can call it.

hopefully this helps. :-)
Feb 22, 2007 at 6:35 PM
Couldn´t have said better.

Bernardo Heynemann
Feb 23, 2007 at 4:44 AM
Thanks Brian and Gilligan for your invaluable comments. I cannot disagree to the comments on those things that are in the scope of the framework and those that are not. Unfortunately, some of them are requirements which I am trying to implement in my solution in such a way that I can not only capitalize on the power of NMVP but also build something that I can reuse down the line.

I totally appreciate the power of the framework and am learning the way it works. Brians comments were invaluable. Would help me to move forward.

Thank you guys. I am sure to post more as I continue to play around with NMVP.