top of page

.NET Core Best Practices

.NET Core is a free, open-source, general-purpose development platform maintained by Microsoft. It is a cross-platform framework that runs on Windows, macOS, and Linux operating systems.



.NET Core Framework can be used to build different types of applications such as mobile, desktop, web, cloud, IoT, machine learning, microservices, game, etc.


.NET Core is written from scratch to make it modular, lightweight, fast, and cross-platform Framework. It includes the core features that are required to run a basic .NET Core app. Other features are provided as NuGet packages, which you can add it in your application as needed. In this way, the .NET Core application speed up the performance, reduce the memory footprint and becomes easy to maintain.


Here are some of the best .NET Core practices to help developers translate their client's business logic into reality:


1. Inline methods

By passing arguments, reducing jumps, and restoring registers, inline methods improve app performance. Remember that the JIT (just-in-time) compiler will not inline one method that contains a throw statement. To fix it, create a static helper process that includes a throw statement.


2. Use Asynchronous Programming : (ASYNC — AWAIT)

Asp.Net Core uses the same Asynchronous programming approach to make an application more dependable, faster, and interactive. End-to-end asynchronous programming should be used in our code.


3. Optimize Data Access

To boost the application’s performance by optimizing its data access logic. Most applications are completely reliant on a database, and they must retrieve data from it, process it, and display it.


Suggestions:

  • Call all data access through the APIs asynchronously.

  • Do not get data that is not required in advance.

  • When retrieving data for read-only reasons in Entity Framework Core, use non-tracking queries.

  • Try to use aggregate and filter LINQ queries like with Where, Select, or Sum statement, so that filter thing can be performed by the database.


4. Always Use Cache

Caching is a popular and well-proven method of improving performance. Any data that is relatively stable should be cached. Response caching middleware support is provided by ASP.NET Core, which we can use to enforce response caching. Response caching can be used to improve output caching, and it can cache web server responses by adding cache-related headers to HTTP response objects. Caching large objects also saves money on allocations.


Caching technique:

  • In-memory caching

  • Distributed cache

  • Cache tag helper

  • Distributed cache tag helper

A memory cache can be used or a distributed cache like NCache or Redis Cache can be used.


5. Response Caching Middleware Components

If the response data is cacheable, this response caching middleware monitors, stores, and serves the responses from the response cache. Microsoft has access to this middleware. AspNetCore. Package ResponseCaching.


6. Enable Compression

We can improve application performance by reducing response size because it transfers less data between the server and client. You can take advantage of response compression in ASP.NET Core to reduce bandwidth requirements and response time. It serves as a sure-shot middleware component in ASP.NET Core.


7. Bundling and Minification

We can reduce the number of server trips by doing so. Try to upload all client-side assets, such as styles and JS/CSS, at once. You can use minification to first minify your files before bundling them into a single file that loads faster and reduces the number of HTTP requests.


8. Use Content Delivery Network (CDN)

Despite the fact that the speed of light exceeds 299000 km/s, which is extremely fast, it also aids us in keeping our data close to our customers. It is simple to load on the server if there are only numbered CSS and JS files. Consider using a CDN for larger static files. Most CDNs have multiple locations and serve files from a local server. Loading files from a local server can improve website performance.


9. Load JavaScript from the Bottom

We should always try to load our JS files at the end, unless they are required earlier. As a result, your website will load faster, and users will not have to wait as long to see the information.


10. Cache Pages or Cache Parts of Pages

Instead of querying the database and re-rendering a complex page, we could save it to a cache and use that data to serve subsequent requests.


11. Use Exceptions only When Necessary

Exceptions should be uncommon. In comparison to other code flow patterns, the catch and throw of exceptions is slow. Exceptions are not used to control the program’s flow. Consider the program’s logic when identifying and resolving exception-prone scenarios. Exceptions should be thrown or caught for unusual or unexpected circumstances. App diagnostic tools, such as Application Insights, can be used to identify common exceptions in an app and how they perform.


12. Setting at Environment Level

