Why NestJS is the Perfect Framework for ASP.NET Teams Migrating to Node.js (Or Vice-Versa)
Nest.js vs. ASP.NET: Comparing Frameworks
There are a plethora of ways you can start your web development journey. The modern web has a lot of frameworks, and it’s becoming more of a hurdle for newbies to pick which one is the best for them. Even though a web framework is made for the web itself, there are different reasons you should use them.
The type of website you’re going to build, how performant you need your site to be, the available developer pool, and the cloud provider you use will all affect the decisions you’re going to make. As a result, there is a lot to consider when choosing a framework.
What are Nest.js and ASP.NET?
Starting with Nest.js.
Nest.js
Nest.js is a Node.js web framework fully implemented using the TypeScript programming language. Compared to other web frameworks in Node.js, Nest.js emphasizes a more organized project structure of your code. Most experienced developers will recognize how Nest.js structures to code to another framework, Angular.
Nest.js utilizes TypeScript’s object-oriented programming to the fullest, compared to Express’ functional approach. But under the hood, Nest.js uses Express or Fastify (depending on your configuration) to run its HTTP servers. So you can think of it as an abstraction layer on top of another framework.
ASP.NET
ASP.NET is a web framework made by Microsoft. It uses Microsoft’s C# programming language. ASP.NET has been around for a long time, from as early as 2002. Even so, it has improved tremendously over the years and has continuously released a new version annually. As a result, ASP.NET, now more popularly called .NET, is a suite of programming tools you can use to build almost anything with C#.
The web framework is now commonly referred to as .NET Core, with .NET 6 as the latest iteration.
What are TypeScript and C#?
Microsoft created both TypeScript and C#. Microsoft created C# in response to the increasing demand amid the dot-com boom. Since languages like Java by Sun Microsystems were evolving to be one of the most popular programming languages used by web developers, Microsoft decided to enter the market by creating its language.
JavaScript was also becoming increasingly popular after the dot-com burst to this day. During the early 2010s, Microsoft engineers created a superset of JavaScript with a robust types system called TypeScript. But TypeScript took its time to mature before booming in the late 2010s, around 2016–2017.
Since the same company created both programming languages, they have inherent similarities and increasing development experience because of familiarity.
Both languages are compiled, meaning the compiler will check the errors during compile time.
Note: Compiling is only true if you use TypeScript, not JavaScript.
Object Oriented Programming in TypeScript
OOP in TypeScript is somewhat better than it is in JavaScript. However, both JavaScript and TypeScript support classes, and implementing code encapsulation in both languages seems pretty straightforward.
class Car {
color: string; getColor() {
return this.color;
} setColor(_color: string) {
this.color = _color;
}
}
The one thing TypeScript has that JavaScript doesn’t are interfaces.
interface Car {
type: string;
getType(): string;
setType(_type);
}
Implementation of the interface will look something like this:
class Audi implements Car{
type: string;
color: string; getName() {
return this.color;
} setName(_color: string) {
this.color = _color;
} getType(): string {
return this.type;
} setType(_type: any) {
this.type = _type;
}
}interface Car {
type: string;
getType(): string;
setType(_type);
}
Nest.js is the framework with one of the most proficient uses of OOP in TypeScript. Everything involves OOP concepts, such as interfaces, classes, and dependency injection.
Object Oriented Programming in C#
C# is modeled after the most popular OOP programming language of its time, Java. Until recently, coding in C# always has the same downsides as coding in Java. Having to create a main
function, complex project structures, and similar (or conflicting) class namings for providers, services, or controllers.
Creating a similar base class in C# will be something like this:
class Car
{
private string color;public void getColor()
{
return this.color;
}public void setColor(string color)
{
this.color = _color;
}
}
An interface will look something like this:
interface ICar
{
string getType();
void setType(_type);
}
With a full implementation looking like this:
class Audi : ICar
{
private string type;
private string color; public void getColor()
{
return this.color;
} public void setColor(string color)
{
this.color = _color;
} public void getType()
{
return this.type;
} public void setType(string type)
{
this.type = _type;
}
}
Speaking from experience, compared to TypeScript, you will find C# to have more rich OOP features. Interfaces scratch the bare surfaces of what C# can do. A more advanced concept will be to use reflections, but I’m getting ahead of myself here.
Nest.js vs. ASP.NET
Now you’re heading to the controversial part. Which one should you choose for your project?
Both are remarkable frameworks for you to build on, but not some of the decisions you need to consider will more likely fall upon personal preferences, among others. It’s not a popularity contest and is as situational as anything else in a business. Choosing between these two frameworks is tricky because of how similar they are.
Main Features Comparisons
Both Nest.js and ASP.NET gives you out-of-the-box features when it comes to authentication, caching, and database access. All of these are available for both frameworks to use. Nest.js has those covered using the @nestjs/passport
, @nestjs/cache-manager
, and @nestjs/typeorm
packages. While ASP.NET has authentication, caching, and database access (and more!) covered with packages like Microsoft.AspNetCore.Authentication
, Microsoft.AspNetCore.Caching.Memory
and the famous Microsoft.AspNetCore.Identity.EntityFrameworkCore
.
An article by Tevpro describes a collection of features Nest.js gives you out-of-the-box, and to provide you with a better description, you can look at the table comparing the available packages between Nest.js and ASP.NET.
While the results between each framework’s main features are close, both Nest.js and ASP.NET has huge communities behind them. For example, Nest.js can rely on the Node.js community, and ASP.NET has a large corporate backing since it’s used in almost every large corporate system worldwide.
Naturally, there are many more packages for the ASP.NET web framework side compared to Nest.js since ASP.NET has been around longer. But over time, Nest.js will benefit from the ever-growing number of JavaScript and TypeScript developers entering the space.
Similarities
It may surprise you that there are a lot of similarities between Nest.js and ASP.NET. However, since both heavily rely on OOP on a day-to-day and given how similar TypeScript and C# are as programming languages, it’s not too different when you compare an app using either framework.
A service controller for a simple Nest.js project will look something like this:
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {} @Get()
getHello(): string {
return this.appService.getHello();
}
}
The AppController
receives an AppService
via dependency injection, and the getHello
function will return a string as a response. For example, a similar controller in ASP.NET will look like this:
namespace Backend.AppController
{
[ApiController]
public class AppController
{
private readonly AppService appService;
public AppController(AppService _appService)
{
this.appService = _appService;
} [HttpGet]
public async Task<IActionResult> GetHello()
{
return Ok(this.appService.GetHello());
}
}
}
Compared to the Nest.js counterpart, it does seem more verbose to write in ASP.NET. And because both uses compiled languages, compilers can catch trivial errors such as type errors before runtime.
Benchmarking Nest.js vs. ASP.NET
Comparing speed between two frameworks is controversial and will invite a few snarks at the very least, but you should slightly consider it when choosing frameworks.
In the 2022 Tech Empower Web Benchmarks, ASP.NET Core is the 14th fastest framework.
While Nest.js, using a Fastify backend, is #239.
Please note that these benchmarks are arbitrary speed calculations of how fast a framework would respond. You don’t necessarily need a fast framework most of the time. And the most important metric is (arguably) your development speed, but that’s VERY subjective, bringing us to the next point.
When You Should Consider Nest.js over ASP.NET (Or Vice-Versa)
Taking a glance at public opinion will leave you indecisive. Comments regarding the comparison between both frameworks will usually boil down to which community you ask (or, in this case, Sub Reddit).
After reviewing Reddit, a few posts compare the two frameworks (some comparing Node.js directly with ASP.NET). The main consensus from the discussions can be summarized as, “If you want to move from Node.js over to ASP.NET, consider TypeScript first.”
Since Nest.js already uses TypeScript, this automatically solves the problem, right? Wrong. It’s not as simple as that, sure TypeScript is claimed to be better because it combines a frontend language and a backend language, making it easier for small teams to grow their product. But there are some instances where using ASP.NET will make more sense.
For example, when your cloud provider is Azure, and you don’t mind a vendor lock-in, ASP.NET is better.
But on a broader scale, there aren’t many differences between Nest.js and ASP.NET because there’s a lot of overlap in the philosophies they pursue, most evidently in their heavy use of OOP. So you will find more similarities between the two frameworks than differences on some occasions, except particularly language-specific differences, in which a lot exists.
Pragmatically you will not need to consider the essential speed, project structure, library availability, and community support. You will only need to consider the following:
- Your team’s specific skillset.
- Your product timeline.
- The cloud provider you are using.
Both are server-side rendering frameworks, making it easy for you to hook up a traditional MVC application that puts everything together in a tightly-knit monolith.
You need to be aware of common pitfalls when choosing your framework. Don’t be too dogmatic about your previous choices; times change, and some frameworks might become more adopted not because of their sheer capability but because they won a popularity contest. Don’t shoot yourself in the foot over something trivial, do better!
Conclusion
Nest.js and ASP.NET are remarkable frameworks, but their uses are usually limited to the kind of team you are building (or end up having).
There’s no reason to choose these frameworks definitively. Unless you are doing something industry-specific that requires you to use one set of languages over the other, for example, if you use Unity for game development, which inadvertently uses C#, then consider using ASP.NET for your backend well.
Choosing between Nest.js and ASP.NET is not as high-stakes as choosing between Nest.js or a high-performant Go/Rust web framework. Each has its uniqueness but overall has more overlap than differences. So don’t be too dogmatic about tech; get with the times. Happy coding!
Originally published at https://blog.logrocket.com on November 22, 2022.