Closed montazher closed 4 years ago
Generally different implementations of the same type are registered and resolved with tags. I'd also advice against using DI containers with things that are considered data in your system, Data
in your case though it's hard to say what it is from the snippet.
@ilyapuchka Hi! Thanks for your response. But tag is required to use it across all the chain of register and resolve actions and this is not so convenient as simple type inference )) Is the idea of drop & create container per each view controller will not work any better?
If you are using auto-injection or auto-wiring the container will pass the tag along when resolving all the dependencies and if they are registered separately with such tag then this definition will be used, otherwise it will fallback to definition without any tag. Other than that you can try to register your different components in different containers and use proper container to resolve them. Common dependencies can go into a common container that you would use to resolve these dependencies manually or it can be linked to others as collaborating container so that it is used to resolve them automatically.
I dig dip into Dependency Injection in Swift via DIP and I faced some collision that I don't understand how to avoid properly. I have same types with different values inside but of course DIP resolves the last one, which had been registered. I'll explain you with code: Here is ViewController & ViewModel 1
The second ViewController and ViewModel are the same except the different name. ViewController2 and ViewModelB. The Composition1 is
and the Composition2 is
The RootComposition is:
So all ViewControllers - the 1 and 2 show "Composition2" in the label because Composition2.configure called the last one. Could somebody give me a helping hand please? What is the proper way to register and reset container?