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
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.
So you are using keyed services to make them private? Thats smart
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.
with every video, you grow a longer beard 😂
There is a cycle, but period is still unknown
Agreed with you, they're bloat.
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?
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.
@@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?
.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
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.
What packages do you use that can mock concrete classes?
Moq (virtual keyword), NSubstitute
What about injection of classes that aren't yours with different configurations for example?
What about them?
@@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 ?
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?
Nice video, tkss
You will intend make new courses ?
If programmer has a lot of beard he has a lot of work no time to shave 😅
beard instead of hair)))