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...

Design Patterns

                                                      Design pattern 1. Factory Design Pattern  In Factory pattern, we create object without exposing the creation logic to the client and refer to newly created object using a common interface. Step 1 Create an interface IShape.cs public interface I Shape { void draw (); } Step 2 Create concrete classes implementing the same interface. Rectangle.cs public class Rectangle : I Shape { @Override public void draw () { C onsole.WriteLine ( "Inside Rectangle::draw() method." ); } } Square.cs public class Square : I Shape { @Override public void draw () { C onsole.WriteLine ( "Inside Square::draw() method." ); } } Circle.cs public class Circle : I Shape { @Override public void draw () ...

Paytm integration

How to integrate Paytm to asp.net site   You will need to create an Checksum on your server. Kindly refer the dll file for the attachment.  Usage of CheckSum API: ·         Add provided “paytm.dll” as a “Reference” in your project. ·         Import namespace “paytm” in your Class with statement “using paytm”. ·         Now Generate CheckSum API as well as Verify CheckSum API are available as follows: o   String CheckSum.generateCheckSum(String masterKey, Dictionary<String, String> parameters) o   Boolean CheckSum.verifyCheckSum(String masterKey, Dictionary<String, String> parameters, StringcheckSum) ·         For Generating CheckSum, use following snippet code: String masterKey = “merchantKey” ; Dictionary<String, String> parameters = new Dictionary<string, string>(); p...