Moving from Node.js to .NET Core

Moving from Node.js to .NET Core
In this article, we’ll talk about going from Node.js to .NET Core, by exploring some key differences we noticed as we ported some targeted REST API endpoints.

Here on Visual Studio App Center, our platform is built as a set of microservices, which has afforded teams to make language and platform choices that work best for them, and ultimately allowed us to move and iterate quickly. Over time, two distinct stacks have emerged: 

As we have continued to merge our teams and do more and more cross-team work, it’s become apparent that we should settle on a single choice and unify so that jumping between services is less jarring. The fact that a lot of our customers run non-Windows platforms, and our team runs a mix of Mac, PC, and Linux, led us to explore a new direction. 

Enter .NET Core 

We started investigating .NET Core as a solution because of the cross-platform abilities it offered, and the fact that it works great with VS Code, our cross-platform development solution. We felt like .NET Core allowed us to preserve this flexibility, but not continue diverging our code base, with some new services written in TypeScript, and others in C# on the .NET Framework. This allows us more flexibility when moving work between teams as well. We could have also gone with TypeScript but given the skill set of the majority of the team (C#) and the pain we have seen managing Node.js dependencies, we decided on .NET Core. We’ll discuss the dependency issue in more detail later in this post. For more information on .NET Core, see the docs

Our goal with this post isn’t to convince you that .NET Core is better than Node.js: we made our decision primarily based on the skill sets of our team. Instead, we want to share the things that we noticed as we have been starting the porting work, in the hope that if you’re embarking on a similar journey, this information will be helpful. If you’re curious about direct comparisons regarding performance, this site is a good resource that is constantly updated. The .NET Core entry on that list is called aspcore. Another interesting post explores saturating 10GbE at 7+ million request/s using .NET Core.  

Also, note that for the purposes of this post we aren’t looking at specific TypeScript features, and on our team, we continue to use TypeScript for new front-end development as there are many benefits to having concrete types on top of JavaScript. 

The Challenge 

We are not embarking on a re-write of all services, but aside from having all new services run on .NET Core targeting AKS, we are looking for opportunities to port over problematic code from either the .NET Framework or Node.js. In this blog post we’ll talk about going from Node.js to .NET Core, by exploring some key differences we noticed as we ported some targeted REST API endpoints. For our take-aways, we are mainly focused on the developer experience, and not necessarily on performance benchmarks or other orthogonal concerns. 


Key Take-Aways We Noticed 

Porting code into a strongly typed language takes time 

In our existing TypeScript code base, we take advantage of dynamic types frequently, and in the C# version of the code we had to define more concrete types. Additionally, the code that we were porting specifically dealt with bit masks, so translating those concepts between JavaScript and C# presented some pain for us. One of the main things we lost was easy parsing of JSON and dynamic object graphs. .NET Core includes JSON parsing out of the box, but it isn’t quite as native as what JavaScript provides. 

Another thing that came up was the need to create more mapping types instead of relying on dynamic mapping, which we didn’t have to handle as much since we were already in TypeScript, but if you are coming from a straight JavaScript project this is something else you will want to consider. 

All in all, if you embark on this journey, be prepared to invest the appropriate amount of time on model classes and other scaffolding that you will need for a successful transition. 

String comparison differences 

In JavaScript, when doing case insensitive string comparisons, we tend towards doing  toLowerCase() prior to doing a ===, where we don’t care about case. Generally speaking, the equivalent C# code uses string.Equals  and passes in a StringComparison  enum value to indicate how to handle issues of case sensitivity. A common case is scenarios where you do not care about case and locale, and for those you should use StringComparison.OrdinalIgnoreCase. Here are some useful references for other scenarios that may arise: 

A concrete example of porting where both examples return success under the same conditions: 

JavaScript:

var desiredValue = 'someValue';
 
if (envVar && envVar.toLowerCase() === desiredValue.toLowerCase()) {
 
  console.log('Success!');
 
 
}

Equivalent C#:

var desiredValue = "someValue";
 
if (string.Equals(envVar, desiredValue, StringComparison.OrdinalIgnoreCase)) {
 
  Console.WriteLine("Success");
 
}

Fewer dependencies in .NET Core 

One thing that has been nice is to manage fewer dependencies (and a shallower dependency tree) in the .NET Core version of the code, and hopefully this means it will be easier to maintain. We have struggled with keeping dependencies up to date and managing many dependencies in our Node.js projects. Additionally, developers on our team prefer NuGet’s policy of “lowest matching version” rather than npm’s encouragement to use “highest matching patch” or “highest matching minor version” (using the ~syntax in package.json by default via npm i). We recognize this is a philosophical difference and opinions on this may vary. The pros of the NuGet approach are more control and explicitness, whereas the npm approach allows for theoretically non-breaking changes to be easily applied. 

Other Thoughts 

There isn’t one right answer, and the purpose of this post isn’t to convince you one way or another, just to shed some light on the process we have gone through recently as a team. As we discover and do more in .NET Core on AKS we will be sure to share along the way! 

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow us on Facebook | Twitter

Read Also

The Complete Node.js Developer Course (3rd Edition)

Angular & NodeJS - The MEAN Stack Guide

NodeJS - The Complete Guide (incl. MVC, REST APIs, GraphQL)

Node.js: The Complete Guide to Build RESTful APIs (2018)

10 Node Frameworks to Use in 2019

Moving from NodeJS to Go

Google’s Go Essentials For Node.js / JavaScript Developers

A to Z of Node.js Packages(unconventional)

New ECMAScript Modules in Node v12

The definitive Node.js handbook