0

I'd like to have a class that works as a collection for another class. So let's say I have FooBarCollection that has a collection of named FooBarItems. I can bind the FooBarCollection and injecting it works. The problem is that in order to keep things tidy, I'd want to do one injection within the FooBarItem class too, but in order for that to work I'd need to use a DiContainer for the instantiation, and that has too problems:

  • I can't do the DiContainer-based instantiation in the same line where I define the field, so I'd need two lines of code for each item in the collection. I'd like to avoid this kind of redundancy, and depending on remembering both lines of code to be added for every single item.
  • Passing parameters to the DiContainer instantiation isn't type-sensitive.

So is there some way that:

  • I could use Zenject for to inject the FooBarCollection
  • Have each FooBarItem under FooBarCollection to be added with a single line of code, while still being able to inject one dependency in the FoobarItem ?

Any ideas?

  • As you are talking about a specific asset I could not help, but just giving my 5 cents. Doing your DI through plain code and just exposing what is necessary to answer the unity message isn't an option? Or maybe use the unity editor as your Injector, make a scriptable object as your collection of plain classes, and then add them through APIs – Nefisto Nov 29 '22 at 13:25
  • These are not items I want to serialize in a scriptable object, and I don't think that would even solve the problem as I'd still want the injection to work within those items. I'm not sure what you mean by doing "DI through plain code and just exposing what is necessary to answer the unity message". But thanks for the input in any case. – JeffersonTD Nov 30 '22 at 07:10

1 Answers1

0

I am afraid I do not understand the specific situation.

It fulfill your wishes Bind an empty FooBarCollection and a Factory for FooBarItem, then call the Factory from the Spawner class to add elements to the FooBarCollection , I think.

Zenject factory Document is following. https://github.com/modesttree/Zenject/blob/master/Documentation/Factories.md

And then, SubConatiner Document case is similar in some ways to yours maybe.
https://github.com/modesttree/Zenject/blob/master/Documentation/SubContainers.md