.NET everywhere! In this fun session, we'll explain exactly what .NET Standard is, then show how .NET Standard allows us to write the one .NET class and use it in the full .NET Framework, .NET Core, Xamarin, UWP, and more.
Note: You can jump to the complete state for demo 1 and 2 by switching to the
result
branch.
- Clone https://github.com/jongalloway/netstandard-dataset-demo
- Run the application and show that it's using an XML backed DataSet in a Windows Forms application. This application shows all Northwind employees that are now at retirement age.
- Right-click the solution and add a .NET Standard Class Library to the solution.
- Delete
Class1.cs
from the new .NET Standard project. - Drag the
NorthwindDb.cs
class and thenorthwind.xml
file from the original project into the new .NET Standard project. - Add a project reference from the original project to the .NET Standard project.
- Delete the
NorthwindDb.cs
class and thenorthwind.xml
file from the original project. - Run the application and verify it still works.
- Add a reference to the
NQuery
package from the .NET Standard library. - Modify the
GetData()
method to use theNQuery
library as shown below:public static string GetData() { var result = ""; var dataSet = new DataSet(); dataSet.ReadXml(@"C:\demos\northwind.xml"); var dataContext = new DataContext(); dataContext.AddTablesAndRelations(dataSet); var sql = @" SELECT e.FirstName + ' ' + e.LastName FROM Employees e WHERE e.Birthdate.AddYears(65) < GETDATE() "; var query = new Query(sql, dataContext); var results = query.ExecuteDataTable(); var values = results.Rows.Cast<DataRow>().Select(r => (string)r[0]); result = string.Join(Environment.NewLine, values); return result; }
- Run the application and demonstrate that the reference to the NuGet package is working due to the .NET Standard compatability shim.
- Clone the https://github.com/terrajobst/netstandard-gps-demo repository.
- Note that there are two different applications (Windows Forms and UWP) backed by two different GPS libraries that do essentially the same thing. These libraries don't share code because the different platforms have different geolocation APIs.
- Run the Windows Forms application and show that it's using the geolocation API.
Note: If you're going to run the UWP application, you'll need to publish it locally first.
- Switch to the
result
branch for this repository (either by toggling in Visual Studio or usinggit checkout result
from the command line). - Note that these two applications are now sharing one .NET Standard library using C# preprocessor directives.
- Explain that this library can also now add an
IsSupported()
method which uses the same preprocessor logic to return true or false depending on whether GPS support is available. This allows you to share the .NET Standard library to any platform, and clients can check if geolocation is supported before making the library call. - In the project properties, turn on NuGet package generation support. Build the application and show that just one NuGet package is created. Previously, you'd need to write your own script to build a single NuGet targeting multiple platforms.
Note: You can see these demos in this .NET Standard Deep Dive video on Channel 9.