Using these nuget projects consistently in most projects I'm building. See the examples in the details for usage.


Install-Package FluentValidation
Doing user input validation can be tedious. This makes it less so.
public class CustomerValidator: AbstractValidator<Customer> {
    public CustomerValidator() {
        RuleFor(x => x.Surname).NotEmpty();
        RuleFor(x => x.Forename).NotEmpty().WithMessage("Please specify a first name");
        RuleFor(x => x.Discount).NotEqual(0).When(x => x.HasDiscount);
        RuleFor(x => x.Address).Length(20, 250);
        RuleFor(x => x.Postcode).Must(BeAValidPostcode).WithMessage("Please specify a valid postcode");

    private bool BeAValidPostcode(string postcode) {
        // custom postcode validating logic goes here

// Usage
var customer = new Customer();
var validator = new CustomerValidator();
ValidationResult results = validator.Validate(customer);

bool success = results.IsValid;
IList<ValidationFailure> failures = results.Errors;

GitHub reference


Install-Package Microsoft.Extensions.DependencyInjection
Dependency injection aka inversion of control is a popular pattern. Lately I've stuck with the Microsoft version as it integrates nicely with and fairly simple. Unit testing becomes easier since the interfaces can be mocked.

In this example, a service collection is created using interfaces with corresponding concrete implementation. Then one service is used within a scope.

var services = new ServiceCollection();
services.AddSingleton<IFoo>(new Foo());
services.AddScoped<IBar, Bar>();
var provider = services.BuildServiceProvider();

// usage
using var scope = _provider.CreateScope();
var instance = scope.ServiceProvider.GetService<IBar>();

Usage reference


Install-Package Easy.Common
Have you ever tried counting lines of a very large file? If not, it can get complicated quick.
var file = new FileInfo("veryLargerFile.csv");
using var stream = file.OpenRead(); 
var lines = stream.CountLines();

There are other useful extensions. GitHub reference

Xunit, Moq, AutoMoq

Install-Package xunit
Install-Package Moq 
Install-Package AutoFixture.AutoMoq
Example of mocking
    public async Task Can_get_message_from_cache() {

        // Arrange
        var expected = _fixture.Create<uint>();
        _mock.Setup(x => x.Get(expected))
                .Do(x => x.Headers.Add("UID", expected.ToString()))

        // Act
        var message = await _provider.GetMessageAsync(expected);

        // Assert
        Assert.Equal(expected, uint.Parse(message.Headers["UID"]));

Xunit getting started,was%20called%20a%20particular%20number%20of%20times.%20


Install-Package Polly
Need retry logic? Exponential back off? This project solves the problems easily with tons of of extensions!
var retryPolicy = Policy.Handle<TransientException>()
.WaitAndRetry(retryCount: 3, sleepDurationProvider: _ => TimeSpan.FromSeconds(1));

var attempt = 0;
retryPolicy.Execute(() =>
    Log($"Attempt {++attempt}");
    throw new TransientException();

GitHub reference

Honorable mentions