7 Useful .NET Libraries You Will Need

Cornelius Tantius
Bina Nusantara IT Division
4 min readDec 15, 2022
Photo by Gabriel Sollmann on Unsplash

For the last two year, I have been working as BackEnd developer at Bina Nusantara IT Division. During my work, I often use .NET as well. Based on my experience while working with .NET, on this article, I will share 7 useful .NET libraries that I use during my work and might be the library that you need.

1. AutoMapper

As described from the name itself, AutoMapper is a library used to map data between object. Since .NET is such an Object Oriented Programming framework, AutoMapper helps a lot by doing object value mapping.

var config = new MapperConfiguration(cfg => cfg.CreateMap<Order, OrderDto>());

var mapper = config.CreateMapper();
// or
var mapper = new Mapper(config);

OrderDto dto = mapper.Map<OrderDto>(order);

2. RestSharp

RestSharp is a lightweight wrapper for HttpClient library. So, RestSharp basically make your any Rest API request easier. With RestSharp you can add any kind of parameter such as body in form of json, XML and Form Data, not just headers. It even has build in json and XML serialization and deserialization.

var client = new RestClient("https://api.twitter.com/1.1") {
Authenticator = new HttpBasicAuthenticator("username", "password")
};
var request = new RestRequest("statuses/home_timeline.json");
var response = await client.GetAsync(request, cancellationToken);

3. MailKit

MailKit is a mail library for client server that allows you to do so many things such as SASL Authentication, Proxy support for SOCKS4/4a, SOCKS, and HTTP, SMTP, POP3, IMAP4 Client, Async API and API cancellation.

var message = new MimeMessage ();
message.From.Add (new MailboxAddress ("Joey Tribbiani", "joey@friends.com"));
message.To.Add (new MailboxAddress ("Mrs. Chanandler Bong", "chandler@friends.com"));
message.Subject = "How you doin'?";

message.Body = new TextPart ("plain") {
Text = @"Hey Chandler, I just wanted to let you know that Monica and I were going to go play some paintball, you in?

-- Joey"};
using (var client = new SmtpClient ()) {
client.Connect ("smtp.friends.com", 587, false);

// Note: only needed if the SMTP server requires authentication
client.Authenticate ("joey", "password");

client.Send (message);
client.Disconnect (true);
}

4. LiteDB

LiteDB is a serverless database packed in less than 450kb DLL, that allows you to create and use a NoSQL database. It is very suitable and useful for small project that require not much database write. It allows Thread-safe with cross collection transaction, No locks for readers. Per collection writer locks and a lot more.

// Create your POCO class entity
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public string[] Phones { get; set; }
public bool IsActive { get; set; }
}

// Open database (or create if doesn't exist)
using(var db = new LiteDatabase(@"C:\Temp\MyData.db"))
{
// Get a collection (or create, if doesn't exist)
var col = db.GetCollection<Customer>("customers");

// Create your new customer instance
var customer = new Customer
{
Name = "John Doe",
Phones = new string[] { "8000-0000", "9000-0000" },
IsActive = true
};

// Insert new customer document (Id will be auto-incremented)
col.Insert(customer);

// Update a document inside a collection
customer.Name = "Jane Doe";

col.Update(customer);

// Index document using document Name property
col.EnsureIndex(x => x.Name);

// Use LINQ to query documents (filter, sort, transform)
var results = col.Query()
.Where(x => x.Name.StartsWith("J"))
.OrderBy(x => x.Name)
.Select(x => new { x.Name, NameUpper = x.Name.ToUpper() })
.Limit(10)
.ToList();

// Let's create an index in phone numbers (using expression). It's a multikey index
col.EnsureIndex(x => x.Phones);

// and now we can query phones
var r = col.FindOne(x => x.Phones.Contains("8888-5555"));
}

5. CacheManager

CacheManager is caching library that helps on complex caching cases that allows you to do complex caching in few lines of code.

var cache = CacheFactory.Build("getStartedCache", settings =>
{
settings.WithSystemRuntimeCacheHandle("handleName");
});

cache.Add("keyA", "valueA");
cache.Put("keyB", 23);
cache.Update("keyB", v => 42);

Console.WriteLine("KeyA is " + cache.Get("keyA")); // should be valueA
Console.WriteLine("KeyB is " + cache.Get("keyB")); // should be 42
cache.Remove("keyA");

Console.WriteLine("KeyA removed? " + (cache.Get("keyA") == null).ToString());

Console.WriteLine("We are done...");
Console.ReadKey();

6. SharpCompress

SharpCompress is a library allows you to perform compression or decompress to files. It can do Zip writing. RAR support, and much more.

using (var archive = ZipArchive.Create())
{
archive.AddAllFromDirectory("D:\\temp");
archive.SaveTo("C:\\temp.zip", CompressionType.Deflate);
}

7. PuppeteerSharp

PuppeteerSharp is official port from Node Js Puppeteer API where this library allows you to automate browser interaction and even do web testing or scraping.

using var browserFetcher = new BrowserFetcher();
await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);
var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
Headless = true
});
var page = await browser.NewPageAsync();
await page.GoToAsync("http://www.google.com");
await page.ScreenshotAsync(outputFile);

--

--