Skip to content

Essential NuGet Packages to supercharge dotnet productivity

Posted on:Mar 20, 2023 |Your Image4 Mins| Tech
  •  dotnet
  •  nuget
  • essential nuget packages for dotnet As a senior .NET developer, it’s crucial to stay updated with the best NuGet packages available for various functionalities. In this technical blog, we will explore and discuss the top packages for mapping, logging, ORM (Object-Relational Mapping), HTTP clients, validation, mail, and task scheduling functionalities. Let’s dive in!

    Table of contents

    Open Table of contents

    Mapping: AutoMapper

    AutoMapper is a great tool for transferring data between different parts of your application. It makes the process of converting complicated data structures simpler, reducing the need for excessive code. With AutoMapper, you can easily set up rules for mapping objects to other objects, enabling you to concentrate on the core logic instead of writing repetitive mapping code. It offers a straightforward and effective approach to managing object mapping in your .NET projects.

    using AutoMapper;
    
    // Configure AutoMapper
    var config = new MapperConfiguration(cfg =>
    {
        cfg.CreateMap<SourceClass, DestinationClass>();
    });
    
    // Create the mapper instance
    var mapper = config.CreateMapper();
    
    // Map objects
    var destination = mapper.Map<DestinationClass>(source);

    Logging: Serilog

    Serilog is a highly extensible and flexible logging library for .NET applications. It offers a simple yet powerful API for logging structured events, making it easier to analyze and diagnose issues in your system. Serilog supports various sinks, enabling you to log to different destinations such as the console, files, databases, or even cloud-based services. With its rich ecosystem of extensions and integrations, Serilog is an excellent choice for all your logging needs.

    using Serilog;
    
    // Configure Serilog logger
    Log.Logger = new LoggerConfiguration()
        .WriteTo.Console()
        .WriteTo.File("log.txt")
        .CreateLogger();
    
    // Log messages
    Log.Information("This is an informational log message");
    Log.Error("An error occurred: {ErrorMessage}", ex.Message);

    Unlike other logging libraries, Serilog is built from the ground up to record structured event data.

    var position = new { Latitude = 25, Longitude = 134 };
    var elapsedMs = 34;
    log.Information("Processed {@Position} in {Elapsed} ms", position, elapsedMs);

    Community-backed logging API with familiar levels (Debug, Info, Warning, Error, etc.). Supports C# config syntax, XML/JSON config, and performs efficiently. Excellent .NET Core support, including ASP.NET Core integration. Provides diverse logging sinks (files, console, servers, databases, queues). Enriches log events with contextual info (scoped props, identifiers, correlation ids). Logger objects are stateless, with optional global static Log class. Allows logging in various formats (plain text, JSON, in-memory LogEvent objects).

    ORM (Object-Relational Mapping): Entity Framework Core

    Entity Framework Core (EF Core) is a popular ORM framework for .NET apps. It offers a simple way to interact with your database by using objects and LINQ queries. With support for different database providers, EF Core provides features like migrations, query optimization, and change tracking. It streamlines your data access layer and ensures efficient database interaction.

    using Microsoft.EntityFrameworkCore;
    
    // Create DbContext
    public class MyDbContext : DbContext
    {
        public DbSet<Customer> Customers { get; set; }
    
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer("connection_string_here");
        }
    }
    
    // Use DbContext in code
    using (var dbContext = new MyDbContext())
    {
        var customers = dbContext.Customers.ToList();
        // Perform database operations
    }

    HTTP Clients: RestSharp

    RestSharp is a popular library for making HTTP requests in .NET applications.

    It provides a simple and intuitive API for sending HTTP requests and handling responses. RestSharp supports various features such as request parameters, headers, authentication, and serialization/deserialization of request/response bodies. It is a reliable choice for interacting with RESTful APIs.

    If you need to make a few one-time requests to an API, RestSharp can be used as follows.

    using RestSharp;
    
    // Create a RestSharp client instance
    var client = new RestClient("https://api.example.com");
    
    // Create a RestRequest for the specific API endpoint
    var request = new RestRequest("users/{id}", Method.GET);
    request.AddUrlSegment("id", "123"); // Replace with the actual user ID
    
    // Optionally, add query parameters
    request.AddQueryParameter("name", "John");
    
    // Optionally, add request headers
    request.AddHeader("Authorization", "Bearer <access_token>");
    
    // Execute the request and get the response
    var response = client.Execute(request);
    
    // Check if the request was successful
    if (response.IsSuccessful)
    {
        // Process the response data
        var content = response.Content;
        Console.WriteLine(content);
    }
    else
    {
        // Handle the error
        Console.WriteLine($"Request failed with status code: {response.StatusCode}");
        Console.WriteLine($"Error message: {response.ErrorMessage}");
    }

    Validation: FluentValidation

    FluentValidation is a powerful and extensible validation library for .NET applications. It allows you to define validation rules for your models using a fluent and expressive syntax. With FluentValidation, you can easily validate user input, API requests, or any other data within your application. It supports complex validation scenarios, such as cross-property validation and custom validation rules. The package integrates seamlessly with popular frameworks like ASP.NET Core and provides comprehensive error messages and localization support. Full documentation can be found at https://docs.fluentvalidation.net

    using FluentValidation;
    
    // Create a validator
    public class UserValidator : AbstractValidator<User>
    {
        public UserValidator()
        {
            RuleFor(user => user.Name).NotEmpty().MaximumLength(50);
            RuleFor(user => user.Email).NotEmpty().EmailAddress();
        }
    }
    
    // Use the validator
    var user = new User { Name = "John", Email = "john@example.com" };
    var validator = new UserValidator();
    var validationResult = validator.Validate(user);
    
    if (validationResult.IsValid)
    {
        // The user is valid
    }
    else
    {
        // Handle validation errors
        foreach (var error in validationResult.Errors)
        {
            Console.WriteLine(error.ErrorMessage);
        }
    }

    Mail: MailKit

    MailKit is a cross-platform email client library that provides a high-level API for sending and receiving emails. It supports various email protocols, including SMTP, POP3, and IMAP, making it suitable for a wide range of email-related tasks. MailKit is reliable, performant, and offers extensive features like MIME support, secure connections, attachments, and HTML email composition. With MailKit, you can easily integrate email functionality into your .NET applications. Detailed documentation can be found at http://www.mimekit.net/docs/html/Introduction.htm

    using MailKit.Net.Smtp;
    using MimeKit;
    
    // Create a MimeMessage
    var message = new MimeMessage();
    message.From.Add(new MailboxAddress("Sender Name", "sender@example.com"));
    message.To.Add(new MailboxAddress("Recipient Name", "recipient@example.com"));
    message.Subject = "Hello from MailKit!";
    message.Body = new TextPart("plain")
    {
        Text = "This is the message body."
    };
    
    // Configure and send the message
    using (var client = new SmtpClient())
    {
        client.Connect("smtp.example.com", 587, false
    
    );
        client.Authenticate("username", "password");
        client.Send(message);
        client.Disconnect(true);
    }

    Task Scheduler: Hangfire

    Hangfire is a popular background processing and task scheduling library for .NET applications. It provides a simple and reliable way to execute recurring or delayed tasks in the background, allowing you to offload time-consuming operations from the main request/response cycle. Perform fire-and-forget, delayed and recurring, long-running, short-running, CPU or I/O intensive tasks. Hangfire supports different storage options and offers a user-friendly dashboard for monitoring and managing scheduled tasks. Its simplicity and robustness make it an excellent choice for implementing background processing capabilities in your .NET projects. Hangfire dashboard

    using Hangfire;
    
    // Configure Hangfire
    GlobalConfiguration.Configuration.UseSqlServerStorage("connection_string_here");
    
    // Define a background job
    BackgroundJob.Enqueue(() => Console.WriteLine("Hello from Hangfire!"));
    
    // Schedule a recurring job
    RecurringJob.AddOrUpdate(() => Console.WriteLine("Recurring job running..."), Cron.Minutely);
    
    // Start the Hangfire server
    using (var server = new BackgroundJobServer())
    {
        Console.WriteLine("Hangfire server started. Press any key to exit...");
        Console.ReadKey();
    }

    Conclusion

    In this blog post, we checked out some awesome NuGet packages for all the common stuff you need in .NET development. These packages give you reliable, efficient, and user-friendly solutions for mapping, logging, ORM, HTTP clients, validation, mail, and task scheduling tasks in your apps.

    Don’t forget to give the documentation and community support a look for each package to make sure they fit your specific needs. By using these NuGet packages, you can save time, level up your code quality, and deliver top-notch software solutions in your .NET projects.

    Good luck, and keep Learning!!