Within this tutorial, I will review 10 hidden Nuget packages that you might not know about that will help improve your codebase and your productivity. If you want some tips on how to potentially level up your coding game, read on 🔥🔥🔥
1. Nodatime
The first hidden gem that we will look at today is NodaTime. NodaTime was developed by the legendary Stackoverflow answering machine, John Skeet. NodaTime has been downloaded over 85.8M so I'm not quite sure you can classify it as a hidden gem anymore, however, when you need to work with time it is a powerful tool nonetheless.
NodaTime was developed to address certain limitations found within the standard DateTime
. These improvements include:
- Improved Time zone support: NodaTime provides a more robust and flexible way to handle time zones. This is especially important when you need to repeatedly convert dates and times across different time zones.
- Immutable: NodaTime dates can be made immutable. This means it is easier to write unit tests for timestamps created with Nodatime
- Better precision: Nodatime provides more granular precision than DateTime, which can be important for certain applications.
- Improved readability: A lot of developers' feedback has been that Nodatime's syntax and naming conventions are more intuitive and easier to read compared to
DateTime
- More accuracy with ambiguous and invalid times such as during daylight saving transitions
If you want to get started with NodaTime, you can install the package within your project using this command:
After the package has been installed, you can create times and dates like this:
These code samples only scratch the surface of what you can do with this package. A complete deep dive into Nodatime would be a whole article on its own (let me know in the comments if you want to see that). For brevity we will move on, however, if you want to learn more about NodaTime, you can find more information about it on its official website here
⬇️⬇️⬇️ Download from here
2. FusionCache
Downloaded over 395 thousand times, FusionCache is a caching package that is arguably a better option to use over the more popular LazyCache. When you need a quick and easy way to start caching your C# objects you should consider installing this package. Some reasons why you might use FusionCache over the other caching packages include:
- FusionCache supports distributed caching through Redis. LazyCache, on the other hand, only supports in-memory caching.
- FusionCache supports manual and time-based cache expiration with sliding expiration. LazyCache does not provide these options.
- FusionCache is thread-safe while LazyCache has some limitations when it comes to thread safety. This means that you might need to use a separate locking mechanism to ensure thread safety
- FusionCache is actively developed. LazyCache, on the other hand, has not been updated for a few years, which may be a concern for some users
To get going with FusionCache you can use this command:
After the package is installed, you can cache your objects using this syntax:
⬇️⬇️⬇️ Download from here
3. OneOf
OneOf is a handy package to bust out whenever you need to return multiple different types from the same method or expression like a switch statement. Downloaded over 8.4 million times, OneOf gives you F# like capabilities in C#. Granted, I have never really looked at F# in anger so I don't know exactly what that means. I took that statement from the packages blurb, however, whenever I have needed to return different types from within the same method I install OneOf 😊
Before learning about this package, whenever I needed to return different types I had two options. First, I could write a bunch of interfaces and map lots of things in order to get around the limitation of only being able to return single things. Alternatively within C# 7 and above you could use Tuples. The difference with oneOf
compared to either approach is that it allows you to only return one data from a response. This ability means simplified code wherever you need to parse your service's response.
In order to get going with OneOf, you can run this command:
After installing it you can use it in anger like this:
⬇️⬇️⬇️ Download from here
4. Humanizer
Have you ever had to parse text that has been formatted using something like camel case, kebab case, or snake case? Whenever you need to convert text into a format that normal people can easily read, instead of trying to create some elaborate string replacement method, instead you can turn to Humanizer to save the day.
Using Humanizers extension methods, Humanize allows you to turn an otherwise computerized string into a more readable human-friendly one. You can install Humanizer using this command:
Once installed you can start to 'humanize' your strings using these methods:
Running this code would result in the examples being converted like this:
As you can see this is now much easier text to read, so whenever you need to do string conversion, give this a go!
⬇️⬇️⬇️ Download from here
5. GuardClauses
I've mentioned the benefits of using the guard pattern on this site for many years now.
Assuming you are sold on the idea, if you want to implement a guard within your codebase, instead of reinventing the wheel whenever you start a new project, you can instead opt for a ready-to-go solution like GuardClauses
While there are a few guard packages on the marketplace like Ensure.That and Dawn.Guard, my personal favorite is GuardClauses. With over 8.1 million downloads, many other developers seem to agree as well. To install GuardClauses you can use this command:
Once installed you have many ways to validate incoming arguments. Below lists some of your available options:
GuardClauses is lightweight, and it has got everything you need, what more is there to say?
⬇️⬇️⬇️ Download from here
6. BenchmarkDotNet
You can install this package using this command:
One thing to note about BenchmarkDotNet is that it will not work directly inside a web project. If you want to use this to test an API, or, some code within your website, you will need to create an additional console app within your solution and use that to benchmark your code's execution time. Another gotcha is that you need to remember to build your code in release
mode and not debug
mode. This caveat definitely caught me out the first time I tried to use it!
To benchmark code, you can decorate your methods using the Benchmark
attribute like this:
Within your console app, you can then trigger this code, like this:
When run the code will benchmark your code 100 times and then output the results within the terminal window:
⬇️⬇️⬇️ Download from here
7. Ben.Demystifier
Demystifier will help you to make the debugging process simpler. After installing Demystifier, whenever an exception is thrown you can configure your code so that your stack traces will include the output based on the compiler-transformed methods, rather than the source code compiled methods.
In essence, this package will provide you with more useful information within your logs. This means that when things go wrong, you should be able to more easily understand the reason why things have broken, saving you time. To install this package you can run this command:
After you have the package enabled, you will have access to two new extension methods on the Exception
called .ToStringDemystified()1 and
Demystify()`
Using these methods you will then get slightly more description within your Stacktrace as seen in the real-world example below:
⬇️⬇️⬇️ Download from here
8. Flurl
Downloaded over 50 million times, Flurl is an HTTP Client that can be used to replace the vanilla .NET HttpClient
class. One of the standout things about Flurl is that it is easier to write unit tests with it. Granted, there are a few other clients on the market like RestSharp, or Refit that claim to do a similar thing, however, I personally prefer this one.
Flurl allows you to consume services by building a fluent chain of instructions. To install Flurl, you can use this command:
After you install Flurl you can then make HTTP calls like this:
You can then write unit tests in order to validate that an API returns a response in the format you expect using this code:
⬇️⬇️⬇️ Download from [here]https://www.nuget.org/packages/Flurl/) and learn more here
That concludes the list. I hope you found something new and useful that will aid your programming abilities. Happy Coding 🤘