The .NET Venn Diagram

The .NET Venn Diagram

James Charlesworth

By James Charlesworth

12 February 20183 min read

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

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

Example Solution 1

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

New .NET Standard Class Library

And we can move our non-http-specific code to these new .NET Standard class libraries

Example Solution 2

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

Venn Diagram Arrow

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

New .NET Core API Project

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.

Example Solution 3

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!