views:

472

answers:

6

Although ASP.NET MVC seems to have all the hype these days, WebForms are still quite pervasive. How do you keep your project sane? Let's collect some tips here.

+1  A: 

Start with Master Pages on day #1 - its a pain coming back to retrofit.

stephbu
+2  A: 
  • Create web user controls for anything that will be shown on more than one page that isn't a part of masterpage type content. Example: If your application displays product information on 10 pages, it's best to have a user control that is used on 10 pages rather than cut'n'pasting the display code 10 times.
  • Put as little business logic in the code behind as possible. The code behind should defer to your business layer to perform the work that isn't directly related to putting things on the page and sending data back and forth from the business layer.
  • Do not reinvent the wheel. A lot of sloppy codebehinds that I've seen are made up of code that is doing things that the framework already provides.
  • In general, avoid script blocks in the html.
  • Do not have one page do too many things. Something I have seen time and time again is a page that say has add and edit modes. That's fine. However if you have many sub modes to add and edit, you are better off having multiple pages for each sub mode with reuse through user controls. You really need to avoid going a bunch of nested IFs to determine what your user is trying to do and then showing the correct things depending on that. Things get out of control quickly if your page has many possible states.
  • Learn/Grok the page lifecycle and use it to your advantage. Many ugly codebehind pages that I've seen could be cleaner if the coder understood the page lifecycle better.
Daniel Auger
Everything here is good, except for the first point; using Master Pages completely negates the need for User Controls for shared presentation, anything that is shared between multiple pages should be in the Master Page.
Dexter
I probably wasn't clear enough, but I meant using controls for repetitive things such as displaying and editing business objects. Example: Say there are 10 pages that allow me to view a product. It's best to have one user control than cut'n'pasted HTML on 10 pages. Answer updated for clarification.
Daniel Auger
+3  A: 

I generally try to stay clear of it... but when i do use WebForms, i follow these precepts:

  1. Keep the resulting HTML clean: Just because you're not hand-coding every <div> doesn't mean the generated code has to become an unreadable nightmare. Avoiding controls that produce ugly code can pay off in reduced debugging time later on, by making problems easier to see.
  2. Minimize external dependencies: You're not being paid to debug other people's code. If you do choose to rely on 3rd-party components then get the source so you don't have to waste unusually large amounts of time fixing their bugs.
  3. Avoid doing too much on one page: If you find yourself implementing complex "modes" for a given page, consider breaking it into multiple, single-mode pages, perhaps using master pages to factor out common aspects.
  4. Avoid postback: This was always a terrible idea, and hasn't gotten any less terrible. The headaches you'll save by not using controls that depend on postback are a nice bonus.
  5. Avoid VIEWSTATE: See comments for #4.
Shog9
How can you avoid postbacks with webforms? Postbacks are at the very core of the webforms model. Does this mean the only code you have in your code behind is in the page load?
Paul Batum
Yes. That is exactly what it means. The end result is pages with very, very little code-behind *period*, since anything not directly concerned with building the page is in its own, purpose-specific class somewhere. It's... very freeing.
Shog9
I'd be interested to see examples of that in use - whilst it sounds great in theory, some things inherently need to be done server side, such as validation.
Dexter
Validation is simple enough: all forms post to an action-specific handler (.ashx), passing a parameter specifying the page to redirect to on success and failure. Each is given a message parameter describing the result (success, validation failure, Armageddon, etc.)
Shog9
For accessibility and supporting non-javascript and mobile devices you should always ensure that your forms are server side validated and javascript functions (esp multi-dropdowns) replicated server side.
Toby Mills
If you are going to avoid using postback you might as well use another language..
Greg
Language? Postback has nothing to do with my choice of language.
Shog9
@Shog9: You are unnecessarily complicating the ASP.NET web forms paradigm and obtusely avoiding a standard of development that most ASP.NET developers are trained to understand for no real purpose. How do you handle showing the form again if the data is invalid in your handler? You redirect back to the form? And what about the fields already filled out? Now you have to redirect with query parameters to allow the original form to repopulate itself? Postback is a good workable concept, but generally misunderstood by a lot of developers who started with a different web language.
Chris
@Chris: actually, you raise a good point - when preserving more than a little bit of user-entered data, posting back to the data-entry aspx does make it *much* easier to restore the values of the form fields. I still don't *like* it, but it's one of the many compromises that must be made... That said, it's worth thinking twice before using this technique, reserving it for only those scenarios where it actually does pay off: a lot of developers who *haven't* started on a different platform leave themselves open to headaches when they blindly accept that every action must result in a postback.
Shog9
A: 

Use version control and a folder structure to prevent too many files from all being in the same folder. There is nothing more painful than waiting for Windows Explorer to load something because there are 1,000+ files in a folder and it has to load all of them when the folder is opened. A convention on naming variables and methods is also good to have upfront if possible so that there isn't this mish-mash of code where different developers all put their unique touches and it painfully shows.

Using design patterns can be helpful in organizing code and having it scale nicely, e.g. a strategy pattern can lead to an easier time when one has to add a new type of product or device that has to be supported. Similar for using some adapter or facade patterns.

Lastly, know what standards your forms are going to uphold: Is it just for IE users or should any of IE, Firefox, or Safari easily load the form and look good?

JB King
+3  A: 

With large projects the best suggestion that I can give you is to follow a common design pattern that all your developers are well trained in and well aware of. If you're dealing with ASP.NET then the best two options for me are:

o Model View Presenter (though this is now Supervisor Controller and Passive View). This is a solid model pushing seperation between your user interface and business model that all of your developers can follow without too much trouble. The resulting code is far more testable and maintainable. The problem is that it isn't enforced and you are required to write lots of supporting code to implement the model.

o ASP.NET MVC The problem with this one is that it's in preview. I spoke with Tatham Oddie and be mentioned that it is very stable and usable. I like it, it enforces the seperation of concerns and does so with minimal extra code for the developer.

I think that whatever model you choose, the most important thing is to have a model and to ensure that all of your developers are able to stick to that model.

Odd
A: 

Following what Odd said, I am trying out a version of the MVP called Model Presentation which is working well for me so far. I am still getting an understanding of it and adapting it to my own use but it is refreshing from the code I used to write.

Check it out here: Presentation Model

Greg