This post lists open source libraries available on Nuget.org that I regularly use and recommend. These packages will save you time and make your applications better. They have good documentation and a great developer experience.
MediatR
MediatR is a simple in-process implementation of the mediator pattern. It helps developer write clean, decoupled and extendable code. Mediator is configured using dependency injection with out of the box support for .NET Core, Autofac and others. The documentation on Github is the best place to get started.
I like to use MediatR to write simple command query type requests using the IRequest interface. IRequest handle messages sent to a single handler. Examples of types of IRequests in a standard web application might be "GetUserQuery" or "UpdateCartCommand". The example below uses an internal nested class to keep the query model and query handler together in one class.
Mediatr also supports notification messages which can have multiple handlers through INotification. Notifications work in a similar way to requests but don't return values. They are great for exposing a "extension point" in your application that you may need to build on later.
As a side note: everything Jimmy Bogard writes code or otherwise is great.
Serilog
Serilog provides structured logging which can target output to just about anywhere. Structured logs capture your log statements and objects as json. They give much more context into what is happening in your application and with the right tools they can be queried and analysed in more detail than standard text logs.
One of the best things about Serilog is the community has written sinks - which write logs to an provider for just about every service you can think of. The provided sinks Github page has a full list. A couple I recommend checking out:
- Seq - self hosted structured log viewer
- Sentry- exception reporting service that plugs in automatically to error log events
Hangfire
Hangfire is an easy way to perform scheduled or fire-and forget background jobs for .NET Core and Framework applications. I love how easy it is to get up an running as the jobs can run in your app's main process without requiring a dedicated service.
Hangfire has a great built in dashboard, dependency injection support and storage options for SQL server, PostgreSQL, Redis and more.
FluentEmail
FluentEmail (written by me) helps you implement complete email sending functionality in your app in less than 10 minutes. It features built in providers for the most popular email senders including SendGrid and Mailgun along with Razor templates out of the box. I recently wrote a full guide to .NET email using FluentEmail that will get you started.
LazyCache
LazyCache is an easy to use, thread safe and developer friendly wrapper for in-memory caching in .NET Core. I've written a bit about LazyCache in the past and I still reach for it every time I need a cache provider.
LazyCache uses a "GetOrAdd" pattern for caching where you request an item from the cache and at the same time provide the function to add that item to the cache if it is missing.
LazyCache is easy to extend so if you need to move beyond a simple in memory cache to something distributed the step up should be pretty seamless.
Dapper
Sometimes SQL data access with Entity Framework is overkill or just too slow. When you want to run raw SQL but still get some nice object mapping Dapper is the way to go.
Dapper is a NuGet library that you can add in to your project that will extend your
IDbConnection
interface.
Dapper takes your existing database connection and adds extension methods to run raw SQL and map it back to C# classes.
MiniProfiler
MiniProfiler is a simple performance profiler for .NET Core and framework (also Ruby, Go and Node) web applications. It attaches automatically to the key parts of your application - mainly database access. MiniProfiler will then render an awesome little timing result directly on your app. It is great for running during development to pick up rogue LINQ queries.
MiniProfiler has great setup documentation with a heap of options. For security reasons, I normally only have it enabled during development.
LinqKit
LINQKit is a set of extensions for querying Entity Framework with Linq. LINQKit allows you to build linq queries and conditions as predicates.
My most common use for LINQKit is simplifying queries with lots of criteria (think grids with filters) using the PredicateBuilder.
FluentMigrator or DbUp
If you are looking for an alternative to running EntityFramework code first migrations both FluentMigrator and DbUp will do the job.
DbUp works best if you like raw SQL and simple scripts. You simply create a console application and add SQL files as embedded resources. Some simple code in Program.cs will run your migration scripts in order - easy to integrate into a devops pipeline.
FluentMigrator takes a similar concept with a more code-based approach. Instead of adding raw scripts you create small classes for each migration. This approach gives you more flexibility in building migrations (and saves you remembering the create foreign key sql syntax) and allows for more complete up/down migration scenarios.
CsvHelper
CsvHelper is my go to package for reading and writing csv files. The library is flexible enough that you can creating custom mapping classes and rules for your C# classes or use the low level row and column methods directly.
There are similar options for exporting csv files either with direct column writes or using classes and mapping.
Hashids
Hashids for .NET generates short, unique string identifiers from integers - similar to YouTube video ids. It is great for turning one or more integers into a single short, non sequencial hashes to use in urls and sharing. Just don't use it in place of security.
var hashids = new Hashids("example salt");
//creates "R9tGSE" hash
var id = hashids.Encode(1, 2, 3);
//decodes "R9tGSE" back into int[] {1, 2, 3}
var numbers = hashids.Decode(id);
Humanizer
Humanizer is for turning dates, numbers, enums and more in human friendly readable strings. It supports lots of data types and can be used with multiple languages.
Bogus
Bogus is a "fake" data generator for .NET. I shudder at the thought of generating "demo" data and "test" data but Bogus takes a bit part of that pain away.
Bogus allow you to map you C# classes to fake data to quickly generate lots of data. It is also possible to access the underlying methods directly and generate a phone number, email address etc on demand.
Honourable mentions
I also love these libraries but this post was taking me forever to finish.
- FluentValidation
- AutoMapper
- Autofac- if you are stuck on .NET framework this if your best dependency injection option
If you enjoyed the post, please share the thread on Twitter and let me know if I missed out your favourite libraries.
Top comments (1)
That is a great list!
Still need to try few of them.