Tips for preventing a large ball of dirt with ASP.NET WebForms

Despite the fact that these days ASP.NET MVC has all the ads, WebForms are still pretty common. How do you support your project? Here are some tips.

+7
webforms
source share
6 answers
  • Create web user controls for everything that appears on multiple pages that are not part of content like the home page. Example. If your application displays product information on 10 pages, it is best to have a user control that is used on 10 pages, rather than shortening the display code 10 times.
  • Put as little business logic as possible in the code behind. The code should be postponed to the level of your business in order to perform work that is not directly related to placing things on the page and sending data from the business level.
  • Do not reinvent the wheel. The many messy codes I've seen are made up of code that does what the infrastructure already provides.
  • In general, avoid script blocks in html.
  • One page does not need to do too much. What I saw again and again is a page that says it adds and edits modes. It's great. However, if you have many additional modes for adding and editing, you are better off having multiple pages for each auxiliary mode with reuse through user controls. You really need to avoid grouping nested IFs to determine what your user is trying to do, and then show the right things depending on this. Things get out of hand quickly if your page has many possible states.
  • Learn / Drop the page life cycle and take advantage of it. Many of the ugly pages of code I've seen can be cleaner if the coder has a better understanding of the page life cycle.
+2
source share

I usually try to stay away from it ... but when I use WebForms, I follow these guidelines:

  • Save the resulting HTML : just because you don’t manually encode a <div> does not mean that the generated code should become an unreadable nightmare. Avoiding controls that produce ugly code can pay off in shorter debugging times later, making problems easier to view.
  • Collapse external dependencies : you are not paid to debug other people's code. If you prefer to rely on third-party components, then get a source so you don't have to spend an unusually large amount of time fixing bugs.
  • Avoid doing too much on one page . If you find that you are implementing complex β€œmodes” for a given page, consider splitting it into several single-mode pages, perhaps using master pages to discard general aspects.
  • Avoid postback . It has always been a terrible idea, and it has not become less terrible. Headaches that you save by not using controls that depend on postback are a good bonus.
  • Avoid VIEWSTATE : see comments for # 4.
+3
source share

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

o Model View Presenter ( although it is now a Supervisor controller and passive view ). This is a solid model that fosters the separation between your user interface and business model that all your developers can follow without any problems. The resulting code is much more verifiable and maintainable. The problem is that it does not apply, and you need to write a lot of supporting code to implement the model.

o ASP.NET MVC The problem with this is that it is in preview. I spoke with Tatham Oddi and mentioned that it is very stable and usable. I like it, it provides separation of problems and does it with minimal additional code for the developer.

I think that no matter what model you choose, the most important thing is to have a model and ensure that all your developers can stick to this model.

+3
source share

Start with the master pages on day # 1 - his pain returns to modernization.

+1
source share

After what Odd said, I am testing a version of MVP called "Presentation Model" that still works well for me. I still understand it and adapt it for my own use, but it is updated from the code that I used for writing.

Look here: Presentation Model

0
source share

Use version control and folder structure to prevent too many files from all in one folder. There is nothing more painful than waiting for Windows Explorer to download something, because there are more than 1000 files in the folder, and when the folder loads, they load. Agreement on variable names and methods is also useful, if possible, in advance, so that there isn’t such a different type of code in which different developers all put their own unique touches, and this painfully shows.

Using design patterns can be useful in organizing code and scaling it nicely, for example. a strategy template can lead to a simpler time when you need to add a new type of product or device that needs to be supported. Similarly for using some adapters or facade templates.

Finally, know which standards your forms will support: is it only for IE users, or if any of IE, Firefox or Safari easily loads the form and looks good?

-one
source share

All Articles