Tag: C#


  • Unleashing the Power of BeginScope in .NET

    We have been using the ILogger interface provided by .NET Core, but do you know how to leverage BeginScope for our benefit? Let’s explore how BeginScope helps us harness log scoping effectively. Let’s examine a sample transaction API in a minimal API project and see how one would typically write logs as the transaction flows…

  • How Writer Monad Supercharges Option Monad

    We’ll revisit the same example from our previous article on the Option<T> monadโ€”a clean way to handle potential failures without drowning in null checks. By now, you’ve seen how the Option<T> monad lets us chain operations smoothly, sidestepping those tedious null inspections at every step. It’s like assembling a puzzle where missing pieces gracefully halt…

  • Unlock the Differences: When to Use SingleOrDefault vs FirstOrDefault in LINQ

    When to Use SingleOrDefault vs FirstOrDefault? Many developers are familiar with the FirstOrDefault() method in LINQ for C#. However, there’s also SingleOrDefault(). What’s the key difference between them? And why might you choose one over the other instead of always sticking with FirstOrDefault()? Both methods work on collections that implement the IEnumerable<T> interface, allowing you…

  • The only thing you need to reduce nulls: Monad

    Let’s take an example of calculating the delivery estimate given a username. Letโ€™s walk through that journey together and see how a mysterious functional programming concept called a monad can turn nightmare-level null-checks spaghetti into clean, fluent, and safe code. Note: The code snippets here are simplified for clarity and flowโ€”they might not compile straight…

  • A Better Way to Inject Configuration in .NET

    Hey there, fellow .NET developers! If you’ve been building apps for a while, you’re probably no stranger to grabbing settings from appsettings.json using the IConfiguration interface. It’s straightforward and gets the job done. But let’s be realโ€”it’s not without its quirks. Imagine you’re in the middle of a late-night coding session, and a sneaky typo…

  • Multiple authorization handlers for the same requirement in ASP.NET Core

    We saw how we could set up policy-based authorization in our previous article. In this article, weโ€™ll focus on Weโ€™ll use the same scenario as we used for our policy-based authorization: two lounges, one for premium users and one for standard users. Letโ€™s say we want to allow the trial/limited user to experience the standard…

  • Policy-Based Authorization in ASP.NET Core

    What is a policy-based authorization? Unlike role-based authorization, which solely depends on the roles assigned to the users. A policy-based authorization uses requirements that will provide access to a resource when succeeded. A requirement is a collection of data used to evaluate the current user. Why do we need policy-based authorization? With policy-based authorization, we have…

  • Role-based Authorization in ASP.NET Core

    What is role-based authorization? As the name says, role-based authorization authorizes a user based on the role defined to the user. Creating roles and users in ASP.NET Core with Identity For this article, Iโ€™ve created a new ASP.NET Core app in .NET 6 with Individual Accounts as the Authentication type, which will create the roles,…