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.

71 posts •
Mastering owned entities in EF Core: Cleaner complex types

Uses a PostgreSQL-backed EF Core console app with a Movie entity and a Budget owned entity holding ProductionCost, MarketingCost, and DistributionCost to show how owned types get stored as prefixed columns in the parent table without their own primary key, DbSet, or independent lifecycle. ...

Building Read Models with EF Core Projections

Compares fetching a PostgreSQL Movie/Director schema with and without EF Core projections, showing how loading full entities with every column and navigation property triggers extra change tracking, slower deserialization, and N+1 query risk versus selecting only the fields a view actually needs. ...

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

Builds the same Product/Quantity/Price Excel report three ways in C# with the low-level DocumentFormat.OpenXml SDK's SpreadsheetDocument and WorkbookPart classes, then EPPlus, then ClosedXML, contrasting OpenXML's verbose cell-by-cell XML construction against the other libraries' higher-level APIs. ...

Repository pattern vs Specification: Which is more maintainable?

Compares DDD's Repository pattern against the Specification pattern using a Building aggregate root with nested Floor and Location child entities, showing how encapsulating query logic in reusable Specification objects scales better than adding new repository methods each time query needs grow. ...

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

Explains the .NET Garbage Collector's mark, relocate, and compact phases on the managed heap and its Gen 0/1/2 generational model, showing why short-lived objects like loop variables are reclaimed cheaply in Gen 0 while longer-lived objects get promoted to slower, less frequent collections. ...

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

Uses BenchmarkDotNet with MemoryDiagnoser to measure boxing a plain int into object, unboxing it back, and comparing both against an unboxed baseline, then extends the test to ArrayList versus List<int> to quantify how boxing's hidden heap allocations and GC pressure outweigh simple stack usage. ...

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

Shows how to build and compile C# expression trees at runtime using ParameterExpression, ConstantExpression, and Expression.Lambda to generate a working Func<int,int> delegate, then extends the technique to filter a Building entity's properties dynamically based on user-chosen search conditions. ...

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

Demonstrates with runnable C# code why an exception thrown inside an async void method crashes the app instead of being caught by a surrounding try/catch, unlike async Task, then walks through the compiler-generated state machine and AsyncTaskMethodBuilder that make Task methods exception-safe. ...

Visualizing LINQ Queries with LINQPad: Boost Your EF Core Debugging

Walks through installing LINQPad, created by Joe Albahari in 2007, connecting it to PostgreSQL and SQL Server databases, and using its interactive Database Explorer to drill into related tables like Albums and Tracks while inspecting the exact SQL generated behind an EF Core LINQ query. ...