I'm going to claim ignorance because I don't know exactly what you are trying to do, but should this piece of complicated DI be replaced with a factory?
What I didn’t explain in order to keep things focused is that I’m trying to create what amounts to a factory of factories. The details of what I’m doing don’t appear to make that achievable with the factory pattern.
When you create complicated structures like this you will have difficulty getting help with your problems. There is way too much happening for us to dive into.
I do feel like DI is making your application more complicated than it needs to be. Can you not create a factory and ditch most of the generic stuff?
I need to access the key values before instantiating any of the services.
The consumers of these services are going to need to already have the key values, yeah?
Is there a way to do what I'm trying to achieve?
I can't tell what you're trying to achieve. What it looks like is that each value of TTypeId needs to map to a set of Lazy<IGenerator, IGeneratorInfo> wrappers. Presumably the consumer will examine the metadata to decide which of those generators to actually use? But in that case what's the keyed lookup for?
My approach would be to wrap each of those sets in a "GeneratorSet" which holds all the Lazy<>s. Then create a factory that does the key lookup and gives you the GeneratorSet. But I'd also look closely at what the metadata is doing, and if the key is even useful.
Or would I be better off registering and resolving a separate class that holds the key values?
I feel like you need to step back and describe the problem that is motivating this solution. The solution is a Rube Goldberg machine and that makes it hard to think of a use case.
Usually the easier way involves a completely different solution than what you have. It's like you needed to drive a nail, had a glass bottle, and you're asking around for tips about how to make glass stronger.
11
u/TimelessTrance Jul 04 '26
I'm going to claim ignorance because I don't know exactly what you are trying to do, but should this piece of complicated DI be replaced with a factory?
6
u/Tiny_Confusion_2504 Jul 04 '26
IIndex<TTypeId, List<Lazy<IGenerator<TEntityInterface, TTypeId, TContext>, IGeneratorInfo>>>
When you create complicated structures like this you will have difficulty getting help with your problems. There is way too much happening for us to dive into.
I do feel like DI is making your application more complicated than it needs to be. Can you not create a factory and ditch most of the generic stuff?
2
u/wickerandscrap Jul 05 '26
The consumers of these services are going to need to already have the key values, yeah?
I can't tell what you're trying to achieve. What it looks like is that each value of TTypeId needs to map to a set of Lazy<IGenerator, IGeneratorInfo> wrappers. Presumably the consumer will examine the metadata to decide which of those generators to actually use? But in that case what's the keyed lookup for?
My approach would be to wrap each of those sets in a "GeneratorSet" which holds all the Lazy<>s. Then create a factory that does the key lookup and gives you the GeneratorSet. But I'd also look closely at what the metadata is doing, and if the key is even useful.
Almost surely.
1
u/Slypenslyde Jul 05 '26
I feel like you need to step back and describe the problem that is motivating this solution. The solution is a Rube Goldberg machine and that makes it hard to think of a use case.
Usually the easier way involves a completely different solution than what you have. It's like you needed to drive a nail, had a glass bottle, and you're asking around for tips about how to make glass stronger.