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.

65 posts
16 common mistakes C#/.NET developers make (and how to avoid them)

Covers 16 recurring C#/.NET mistakes with code fixes, including catch blocks resetting the stack trace with throw e instead of a bare throw, organizing code by technical layer instead of feature/domain, over-engineering with unneeded abstraction layers, and skipping async/await on DbContext queries. ...

IEnumerable vs. IAsyncEnumerable in .NET: Streaming vs. Buffering

Benchmarks IEnumerable against IAsyncEnumerable in .NET using an EF Core InMemory database seeded with 100,000 and 1,000,000 Book records, measuring memory allocation and execution time to show why IAsyncEnumerable, introduced in .NET Core 3.0 and C# 8.0, cuts overhead for latency-prone sources. ...

Storing JSON data in relational databases using EF Core

Shows how to persist JSON in EF Core across three databases: PostgreSQL's native jsonb column via the Npgsql.EntityFrameworkCore.PostgreSQL package, SQL Server's nvarchar(max) with a HasConversion-based System.Text.Json serializer, and MySQL, all demonstrated on a shared LogEntry/LogDetail model. ...

Soft deletes in EF Core: How to implement and query efficiently

Implements soft deletes in EF Core three ways on a Book entity: manual IsDeleted flagging, a global HasQueryFilter driven by a shared SoftDeletableModel base class exposing IsDeleted and DeletedAt, and a SaveChanges override, comparing how each approach affects querying and rollback safety. ...

4 real-life examples of using reflection in C#

Walks through four practical C# reflection scenarios: invoking a method on a runtime ClassA instance via Activator.CreateInstance and MethodInfo.Invoke, then building a dynamic CSV exporter, an automatic property-to-property model mapper, and a plugin loader, plus tips for efficient use. ...

Understanding GraphQL in .NET: When and why to use it

Explains when GraphQL beats REST in .NET via three cases: a social feed fetching nested author/likes/comments in one query, an e-commerce app returning different fields to mobile versus web, and an IoT dashboard using GraphQL subscriptions over WebSockets, then builds an API with HotChocolate. ...

Optimizing JSON Serialization in .NET: Newtonsoft.Json vs. System.Text.Json

Compares Newtonsoft.Json, with roughly 6 billion NuGet downloads, JsonProperty attributes, and JsonConvert.SerializeObject, against System.Text.Json's JsonPropertyName attribute and JsonSerializer.Serialize, introduced in .NET Core 3.0, covering nested object graphs to help decide when to migrate. ...

Understanding EF Core Change Tracking: How It Works Under the Hood

Traces what EF Core's ChangeTracker does with a Student/Enrollment model: context.Add marks new entities Added and assigns temporary keys, SaveChanges emits INSERTs in dependency order and backfills the real StudentId foreign key, versus attaching an Enrollment to an already-tracked Student. ...

.NET Dependency Injection: Advanced Techniques Beyond the Basics

Goes beyond Singleton/Scoped/Transient basics with concrete .NET DI patterns: resolving services manually in an MqttListener via IServiceProvider versus IServiceScopeFactory for scoped lifetimes, injecting IEnumerable to get every registered implementation, and registering variants via AddKeyed. ...