Skip to main content

Best Practices for Dotnet Framework

1. MAKE A PLAN BEFORE YOU START WRITING YOUR CODE
OOP is a great way to compartmentalize software code, but you can get lost in the details if you don’t have a good plan. It’s a good idea to use a tool such as Visio to plan out each class and its properties and methods, for example, before you start coding. Planning ahead ensures the organization of your code is a success before you even write the first line of code.
When you’re planning ahead, identify each component of your application and plan methods and properties that would make up a specific class. If you have a customer class, identify what information you need to store for a customer. You might need the customer’s address, name, phone number, and gender. These would be properties of the customer class. Planning helps you identify the components needed for your software, so you won’t forget them and have to go back and refactor your code.

2. DON’T FORGET THE DATABASE

Every dynamic web application needs a database. Normally, .NET developers work with Microsoft SQL Server because it plugs into .NET code so easily. You can, however, use another solution such as MySQL or Oracle and several of the NoSQL solutions on the web.
Just like planning your classes, you need to plan your database layout. Because a poor database design can cripple the application, the database layout is almost more important than planning your code . You can use Visio or another third-party solution to plan out your database design. When you plan your design, don’t forget basic database rules such as normalization, redundancy, and backup plans.

3. KEEP VARIABLE NAMING SCHEMES CONSISTENT

If you’re writing code for a client, they might have documentation that defines the way you name your variables. If you code for your own projects, you should keep the naming schemes consistent.
Some basic standard rules across development teams include:
  • Using camelCase for method variables.
  • Prefixing private variables with the _ character such as _myVariable.
  • Always using verbs for methods such as “GetCustomerInformation” or “SendEmail.”
  • Using standard case for class property variables such as “FirstName” or “LastName.”
These are some standard rules across the development community, but if you’re working on a project for a client, always ask for their coding standards documentation.

4. REPLACE LONG “IF” STATEMENTS WITH A FUNCTION OR METHOD

You can use if statements, but when you start nesting them or using several “elseif” statements, it’s time to turn the if block into a function. Here’s an example:
if (x > y)
{
// do something
}
elseif (x < y)
{
// do something else
}
elseif (x == y)
{
// do another something else
}
You can keep going with if statements with several elseif statements (or even start nesting them), but this can make the code harder to read, and the logic harder to understand. It also increases the chance of a logic error in your code. To avoid this, break out these statements into a function or method within the class. It organizes your code and makes it easier for a developer other than you to understand.

5. SEPARATE YOUR LAYERS

Good coding requires layers. At the very least, you should have a presentation and a data layer, but most software design calls for a presentation layer, a business logic layer, and a data layer. The presentation layer is what the customer sees, the business logic is the layer specific to the nuances of the business and its organizational rules, and the data layer is for database methods.
The reason for layering is that you can completely overhaul one layer without affecting another. For instance, the data layer is made up of all database calls and connections. If you decide to switch database providers, you can change the data layer without affecting the presentation and business logic layers. This type of design is enterprise level, so it can be difficult for new coders to grasp until they see it in practice.
There are several third-party libraries in NuGet to help you get started with application layering. Do a search for “dependency injection” and you’ll find several frameworks to get you started. If you work with MVC, then your layers are somewhat automatically separated (with the Controller owning the business logic and the View containing the presentation layer). You still must use dependency injection for data layers.

6. WRITE ERRORS TO AN ERROR LOG

When you have a large application that runs on a server, you need a logging solution. This solution allows you to identify critical errors in the application sometimes before users do. Normally, users will report errors and it’s up to you to find the underlying cause. It can also be extremely tedious and time-consuming to reproduce a user error.
Luckily, .NET has an internal logging system that lets you write errors to the Windows Event Viewer. You and the system administrator can search for errors thrown by the application on the server, so you can see the exact error without trying to reproduce it in your development environment.
You can create your own separate application log in .NET, too, so all of your application’s errors are logged to a specific category in Event Viewer. This separates your custom software errors from other applications.

CONCLUSION

.NET is the de facto framework for Windows development, but it can be difficult for a new developer to learn the ins and outs. Just remember to plan your code so that you don’t wind up refactoring soon after you’re finished. Make your code scalable by creating a plan first, and then code using common standards. These standards make it easy for another developer to read your code and add to it in the future.

Comments

Popular posts from this blog

Claims Class For security in .NET4.5 with C#

What is Claim ? A claim in the world of authentication and authorization can be defined as a statement about an entity, typically a user. A claim can be very fine grained: Ram is an admin Ram’s email address is Ram@yahoo.com Ram lives in Mumbai Tom’s allowed to view sales figures between 2009 and 2012 Tom’s allowed to wipe out the universe Claims originate from a trusted entity other than the one that is being described. This means that it is not enough that Tom says that he is an administrator. If your company’s intranet runs on Windows then Tom will most likely figure in Active Directory/Email server/Sales authorisation system and it will be these systems that will hold these claims about Tom.  The idea is that if a trusted entity such as AD tells us things about Tom then we believe them a lot more than if Tom himself comes with the same claims . This external trusted entity is called an  Issuer . The KEY in the key-value pairs is called a  Typ...

How to do auto logout and redirect to login page when session expires using asp.net?

In this article I will explain you how to auto logout and redirect to  login page when users session expires within specific period. Introduction In every applications, we need to maintain the session expiration time  say for example 1hr or 30 minuets and this configuration item we  can do with web.config file or in IIS. But here, application should  redirect to login page automatically when session expires. Configure session time First configure the session timeout value in web.config file as like  below, here I’m configuring the session timeout value as 3 minutes  for this sample . < system.web > < sessionState mode = " InProc " timeout = " 3 " ></ sessionState > </ system.web >   Create Pagebase class create a custom pagebase class and write the common functionality codes into this class. Through this class, we can share the common functions to other web pages. In this class we need...

Javascript ECMAScript6 New Feature

Extended Parameter Handling Another nice set of features introduced in ES6 is the extended parameter handling. These provide a very nice set of idioms that bring us very close to languages like C# or Java. Default parameter values and optional parameters Default parameters allow your functions to have optional arguments without needing to check arguments.length or check for undefined. Let’s take a look at what that might look like: let greet = (msg = 'hello', name = 'world') => {   console.log(msg,name); } greet(); greet('hey'); Looking at the preceding code, you can see that we are using the new fat arrow (=>) syntax as well as the new let keyword. Neither of those are necessary for this example, but I added it to give you more exposure to the new syntax. Running the preceding code produces the following result: hello world hey world  Rest parameter Handling a function with a variable number of arguments is always t...