Untitled 1
10 Things to Keep in Mind while Developing a New ASP.NET Web Forms
Application
Many developers are opting for ASP.NET MVC for their new web applications.
However, this may not be always possible (reasons can be many and valid in a
given context) and you might be required to use Web Forms for your new projects.
In such cases novice developers have this doubt - "If I develop my project using
Web Forms and later want to migrate to MVC, how difficult this task would be?".
There is no single answer to this question. However, if you follow certain
guidelines while developing a Web Forms project today, at later stage migrating
to MVC would be less painful than otherwise. Here I have listed my top 10
recommendations in dealing with such a situation.
1. Use Class Libraries wherever possible
If you observe the evolution of .NET framework over the years you will
realize that Class Libraries are best for writing code that is independent if
any particular type of UI. The reason is simple - they contain mainly POCOs. If
you isolate as much code as possible in class libraries then reusing then during
and after the migration would be quite straightforward. Of course, you should
design these class libraries in a structured and neat fashion rather than a
"dumping ground" for classes. For example, if you isolate your data access and
business logic in a separate class library projects you can use them as-is in
MVC applications also.
2. Don't use server control level UI properties
ASP.NET Web Forms make UI development quite easy. Just drag-n-drop controls,
set their properties and your UI is ready! One common mistake beginners make is
to set UI related properties of the server controls. For example, BackColor,
ForeColor, Font-Name and so on. These properties are converted into style
attribute of the respective control tag. A recommended approach is to place all
such styling information in CSS style sheets and then use ClassName property of
the server controls to attach a CSS class. This way your UI uses a standard way
of styling and after migration you can reuse the same CSS quickly.
3. Use jQuery Ajax over Web Forms specific techniques
Web Forms offer AJAX Extensions to help you develop Ajax enabled
applications. However, these controls are specific to Web Forms. Instead of
relying on them you may consider using standard Ajax techniques such as jQuery
$..ajax() or XMLHttpRequest object. This way your Ajax code will be quite
portable to any other web development framework including MVC.
4. Make use of Web API
Web API was introduced along with MVC but now it's the part of the ASP.NET
framework and you can use Web API in Web Forms applications too. For example,
instead of writing Ajax code that calls a Page Method or ASMX Web Service, it is
recommended to create a Web API and call it from your client side code. Your Web
API controllers will be 100% reusable in MVC applications.
5. Avoid Web Forms specific features
Web Forms use several features that are specific to Web Forms. These features
are not available in MVC. Features such as Themes and Web Parts fall in this
category. Avoid such features for new projects because it will be difficult to
migrate them to MVC projects due to the lack of equivalent feature set.
6. Design forms so as to avoid ViewState as much as possible
One big reservation against Web Forms is ViewState. Many Web Forms developers
design their data entry pages in such a way that multiple tasks (Insert, Update
and Delete) happen on a single page. This may require page ViewState enabled
resulting in bulky forms. You should see if these tasks can be separated into
their own pages in an attempt to avoid ViewState altogether. For example, say
you have a GridView at the top of a page. Selecting a row in the grid displays
its content for editing at the bottom half of the page. That means selecting and
editing are happing in one page itself and ViewState might be necessary.
Instead, think if the grid can have hyperlinks (instead of Select postback
buttons) that points to another web form where data editing takes place. In this
case you can turn the ViewState off for the page housing the grid since there is
no postback and editing is happening on a different page.
7. Create reusable UI through User Controls
Web Forms developers have two choices for creating reusable user interfaces -
Web User Controls and Custom Server Controls. If you create Custom Server
controls you can provide a rich design time interface (in terms of toolbox
appearance, smart tags, designers etc.) however this control won't be readily
reusable in MVC. If you wrap your reusable UI as a Web User Controls your life
will be easy. This is because you have raw HTML and code with you and MVC does
offer a close equivalent in the form of partial pages.
8. Use standard HTML over server controls wherever possible
Rich server controls is one of the biggest strength of Web Forms
applications. Controls such as GridView and ListView are quite popular. However,
using these controls make your migration path difficult because there is no
direct equivalent in MVC. Although you may not avoid using them altogether try
to minimize their use whenever possible. For example, if you don't need to
access Label controls programmatically, don't use them. Use standard <span> or <lable>
tags. This way you can reuse that much markup readily in MVC.
9. Think in terms of MVC even for Web Forms applications
Although Web Forms are not designed keeping MVC design pattern in mind,
nothing stops you from thinking in MVC terms. The separation of concern (SoC) as
enforced by MVC can be done in Web Forms also. When you develop the Web Form try
thinking in terms of its Model, Controller Actions and View.
10. Learn and use Design Patterns and SOLID principles
Many Web Forms developers are accustomed to drag-n-drop development. Their
applications are UI centric. Although these application may meet the
requirements at a given point of time, they become difficult to maintain and
extend. This happens because a lot of code is stuffed directly into Web Forms.
Take some pain to learn SOLID design principles and commonly used Design
Patterns so that you can structure your Web Forms applications in a better way.
At the end you will realize that the efforts are worth the learning efforts you
put.