Dependency Injection

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

3 Likes

It’s not a good definition.
It is actually not even telling us what Dependency Injection is doing.

2 Likes

The inspector is already a great tool for dependency injection IMO.

3 Likes

ScriptableObject is the best injection :stuck_out_tongue:

3 Likes

You do really like them lol

2 Likes

Yep, we’re using the inspector for dependency injection through ScriptableObjects :smile:

There’s zenject framework for dependency injection in Unity GitHub - modesttree/Zenject: Dependency Injection Framework for Unity3D

Also, there’s Extenject for DI in Unity https://github.com/svermeulen/Extenject which is a maintained version of the original creator of Zenject.

But DI isn’t what you need when creating a relatively simple project. Most of the contributors might not know about Zenject at all.

1 Like

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?

3 Likes

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.

Please do not use Scriptable Objects for DI.

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.

Please avoid necroposting. Locking this thread. If you’d like to continue the conversation, please create a new thread.

1 Like