The .NET Venn Diagram
By James Charlesworth
Published 12 February 2018
How moving your code to .NET Standard can help you migrate to dotnet core
If you work in the Microsoft stack you may have found yourself asking two questions recently.
"Is .NET Core 100% ready to replace .NET Framework?" and "How do I migrate all my existing code?"
Well the answers to these questions are "YES" and "With .NET Standard".
From Microsoft
The .NET Standard is a formal specification of .NET APIs that are intended to be available on all .NET implementations. The motivation behind the .NET Standard is establishing greater uniformity in the .NET ecosystem
So basically, .NET Standard is a set of common APIs that can be compiled and run by both the .NET Framework compiler and .NET Core. I like to think of it like a Venn diagram
The functionality on the left will have an equivalent on the right under .NET Core, though it will not be a straight swap. The code in the middle will be a straight swap.
Migrating a .NET Framework WebApi
So let's say you have an old skool .NET Framework 4.7.1 web application you want to migrate to .NET Core. your project might look something like this
The first thing you should do, if you haven't already, is take out any code that is not WebApi-specific and move them into their own projects. This means POCO's, Service classes, Factories, your DAL, anything that does not directly need to reference the HTTP context.
So let's move everything other than the Controller and Startup.cs
code into its own class library. When creating the class libraries I'm going to select .NET Standard
And we can move our non-http-specific code to these new .NET Standard class libraries
Now it may look like all we've done is move some classes to another project, but in fact by moving them to .NET Standard we have actually moved them into the central part of our venn diagram
We can't move everything to .NET Standard, we still have to leave our API controller and Startup.cs
files in the left hand side of the diagram. This code will not compile if we try to move it to .NET Framework because it relies on base classes and things that don't exist in .NET Standard. That's fine though, we have moved all of the important code, the API boilerplate can be re-written in .NET Core. Add a new .NET WebApi project to the solution
Since this new project is .NET Core it cannot reference the Example.Api
project, but it can reference the other class libraries, since they are in .NET standard. You can now re-write the API layer in .NET Core and seamlessly re-use all your class libraries, which should have been the "meat" of your API in the first place anyway.
Conclusion
It is worth noting that this solution has only been possible since .NET Standard 2 and .NET Framework 4.7.1. Prior to that, mixing all three of these project types in one solution in Visual Studio was not possible, it couldn't build them all with MsBuild and generally threw a hissy fit. All of these issues have now been resolved by Microsoft - so you have no excuses to not use .NET Standard wherever possible!
Hi, I'm James
I've worked in software development for nearly 20 years, from junior engineer to Director of Engineering. I'm here to help you get your foot on the next rung of your career ladder. I post weekly videos on YouTube and publish guides, articles, and guided project tutorials on this site. Sign up to my newsletter too for monthly career tips and insights in the world of software development.
Related Project Tutorials
Read Next...
What is JavaScript Strict Mode and Why You Should Use It?
3 December 2023 • 3 min read
Explore the benefits of JavaScripts strict mode in enhancing code reliability and preventing common mistakes. This article delves into its features, real-world examples, and reasons why both beginners and pros should consider its adoption.
How to Run TypeScript in VS Code
30 November 2023 • 3 min read
Learn how to set up, run, and debug TypeScript in Visual Studio Code. This guide provides step-by-step instructions to enhance your JavaScript development process. Dive into the seamless integration of TypeScript with VSCode for a productive coding session.
First Class Functions in Javascript
17 August 2023 • 3 min read
This article introduces you to first class functions, and how functions can be treated as first class citizens in programming, demonstrating their utility and practical applications in Javascript.