10

I'm pretty new to programming in general (really started only 2 1/2 years ago) and I'm trying to decide what the best way is to approach a web app I'm making at work. A senior developer at work is encouraging me to get into MVC and after a good 24 hours of pouring over blogs, source code and other material on the subject I'm beginning to understand why I'd want to use it.

At the same time though, our company's existing apps are written as WebForms so I don't want to do something as drastic as using the actual ASP.NET MVC framework to make my app(would it really be THAT drastic though?).

What I'd really like to know is whether or not it would be practical or even possible to do WebForms but still follow the MVC philosophy of separation of concerns. Will I really just be adding an unnecessary layer to an already complicated .aspx + codebehind page?

Everyone in the blogosphere seems to think that they MUST use some kind of framework if they want to do MVC. What in WebForms is stopping them from just doing it themselves?

John Farrell
  • 24,673
  • 10
  • 77
  • 110
Gagege
  • 650
  • 3
  • 8
  • 20

4 Answers4

9

If a senior in your team is encouraging you to look into MVC for your app and you think it's a good move then go for it (if this app is stand-alone especially).

You can also look into the MVVM pattern. This is what many have done with WebForms and it is very similar to the MVC pattern. By applying the MVVM pattern to WebForms you would be showing how one can still use WebForms but get much of the goodness that is in the MVC pattern with ASP.Net MVC. It would be a nice way to show other devs in the team what can be done to make WebForms more SOC and testable without abandoning WebForms outright.

Here's a few more links on MVVM:

http://weblogs.asp.net/craigshoemaker/archive/2009/11/03/vm-workshop-model-view-viewmodel-mvvm-and-the-presentation-model-pattern-in-5-ui-platforms.aspx

http://russelleast.wordpress.com/2008/08/09/overview-of-the-modelview-viewmodel-mvvm-pattern-and-data-binding/

MVVM is also very popular in Silverlight apps....

There is also the MVP pattern as well. Here's an open source implementation for WebForms. This particular implementation is used by DotNetNuke 5.3.

A bit more explanation from MS on MVP and .Net

Either one of these is a great choice if you want to gain more control of your code but still have the WebForms features you enjoy and/or want to continue to have for any reason such as what it sounds like in your case of a lot of legacy code using it.

Community
  • 1
  • 1
Kevin LaBranche
  • 20,908
  • 5
  • 52
  • 76
3

Yes, each version of MVC is even more drastically different from web forms than the previous one.

If you really don't want to use actual MVC, check out MVP (Model - View - Presenter) pattern.

Necros
  • 3,004
  • 23
  • 29
  • 1
    We used MVP at a previous job of mine, and it worked really well separating the UI from the Business Layer. A nice alternative to MVC if you are stuck in Webforms. – Martin Aug 20 '10 at 18:41
3

IMHO, I think you should just make the move to MVC.

Here are a few reasons that I am making the transition:

  • I like to be able to control my HTML output. [There are certain web controls that render different tags based on the browser. Additionally, it is nice to have control of the Client ID.]
  • I like to be able to manage the state of my aplications. [As opposed to having WebForms try to manage it for me, passing larger chunks of data around than are strictly necessary.]
  • I prefer to work in a test driven manner and I've found that I can get better test coverage with MVC.
  • I've found that libraries like JQuery work incredibly well with MVC; I've found that making UI elements that were a pain in WebForms have become trivial in MVC.

So, getting back to your question... can you build WebForms apps that utilize many of the lessons of MVC. Sure, and if you are going to build WebForms apps I'd recommend it as a good practice. Can you apply frameworks to make it more like MVC. Sure, but why would you want to when you can just use MVC?

joe.liedtke
  • 582
  • 2
  • 14
  • Agree with everything except the first point. I hear web forms 4 are better at that. – Necros Aug 20 '10 at 18:52
  • Sorry but I must disagree... WebControls such as the Panel control make certain assumptions about what they should output in various conditions. [For example, if I recall correctly they output a DIV to IE and a table to Firefox... this might have changed as I recall running into this with ASP.Net 1.1] Additionally, the framework insists upon controlling the client side ID's for any runat=server elements. Depending on the application, these may not be issues and I may just be pickier than most about my HTML output. – joe.liedtke Aug 20 '10 at 20:55
  • You've missed the most important point from my perspective. With MVC your state is transferred as data, not as page/control states. When you want to get user input, you don't have to explicitly state that you want a textbox, checkbox or dropdown, etc. You can use input builder conventions to drive what is rendered to the user based on your model. This allows you decouple the business data/process from the UI. – Ryan Aug 20 '10 at 22:21
  • Maybe in older versions, but i was referring to the changes made to control html rendering and client id modes in ASP.NET 4. See http://weblogs.asp.net/scottgu/archive/2010/03/30/cleaner-html-markup-with-asp-net-4-web-forms-client-ids-vs-2010-and-net-4-0-series.aspx – Necros Aug 21 '10 at 15:27
  • @Necros - Thanks for mentioning that about ASP.NET 4, I hadn't seen that yet. [I still must disagree that WebForms provides better control over HTML output than MVC, however it looks like it is much better than it was.] --- @Ryan - Well stated, that is a point that was missed in my comments. – joe.liedtke Aug 23 '10 at 17:49
0

Yes, you can still use WebForms and apply an MVC philosophy. For the most part, Web Forms does not prevent you from applying MVC principles; it simply can lead you down the wrong path. If you are still a WebForms shop, try applying some of the following things that are in the "spirit" of MVC. This will help make a future move from WebForms to MVC less drastic.

  • Use Routing. In WebForms you can map routes to .aspx Pages rather than controllers to get nice extensionless Urls. If you are using .Net 3.5 you will need a Custom Route Handler. In 4.0, routing is built-in.

  • Avoid using event handlers in your code behind. Page_Load will be enough in most cases.

  • Avoid using ViewState and Web Controls to keep your markup clean and lean. If you have a submit button in your markup, write it like this <input type="button" rather than like this: <asp:Button... You can expose properties and use foreach loops using <%= %> just like you see in MVC rather than using Label and Repeater controls.

  • Use JQuery for setting up event handlers, AJAX, and client side DOM manipulation.

  • Adopt the ViewModel concept by associating a ViewModel object with your .aspx pages.

  • If you need WebControls, use ClientIDMode="static" for control over your Ids in the markup (.Net 4.0)

James Lawruk
  • 30,112
  • 19
  • 130
  • 137