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 •
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. ...

Handling Complex Query Scenarios with Entity Framework Core

Speeds up EF Core queries on a Building/Floor model: projecting only needed columns into a DTO, disabling tracking per-query with AsNoTracking or globally via HasQueryTrackingBehavior, choosing lazy loading with Microsoft.EntityFrameworkCore.Proxies versus eager loading, plus indexing/pagination. ...

Leveraging Tuples in C#: Real-World Use Cases

Shows C# tuple syntax from Tuple<T> through named literal tuples (C# 7.0+), nested tuples, and deconstruction, then two real examples: passing a (Latitude, Longitude) tuple as a method parameter, and returning a (Subject, Body) tuple from an email-content generator keyed off an EmailTypeEnum. ...

Unlocking delegate's potential in C#

Builds C# delegate intuition with a waiter/chef analogy, then two code examples: a multicast PrintMessage delegate chaining PrintToConsole and PrintToFile via +=, and a menu-driven console app mapping user choices to AddContact/ViewContact/DeleteContact through a Dictionary<string, PrintMessage>. ...