ASP.NET Best Practices

In this article we will explore some of the best practices in ASP.NET development.

Best Practices - These are not rules, these are conventions which will be better if followed.

Before starting I would like to request the reader to share here whatever ideas and best practices he/she uses.

1. Choose pattern correctly

Choosing the correct pattern for your application development is a must.

It's not always that 3-Tier, MVC, MVP or MVVM is best; if you have a fixed small size system which is not going to be extended in the future then 1-Tier/2-Tier will be good.

I prefer at least 2-Tier.

Always make a proper planning and understanding of the system before making any decisions.

2. DataTier

Always follow some guidelines for SQL queries.

  • Alias names like a,b,emp make queries unreadable and query management becomes very difficult.
  • Use alias names for tables only when self join is involved, but alias has to be meaningful and proper.

    Example: TblEmployee Manager or TblEmployee Developer.
  • If more than a single developer will work on a project then always put a description at the top of every procedure along with the creator name.
  • If in a query, in a where condition if a column with a primary key/unique key is involved, there is no need to put other conditions.

    Example:

    Select * from TblEmployee where EmploeyeeId=@EmployeeId
    and AccountId=@AccountId

3. Application Pool

Create a new application pool for your site.

4. App_Offline.htm file

Many people are unaware of this file. When you add a htm file name such as app_offline, every ASP.NET request will be redirected to this page.
Use this page when you want to take your website offline temporarily, instead of writing your own logic for it.

5. Validation

Use ASP.NET validation controls for validation because it supports both client side and server side validation.

6. Page.IsValid

Always check for this property value in Control events like Button_click when validation is being done using Validation Controls because it's easy to bypass JavaScript code.

7. ControlState

While developing user controls if maintaining state is a must then use control states instead of viewstate.

8. Naming Convention

Always follow a consistent Naming Guidelines and standards, which should include everything, from how to declare a variable to what the structure of a stored procedure will be.

9. Foreach loop

Using a foreach loop instead of a "for" loop makes the code less cluttered.
Use a "for" loop only when an index is required.

10. Reference Type as Return Types

Try to understand the exact difference and usage of value and reference types.

Take a look at the following examples:

Public int UpdateSalary(int Salary)

{

return Salary+1000;
}
.

.

.

Employee employee=//Get the Employee object

employee.Salary= UpdateSalary(employee.Salary);

 

Public Employee UpdateSalary(Employee e)

{

e.salary+=1000;

return e;
}

.

.

Employee newEmployee=UpdateEmployee(oldEmployee);

Example 1 is perfectly fine but in example 2 oldEmployee and newEmployee refers to the same Employee. So it doesn't make any sense when a function returns a reference type which came to it as a parameter.

11. Namespace

Always do a clean coding.

Include only necessary namespaces

(In C# just right-click in Visual Studio, go to Organize Usings and click "Remove Unused Usings".)

12. Abstraction and Encapsulation

OOP concepts are the basis for every systematic development.

  • Encapsulate all similar things into a single group.
  • Encapsulation doesn't end at any level, create a function to encapsulate logic, create a class to encapsulate functions performing similar activities, like all the functions performing tasks related to a customer will be placed inside one class, all similar classes in a single namespace; next is assembly and then final product.
  • Abstract things; in other words always create functions in such a way that one knows what the other does, but doesn't know how.

Maintain the standard of one function should contain one logic (purpose).

13. CSS and JS

Never use inline or internal CSS, go with an external.

The same applies to JS.

(When we include external css/js in the page it is cached and so wont download on every request. Two birds in a single shot Performance and Management.)

14. Master Pages

Always use Master pages for consistent layout instead of frames and iframes.

15. Session

Always encapsulate session values as discussed in the following link:

http://www.c-sharpcorner.com/UploadFile/SukeshMarla/Asp-Net-sessions-and-oops

16. ASP.NET Life cycle

Every ASP.NET developer must properly understand the various events exposed during the page life cycle. Each event has it s own significance and use; see:

http://www.c-sharpcorner.com/UploadFile/SukeshMarla/Asp-Net-life-cycle/


Hope you enjoyed reading this article,

I want you guys to share some more tips and thoughts related to same.

If we share then only we can learn, I really appreciate to see some more tips here.

Click here  to read Best Practices for ASP.NET.