ASP.NET Core Keyed Services SUCK!

preview_player
Показать описание
ASP.NET Core dependency injection keyed services are not that good, old and gold techniques prevail.

Рекомендации по теме
Комментарии
Автор

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.

lavivida
Автор

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.AddTransient<IA>(p => p.GetKeyedService(temp))
services.AddTransient<IB>(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.

TheOneAndOnlySecrest
Автор

with every video, you grow a longer beard 😂

johnnm
Автор

7:23 - How does DI prevent you from using a strategy pattern? Are you assuming that you NEED to pass strategy in a constructor? why?

alkr
Автор

I always thought the keyed services were kinda useless since we could do those things easily with just a little bit of code, glad someone finally made a video about it.

nemanjadjordjevic
Автор

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?

softwaredev
Автор

What packages do you use that can mock concrete classes?

pedroferreira
Автор

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.

LukeAvedon
Автор

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

andrewiecisa
Автор

Nice video, tkss

You will intend make new courses ?

ItsSalesGabriel
Автор

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

ezmooj