ASP.NET Core Keyed Services SUCK!

แชร์
ฝัง
  • เผยแพร่เมื่อ 7 ก.ย. 2024
  • ASP.NET Core dependency injection keyed services are not that good, old and gold techniques prevail.
    Support my Work 🤝 / raw_coding
    Buy my Courses 📚 learning.raw-c...
    Buy my Merch 🛒 shop.raw-codin...
    Join the Community 💬 / discord
    Tweet Tweet 📣 / anton_t0shik
    Tune in LIVE! 🎥 / raw_coding

ความคิดเห็น • 20

  • @TheOneAndOnlySecrest
    @TheOneAndOnlySecrest วันที่ผ่านมา +1

    One use case I have for them is to expose multiple interfaces on the same instance without having the actual instance accessible from the DI.
    So basically what I do for some parts:
    class AorB : IA, IB { ... }
    var temp = new object();
    services.AddKeyedSingleton(temp)
    services.AddTransient(p => p.GetKeyedService(temp))
    services.AddTransient(p => p.GetKeyedService(temp))
    In my logic I actually replicated the options from AutoFac using service registrations, but I guess you get the point. In my case I only want to have IA or IB retrievable but not AorB but this is the class implementing both interfaces so to have a single instance I need to have it accessible from the DI.
    Same goes for decorating services and other things.

    • @RawCoding
      @RawCoding  วันที่ผ่านมา

      So you are using keyed services to make them private? Thats smart

  • @lavivida9328
    @lavivida9328 25 วันที่ผ่านมา +1

    The Keyed Service seems to be useful in the following scenario:
    When an existing service (A) is registered as a single instance (ISvc), but later on, another implementation is registered with the same interface (ISvc) (B), and depending on the situation, you need to provide either A or B.
    In such cases, you could change the existing services to be registered as Keyed Services and expose a Resolver that implements ISvc as a regular service.

  • @johnnm3207
    @johnnm3207 หลายเดือนก่อน +15

    with every video, you grow a longer beard 😂

    • @ivank6486
      @ivank6486 หลายเดือนก่อน

      There is a cycle, but period is still unknown

  • @RomainQ
    @RomainQ หลายเดือนก่อน +2

    Agreed with you, they're bloat.

  • @softwaredev5459
    @softwaredev5459 หลายเดือนก่อน +1

    Hello @RawCoding. Have you ever work with Java/Spring Boot? Your C#/.NET tutorials looks very good.(Just watched 1 video about DI) So i am a junior, i also follow spring boot tutorials. But i can't decide which framework i should choose and stick with it. Have any suggestions about that?

    • @RawCoding
      @RawCoding  หลายเดือนก่อน +1

      I would recommend c#, but have a look at what kinda job you want: remote/office in your country/outside; see what jobs are available and make a decision.

    • @softwaredev5459
      @softwaredev5459 หลายเดือนก่อน +1

      ​@@RawCoding Is .NET usually using in Microservice projects Or usually modular monolith in real world? I don't see many microservice projects with .NET on tutorials. Is Spring boot more efficient on this topic for enterprise apps?

    • @RawCoding
      @RawCoding  28 วันที่ผ่านมา +1

      .net can be used for anything, web, desktop, etc… architectural choices are not bound to a language.
      Pick a tool that is closest to how you like to think and solve problems

  • @LukeAvedon
    @LukeAvedon หลายเดือนก่อน

    Nice video! My understanding is they just put keyed services in, to match the qualified services (I think) in Java Spring. To have one up on the @Bean people.

  • @pedroferreira9234
    @pedroferreira9234 หลายเดือนก่อน +1

    What packages do you use that can mock concrete classes?

  • @andrewiecisa2907
    @andrewiecisa2907 หลายเดือนก่อน +1

    What about injection of classes that aren't yours with different configurations for example?

    • @RawCoding
      @RawCoding  หลายเดือนก่อน

      What about them?

    • @andrewiecisa2907
      @andrewiecisa2907 หลายเดือนก่อน +1

      @@RawCoding imagine you want to register a third party class with 2 different configurations. isn't it an example where it's quite convenient to use keyed services ?

    • @RawCoding
      @RawCoding  หลายเดือนก่อน

      Sure you can use keyed services for that, id would rather use a factory with enum, also what about a third configuration that you would like to add without re deploying, then its reading from config and resolving based on dynamic key, not static markup.
      Solving these problems is easier without having to think oh how do I involve DI in this?

  • @ItsSalesGabriel
    @ItsSalesGabriel หลายเดือนก่อน

    Nice video, tkss
    You will intend make new courses ?

  • @ezmooj
    @ezmooj หลายเดือนก่อน

    If programmer has a lot of beard he has a lot of work no time to shave 😅

  • @user-qf2xk1fg6e
    @user-qf2xk1fg6e หลายเดือนก่อน

    beard instead of hair)))