|
Hey Reader, Ever tried to choose the perfect weapon for each creature in a dark, spooky forest? Silver bullets for werewolves, garlic for vampires, chainsaws for zombies... but what if you had to pick your weapon before entering the forest? Just like that forest adventure, dependency injection in C# can leave you stuck with the wrong tool when you need it most - unless you know about the Factory Pattern! Today, I’m breaking down how the Factory Pattern solves real-world dependency injection issues and when to use it instead of conventional DI. If you’re interested in dynamically switching dependencies at runtime (instead of being locked in beforehand), this is for you! Watch on YouTube 📺Check out the full tutorial to see how the Factory Pattern saves the day 👇 The Problem: Static Dependencies in DIIn typical dependency injection, you declare services upfront, which locks in certain dependencies at compile time. Let’s say we’re working with different services to handle weapons:
Registered in the builder.Services.AddScoped But here’s the catch: this setup only lets you pick one weapon before runtime. That’s fine if you’re only fighting vampires, but if you encounter a zombie with garlic in hand… well, you’re out of luck! The Factory Pattern SolutionEnter the Factory Pattern! With this approach, we can dynamically choose the right weapon at runtime based on the creature we’re facing. Here’s how it’s done: Step 1: Define a Factory Interface
Step 2: Implement the Factory
Step 3: Register Services and Factory in
|
Become a .NET & Blazor expert with weekly tutorials featuring best practices and the latest improvements, right in your inbox.
Hey Reader, .NET 10 just dropped. 🥳 And if you’ve been waiting for a reason to build something new, this is it. In my latest tutorial, I’ll walk you through how to build a complete Web API from scratch in .NET 10, including: Setting up Controllers Using Entity Framework Core 10 Connecting SQL Server Working with DTOs the right way It’s perfect if you’re starting fresh or want to upgrade an older .NET 8 or .NET 9 project to the newest version. Watch the full tutorial here 👇 Take a look, and...
Hey Reader, .NET 10 is almost here, and it brings a big upgrade for validation in Minimal APIs. In this week’s tutorial, I’ll walk you through how validation works now in .NET 10 (using Entity Framework, Scalar, and Minimal APIs). Before, writing validation logic in each endpoint was a pain. You had to manually check for null values, invalid quantities, or missing fields. But in .NET 10, that’s all built-in and super clean. Watch the full tutorial now: 👇 Happy coding! Take care, Patrick
Hey Reader, If you’ve been coding for a while, you’ve probably asked yourself this too: “Am I getting better… or just older?” I’ve been writing software for fifteen years now, and that question still sneaks up on me. But looking back, I’ve realized something: real growth in tech isn’t about frameworks or chasing trends. It’s about staying curious, patient, and keeping your spark alive when everything feels heavy. So I made a new video about it, my 15 biggest lessons from 15 years of coding....