We must use the development environment when developing our application, and the production environment when publishing it. The configuration for each environment is different with this, and it is always the best practice. When we use .NET Core, it is extremely simple. Our project folder contains the appsettings.json file. The appsettings are visible. Development.json is a file that contains the development environment and appsettings. If we extend it, we’ll need a Production.json file for the production environment.


13. Routing

We can provide detailed names, and we should use NOUNS instead of VERBS for the routes/endpoints.


14. Use AutoMapper to Avoid Writing Boilerplate Code

AutoMapper is a convention-based object-to-object mapper with minimal configuration requirements. Basically, when we want to separate domain models from view models. We can map domain models and view models like this after configuring AutoMapper.


15. Use Swagger

Swagger is a RESTful API representation that allows for interactive documentation, discoverability, and the generation of Client SDK support. It usually only takes a few minutes to set up a Swagger tool. We get a fantastic tool for documenting our API.


16. Logging

When we keep a consistent, fixed logging format, this is referred to as structured logging. Logs can be easily filtered, navigated, and analyzed using structured logs. Asp.Net Core includes structured logs by default, and the Asp.Net team will need to make it consistent in order to keep the entire code consistent. The application communicates with the web server. Serilog is a great logging framework that you can use for logging.


17. Do Refactoring for Auto-generated Code

In .NET Core, there are a lot of auto-generated codes, so set aside some time to examine the logic flow, and because we know our application better, we can improve it a little.


18.Delete Unused Profiles

  • Delete unused custom middleware components from startup.cs

  • Remove any default controllers you aren’t using.

  • Trace and remove all redundant comments used for testing from the views.

  • Remove the unwanted white spaces as well


Advantages of .NET Core:


1. Object Oriented

.NET is based on Object-Oriented Programming Module. Makes possible to reuse components and code, thereby saves time and cost of development.


2. Visual Studio IDE

Integrated Development Environment makes application development simpler by providing needed to write and test software.


3. Cross-Platform Design

.NET Core is a cross-platform, which means it allows the code to run on Windows, Linux and OS X. dotNET core, unlike the original .NET framework has a fully open source code which ensures that a wide engineering community can contribute to its development.


4. Flexible Deployment and Easy Maintenance

It can be installed as a part of the application you are developing as well as separately. You can also have more than one .NET Core versions running side by side on the same machine. Hence, making it easy to cover different projects and seamlessly perform a deployment task.


5. DotNet core support wide range of applications

With .NET framework, you get the opportunity to develop an application in a multitude of domains, such as gaming, mobile, IoT, AI.


6. DotNet core Enables Top App Performance

With each released version, Microsoft added a few percentages on the performance of the application. Built using .NET Core framework, Stack Overflow, the leading knowledge website for coders, manages to serve it, users, with 5.3 M page views/day and that too with just 9 servers.


7. Cost-Effective

8. Large Community

When a framework is used by many businesses and developers, it means it has such features that attract them. It also proves its stability and high level of trust from the community, makes everyone try the framework.


Disadvantages of .NET Core


1. Limited Object Relational Support

.NET works on the Object-Oriented Programming approach, which is based on objects rather than actions and data rather than logic. The support of data-oriented software application development in .NET Framework is provided by the Entity Framework.


2. Vendor Lock-in

.NET is a framework by Microsoft. While Xamarin and .NET Core are open sources, the whole ecosystem is far from being the community-driven. This means that your products will depend upon decisions made by Microsoft.


3. Memory Leaks

The problem of memory leaks is common with every technology. .NET is also one of those platforms that are frequently has criticized for memory-related and memory leak issues.

Although dotNET possesses a garbage collector for this problem, engineers are still require to invest additional efforts into resource management.


4. Difficulty in the transition to .NET core

The transition to dotNET core could be long and difficult. The applications built with older .NET versions are far more than that built with .NET core. Transiting these applications to the latest versions of .NET can be a little bumpy.



Resource: Medium.com, graffersid.com


The Tech Platform

bottom of page