Dependency Injection in .NET 8: Keyed Service Registrations 🚀

Sdílet
Vložit
  • čas přidán 15. 06. 2024
  • Today, we want to look at Keyed Service Registrations for Dependency Injection in .NET 8. It's a new feature coming with .NET 8 in November 2023.
    00:00 Introduction
    00:11 Dependency Injection before .NET 8
    00:49 Dependency Injection using Keyed Service Registrations (.NET 8)
    04:24 Conclusion
    🔥 C#/.NET Bootcamp: The Fundamentals & more (Code FRIENDS10 for 10% OFF)*
    claudiobernasconi.ch/dotnet-c...
    📌 .NET Developer Roadmap
    claudiobernasconi.ch/learn-do...
    📌 CZcams Playlists:
    The FREE Blazor Crash Course: • The FREE Blazor Crash ...
    Blazor: • Blazor
    ✅ SUBSCRIBE FOR MORE VIDEOS
    Subscribe: 🔔 claudiobernasconi.ch/Subscribe
    🚀 CONNECT & SUPPORT
    Twitter: / chbernasconic
    Blog: claudiobernasconi.ch
    Newsletter: claudiobernasconi.ch/newsletter
    * Affiliate Link: I earn a small commission at no additional cost
  • Věda a technologie

Komentáře • 8

  • @ClaudioBernasconi
    @ClaudioBernasconi  Před 8 měsíci +1

    Will you use Keyed Services in your .NET applications? Do you think they're useful? 🤔

  • @10Totti
    @10Totti Před 8 měsíci

    Great tutorial thanks!

  • @TheWolverine1984
    @TheWolverine1984 Před 8 měsíci

    2:46 I get the use case, but Unity DI and Autofac had this functionality for a while. Also I don't like the use of a IServiceProvider in the service constructor. This makes it so the controller has intimate knowledge about the implementation type it uses. And if the controller is built to use a specific implementation type, then you might as well just register this specific type in the DI container and resolve it in the constructor.
    IMHO passing the serviceProvider in the constructor defeats the whole point of DI.

    • @ClaudioBernasconi
      @ClaudioBernasconi  Před 8 měsíci

      Thanks for watching. I understand your concern. However, I do believe there is still one more level of abstraction when using the interface type and a key to resolve a service compared to hard-coding the implementation type. Of course, it's all personal preference, and I don't judge anyone finding other solutions, such as using a custom/third-party DI solution. The goal of the video was to talk about what's coming with .NET 8, and I hope I achieved that. 🙂

  • @vivekkaushik9508
    @vivekkaushik9508 Před 8 měsíci

    Thanks for creating these short videos Claudio. Very useful and informative however, I must ask do you know why this feature was created in the first place? What was the necessity of this Keyed service is not clear to me. Can you please provide some MS link reg this?

    • @ClaudioBernasconi
      @ClaudioBernasconi  Před 8 měsíci

      The feature proposal was discussed on GitHub. I believe it's nice to have, but since you aren't forced to use it, you can also ignore it if you don't think it's helpful to you. 😉 github.com/dotnet/runtime/issues/64427

    • @10Totti
      @10Totti Před 8 měsíci

      It allows you to use multiple implementations instead of using one Enumerable.