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, If you're building apps with Blazor, you've probably asked yourself this: 👉 Should I use SSR, Blazor Server, or WebAssembly? It’s a common question - and I get it a lot. So I made a brand-new YouTube video walking you through how to choose the best one for your app. 🎥 Watch it here: If you're building a Blazor app or planning to do so soon, this should help clear up a lot of confusion. Let me know what you think, and feel free to reply if you have any questions. Take care, Patrick...
Hey Reader, You may have seen the recent blog post from Jimmy Bogard (creator of MediatR and AutoMapper) announcing that these libraries are going commercial. And to be clear, I fully support that. Time is money, and supporting your family always comes first. BUT… In most of my projects, I don’t actually use everything MediatR offers. I usually just need a small part of it, the classic "send a request, get a response" pattern. So I thought: why not build a simple version myself? 👉 That’s...
Hey Reader, In my last video, I showed you how to build a CRUD app using Vertical Slice Architecture and controllers in .NET 9. But… What if you could do the same thing - cleaner, simpler, and without all that controller clutter? 👉 That’s exactly what I show you in my brand-new video: In this one, you’ll learn how to: Use Minimal APIs instead of controllers Keep your Program.cs neat and tidy Stay fully organized with the Vertical Slice Architecture Work with Carter, a super helpful library...