Preventing breaking changes in .NET class libraries

Have you ever felt frustrated when updating a NuGet package, only to have your build fail because the new version of the package introduced a breaking change? Or perhaps you’re the author of a NuGet package and you’re determined to avoid introducing breaking changes? Ever wonder how Microsoft maintains backwards compatibility in ASP.NET Core for years? There’s of course a lot of design involved, but one tool they use is their Microsoft.CodeAnalysis.PublicApiAnalyzers NuGet package. As the name suggests, it’s a set of Roslyn analyzers to keep track of your public API. It’s used by the .NET team, the Azure SDK team, various other Microsoft projects, and numerous open-source libraries such as Dapper and Polly.

In this post, I will guide you on designing .NET class libraries to prevent breaking changes and demonstrate how to leverage the Microsoft.CodeAnalysis.PublicApiAnalyzers package to enforce these principles.

Continue reading “Preventing breaking changes in .NET class libraries”

The best C# REPL is in your terminal

Read-Eval-Print-Loop (REPL) is an interactive program that reads your input, evaluates it, prints the result, and loops back to the beginning. It’s a great way to experiment with a programming language and an excellent method for learning a new language. C# has many REPLs; some are web-based, others are desktop applications, and some are command-line tools. There’s .NET FiddleTry .NET, the popular LINQPad, the built-in csi.exe, and many more.

But there’s one C# REPL that I find above all others. It runs in your terminal, is easy to install, and easy to use. The included Intellisense, documentation and suggested overloads increases productivity. It supports theming, and when I use this REPL, I almost feel like I’m coding in a regular IDE. I always have a terminal open, so it’s very convenient to have such an advanced C# REPL in it, available in a few keystrokes.

The C# REPL I’m talking about is simply called… C# REPL. It’s an open-source project created by Will Fuqua, and as of today, it has over 2k GitHub stars. It is distributed as a .NET tool and is cross-platform. In this blog post, I’m going to show you how to install it on Windows Terminal, but you can install it on any terminal emulator you prefer.

Continue reading “The best C# REPL is in your terminal”

Convert complex YAML to .NET types with custom YamlDotNet type converters

When it comes to YAML serialization and deserialization in .NET, YamlDotNet is a go-to library with over 100 million downloads on NuGet. It is also integrated into various projects by Microsoft and the .NET team, despite the absence of an official Microsoft YAML library for .NET.

In this blog post, we will explore the process of creating custom YAML serializers and deserializers using YamlDotNet. To illustrate these concepts, we’ll examine the specific use case of partially parsing the environment variables section of a Docker Compose file.

Continue reading “Convert complex YAML to .NET types with custom YamlDotNet type converters”

Instrumenting System.CommandLine-based .NET applications

In our previous posts, we’ve learned how to build beautiful command-line applications using System.CommandLine and modern dependency injection. There might be situations where you want to monitor how your application is used. The specifics of what telemetry data to gather will largely depend on your application’s nature. Remember, it’s not appropriate to collect personally identifiable information (PII) in publicly available apps, although it might be acceptable if you’re creating an internal tool for your company.

This blog post will guide you on how to – one more time – build on top of System.CommandLine to track usage, understand your application’s behavior, and gather valuable data. This might include the duration of the command execution, user information, command arguments, and more.

Continue reading “Instrumenting System.CommandLine-based .NET applications”

Crafting beautiful interactive console apps with System.CommandLine and Spectre.Console

In our last talk about System.CommandLine, we learned how to easily build command line apps using .NET and C#, following most of the advice from CLI guidelines. We also brought in dependency injection to make our code more flexible, easier to read, and simpler to test.

Now that we have our basic setup ready, it’s time to improve our user experience. So far, users have been interacting with our app by typing every single argument each time they invoke the app. In this next part of my System.CommandLine journey, we’re going to make our CLI apps beautiful but also interactive, thanks to the Spectre.Console .NET library.

Continue reading “Crafting beautiful interactive console apps with System.CommandLine and Spectre.Console”