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.

70 posts
The complete guide to mastering Dapper micro-ORM in .NET

Walks through building a .NET console app that uses Dapper, the micro-ORM created by Stack Overflow, with Npgsql to query a PostgreSQL database of Movie and Director records, covering a DbConnectionFactory setup, JOINs, aggregate functions, stored procedures, and transactions. ...

Pattern matching in C#: Advanced scenarios you didn't know

Demonstrates advanced C# pattern matching using record types for User, Address, and Request, covering nested property patterns like user is { Address.City: "Karachi" }, negation with 'not', combined 'or' cases, LINQ filtering by shape, and relational patterns like age is > 18 and < 60. ...

12 practices for optimizing PostgreSQL queries for large datasets

Covers 12 PostgreSQL tuning techniques with EXPLAIN ANALYZE benchmarks, including adding a customer_id index that cuts a 240ms query, avoiding SELECT *, reordering JOINs so the smaller table filters first, using LIMIT, partial indexes like idx_completed_orders, and choosing efficient data types. ...

EF Core query translation: Why does some LINQ never become SQL?

Explains why some EF Core LINQ queries fail to translate to SQL, using a Perfume model backed by PostgreSQL and Npgsql to show ToQueryString() output, how StartsWith becomes a SQL LIKE clause, and cases involving custom methods or reflection that force client-side evaluation instead. ...

Mapping database views in EF Core without breaking migrations

Shows how to map a read-only PostgreSQL view named vw_product_summary into a ProductSummary entity in EF Core using ToView() and HasNoKey(), keeping the mapped view separate from the Product table so schema migrations don't break when the view definition changes. ...

How .NET handles exceptions internally (and why they're expensive)

Benchmarks .NET exception handling with BenchmarkDotNet across 100,000 iterations, comparing a no-exception baseline, a try/catch path throwing InvalidOperationException, and a OneOf<T> discriminated-union return type to quantify the CLR's stack-unwinding and heap-allocation overhead. ...

Designing business rules that don't leak into controllers

Refactors a fat ASP.NET Core OrdersController that enforces a five-orders-per-day rule directly inside CreateOrder into a testable IOrderService/OrderService pair, showing how moving validation like user-activity checks and order limits out of the controller keeps it thin and SRP-compliant. ...

When NOT to use the repository pattern in EF Core

Builds an IMovieRepository/MovieRepository/MovieService layer around EF Core's DbContext with a GetTopRatedAsync(8.0) query, then argues the abstraction pays off only for complex domains like banking or insurance, while simple CRUD apps are often better served by DbContext directly. ...

Why IEnumerable Can Kill Performance in Hot Paths

Benchmarks IEnumerable against List, arrays, LINQ chains, and yield-based sequences in C# using BenchmarkDotNet with collection sizes of 1,000 and 100,000, showing how deferred execution and repeated enumeration can make IEnumerable up to 7x slower than a materialized List in hot paths. ...