What is Dependency Injection?
Dependency Injection is “a set of software design principles and patterns that enable us to develop loosely coupled code.”
I really would like to contribute more to the MonoBehaviour classes, but currently I see that the code currently smells. I think that introducing of DI will help us to maintain much higher quality of the code overall.
I haven’t seen open source projects so far with DI setup, but I have done such. I can share knowledge/PR on how to use Microsoft.Extensions.DependencyInjection, which is currently the best IOC container.
We could create our code in such a way that we have stateless services, which are very easily testable and injectable. Services will live in the IOC container and be injected, where needed.
I definitely support the usage of DI, once the style-guide is finished it will have some reference to this as we will be avoiding Singletons and most likely opting for ScriptableObjects as our way of doing DI. How this will exactly be implemented is not yet known
What do you mean by relatively simple project? The game may look super simple, but if you have 100+ rows of code in each of your Update() methods with dozens of dependencies that could be decoupled. Then shouldn’t we make our code more maintainable and reusable?
I’m opting for using Extenject (previously called Zenject):
It will take time to setup at first, but once it’s done it will help us have decoupled classes that result to effective unit tests earlier on the project.
Forgive me for digging up the topic but I’m really intrested how the DI in Unity looks like in 2022, I did small reserach, I found: “Extenject”, “UniDI (this is probably continuation of Extenject)”, “VContainer” and can someone tell me what is the best DI now for Unity? Are there any problems with VContainer? because it seeems to be better and faster.
Refer to VContainer and Extenject as a good start.
Both have an excellent implementation of ITickable, IStartable etc allowing you to write logic with monobehaviours.
Let’s face it, monobehaviours where a good idea at the time, but it’s not doing anyone any favors to constantly force things to be monobehaviours when they don’t need to be.