17

I'm reading about ASP.net MVP pattern over this weekend and it seem like even the most simple task take too much effort if do it in MVP pattern the pay off seem to be at larger project but i think to myself if i'm going to follow MVP. Why not just do the project in ASP.net MVC?

The reason that I'm looking at MVP pattern is because I've noticed in all my ASP.net Webform projects there are a lot of code in the code behind just for event handling along if i have a lot of server control on the web form so i was looking at the way to reduce that and come across the MVP pattern.

Is it worth the effort to follow the MVP pattern or just switch over to the ASP.net MVC?

Ryan Kohn
  • 13,079
  • 14
  • 56
  • 81
Jack
  • 9,843
  • 23
  • 78
  • 111
  • 3
    Short answer: yes its worth the effort because you can get a lot of your code out of the code-behind file. This facilitates test-ability, maintain-ability, read-ability, and design-ability. – Kris Krause Jul 14 '13 at 12:50
  • Most teams and projects (mine included) simply cannot justify a complete MVC rewrite or all-or-nothing "switch" to MVC. – Kris Krause Jul 14 '13 at 12:51

5 Answers5

20

I would recommend reading the following two links to get you up to speed on MVP and MVC :

Should you switch over?
Based on what you've told me I would recommend that you use the Passive MVP model mentioned in the article above.

My main assumptions are:

  1. Your dealing with an existing codebase of WebForms apps
  2. You need to use ThirdParty .Net controls for existing functionality
  3. Your working on existing apps and don't have time to re architect them
  4. Any ASP.Net Web Apps you work on in the future, you can incrementally apply the passive MVP and get the benefits of TDD straight away

Your View (codebehind + aspx) essentially become dumb and just perform simple tasks:

  • take information given by the presenter
  • responds to events and provides information back to the presenter

I've used this model extensively for Web Forms development and I couldn't imagine not being able to Unit Test my Model and Presenter code. Once you establish your base model which doesn't take very long and seen the power of Unit Testing, working with Web Forms becomes enjoyable.

Some links to MVP stuff that the model I've used is based on:

I would also recommend that you learn MVC to.
When time permits, take an existing App and port it to MVC. This way your sole focus is getting to know MVC and when you move logic into the MVC pattern, you'll discover things that you implemented in WebForms and never gave much thought to but now need to solved in another way. Great way to compare the patterns and see what works for you.

Hope this helps, feel free to ask any questions.

Community
  • 1
  • 1
Ralph Willgoss
  • 11,750
  • 4
  • 64
  • 67
15

If you start a new project, then ASP.net MVC is a better choice. But if you just want to refactor an existing project as you just said, then MVP is the option because there is no easy way to convert those web-forms codes to MVC .

J.W.
  • 17,991
  • 7
  • 43
  • 76
  • why would ASP.NET MVC be a better choice? – cruizer May 04 '09 at 05:33
  • 1
    For one, MVC provides a lot of scaffolding out of the box on which to get started. This makes it very easy to do routing, scaffolding, testing and so on. – aleemb May 04 '09 at 11:09
  • @ aleemb, on the other side "traditional" asp.net "webforms" offer rich set of build-in and 3rd party reusable UI controls as opposed to asp.net MVC where you could control every bit going up/down to wire but will have to write much more html comparing to webforms.. there's always best tool for the job and mvc isn't always a best choice. – Jack0fshad0ws May 03 '13 at 04:46
  • 1
    @Jack0fshad0ws Sorry. All that "3rd party reusable UI controls" are utterly worthless if you do not have enough transparency to unit test them. Slapping together a DataSource with GridView seems fast and convenient, but watch it fall apart when you try to debug for anything. ASP.Net Webform is stateful + tightly coupled. Let it RIP. – Sleeper Smith Jul 25 '13 at 04:45
  • @Sleeper Smith there's more to asp.net "webforms" then "slapping" together gridview with datasource which is in fact only good for a quick n dirty demo.. it is a tried and tested framework and in a complex admin backend beats mvc hands down. If unit testing is the only benefit of MVC than it's not much of a benefit.. – Jack0fshad0ws Aug 31 '13 at 08:22
  • 3
    @Jack0fshad0ws "unit testing is the only benefit of MVC than it's not much of a benefit" should put that first so I can stop reading right there. – Sleeper Smith Sep 04 '13 at 03:14
3

In my opinion, the ideal way to go for new applications is MVC. However, if you have a lot of code already using WebForms, then the MVP pattern is the way to go.

ichiban
  • 6,162
  • 3
  • 27
  • 34
  • 1
    ASP.NET MVC gives you greater control of your HTML, makes your code more testable. It allows you to follow best practices, like "separation of concerns". – ichiban May 04 '09 at 06:29
3

I would go Asp.Net MVC if it's a new project, but I agree, MVP might be a good pattern for legacy webforms projects.

Here's an example from my blog: http://www.unit-testing.net/CurrentArticle/How-To-Use-Model-View-Presenter-With-AspNet-WebForms.html

T123
  • 49
  • 4
2

my personal opinion is that if there's a lot of code in the code behind, there still are ways other than adopting M-V-P in order to slim it down, refactor and make it testable.

if your page has extensive user interaction (like buttons/links enabling/disabling, panels and controls appearing/disappearing) M-V-P would be worth the hassle.

cruizer
  • 6,103
  • 2
  • 27
  • 34