Managing resources effectively in .NET is critical for both application stability and performance. In this post, we’ll explore the proper use of the IDisposable pattern, guidelines for when to dispose objects using a using statement, and some pitfalls of disposing objects blindly—such as disposing shared resources. Understanding IDisposable The IDisposable interface provides a standardized way …
C#
C# Logging Best Practices to Improve Troubleshooting
Designing methods with a focus on logging and exception handling is crucial for building maintainable and debuggable software. In this post, we’ll discuss some C# logging best practices about designing method in a way that gives us the most level of details. A lot of time we see that an error happens in our software, …
ASP.NET Core Best Practices: A Performance Quick Reference
ASP.NET Core is fast by default, and most of the slowdowns in real apps trace back to a handful of habits: blocking on async code, loading too much data in one go, and putting heavy work on code that runs for every request. This post collects those ASP.NET Core best practices in one place, along …
Entity Framework Core and LINQ Best Practices for C#
Entity Framework makes data access easy to write, and that’s also what makes it easy to write badly. A query that’s instant against a few test rows can bring the database to a crawl once it hits production data, and the C# often looks perfectly fine. This is my reference list of LINQ and Entity …
C# Async and Parallel Programming Best Practices
I’ve written about asynchronous and parallel programming before, in the seven most common async mistakes and what SynchronizationContext is. Few topics in C# produce as many subtle bugs. Deadlocks only show up on the UI thread, exceptions disappear from async void methods, and race conditions never happen on your machine and happen every day in …
Thread Affinity in .NET: When CPU Pinning Helps and How to Use TPL
Thread affinity means restricting a thread to run only on certain CPU cores. Without it, the operating system’s scheduler can move a thread from one core to another as it balances the load across the machine. The idea behind pinning a thread is cache locality: a thread that stays on one core keeps its working …
When to Use Inheritance in C# (and When to Avoid It)
Inheritance is the feature most object-oriented programmers learn first. It’s also one of the most misused. Used well, it gives you a single abstraction that many types can stand behind, and calling code that never needs to know which one it has. Used badly, it gives you hierarchies five levels deep, base classes nobody dares …
How to use ConfigBuilders for User Secrets Management in Asp.Net MVC 5
When it comes to storing our app secrets, Asp.Net Core has a built-in mechanism to do that out of the box. But this is not the case for Asp.Net MVC 5. For MVC 5, we need to install custom configBuilders packages in order to be able to have the same functionality. There are many other …
Integration Test In Asp.Net Core 6 Using SqlServer Image and TestContainers
This post is about writing integration tests and TestContainers. As you might know writing integration tests against the in-memory provider is a bad practice. Because it only works well with trivial solutions. Mostly because the in-memory API of IQueryProvider doesn’t match the LINQ query provider. Here’s a good article called “Avoid In-Memory Databases for Tests” …
Enum Serialization: System.Text.Json vs Utf8Json vs Jil vs Newtonsoft
I was reading about the new System.Text.Json in .net core 3 and comparing its speed to other libraries. You can read more about it here if you’re interested. To put it shortly, the new serializer is faster then Newtonsoft but not as fast as others. Also the enum serialization between different libraries can be inconsistent. …
