Ali Hamza Ansari

Ali Hamza Ansari

Meet Ali, a talented .NET developer from Pakistan. Besides blogging, Ali works for a range of international customers doing everything from C# to Azure.

45 posts
Using Strategy Pattern with Dependency Injection in ASP.NET Core

Selection logic is a prominent part of many applications. Whether you add a simple environment toggle, a UI mode decision, or apply a discount, you have to rely on user input. Sometimes, simply using an intuitive if-else or a switch case can work. However, when conditions are growing or a ...

Mastering owned entities in EF Core: Cleaner complex types

Not all data in your application should live as a standalone table with its own ID and lifecycle. Sometimes you need a tightly coupled dependent object that exists alongside its parent, like a movie's budget, a survey's questions, or a customer's address. If you ...

Building Read Models with EF Core Projections

You can't really argue against the importance of performance in any application. Even if the system is not time-critical, efficient and time-saving operations are cornerstones of the system. Data fetching is the primary step for improving the performance of most applications. If you use Entity Framework Core (EF ...

Working with Excel files in .NET: OpenXML vs EPPlus vs ClosedXML

If you have ever had to generate Excel reports in C#-based applications, you know the pain of choosing from too many libraries with too many tradeoffs. Some are extremely powerful but painful to write, while others are friendly but struggle with large files. You probably come across OpenXML, EPPlus, ...

Repository pattern vs Specification: Which is more maintainable?

When talking about Domain-Driven Design (DDD), the repository pattern becomes a default choice for the data access layer. Another pattern is the Specification pattern that organizes code into smaller objects. The question raised here is which suits your project and which is more maintainable as your project scales? I will ...

How .NET Garbage Collector works (and when you should care)

In the world of .NET, memory management is an important aspect of any application. Fortunately, you don't have to shoulder this immense task yourself. .NET handles it with the superpower of the Garbage Collector (GC). A GC is an engine that keeps your app fast, responsive, and resource-efficient. ...

Hidden Costs of Boxing in C#: How to Detect and Avoid Them

C# Boxing and Unboxing are vital players in the performance of an application. However, they are often overlooked. They involve heap allocations that bring a penalty due to their accessing mechanism. In today's post, we will unfold Boxing and Unboxing in detail, study how they are costly to ...

Expression Trees in C#: Building Dynamic LINQ Queries at Runtime

Tired of endless if-else blocks just to build queries? What if your LINQ queries could write themselves at runtime? Today, we will unfold expression trees, which can be used to create dynamic queries at runtime. I will show how to use expression trees in your project and understand their advantages ...

Demystifying async void in C#: Why It's Dangerous and When It's Okay

Async operations are an integral part of any application. In C#, mostly asynchronous methods return Task or Task<T>, but there's also the odd case of async void. We know void is for synchronous operations, so why does C# even allow async void? I will dissect ...