If it’s the first time you visit my blog, please don’t forget to read my main articles on the subject before to continue this post:
It’s finally time to share the latest version of my Inversion of Control container, which I named Svelto IoC, which I will keep updated from now on. This new version is the current IoC container that Freejam is using for the Unity3D game Robocraft (http://www.robocraftgame.com).
Thanks to the possibility to use the library in production, I could analyse in depth the benefits and disadvantages in using extensively an IoC container on a big project with a medium sized team of programmers. I am preparing an exhaustive article on the subject, but I am not sure when I will be able to publish it, so stay tuned.
The new IoC container is structurally similar to the old one, but has several major differences. In order to use it, a UnityRoot specialised monobehaviour must still be created. The class that implements ICompositionRoot is the Compositon Root of the project. The game object holding the UnityRoot monobehaviour is the game context of the scene.
All the dependencies bound in the Composition Root, will be injected during the Unity Awake period. Dependencies cannot be used until the OnDependenciesInjected function or the Start function (in case of dependencies injected inside monobehaviours) are called. Be careful though, OnDependenciesInjected is called while the injection waterfall is still happening, however injected dependencies are guaranteed to have, on their turn, their dependencies injected. Dependencies are not guaranteed to be injected during the Awake period, therefore you shouldn’t use injected dependencies inside Monobehaviour Awake calls.
Other changes include:
- Monobehaviours that are created by unity after the scene is loaded, don’t need to ask explicitly to fill the dependencies anymore. They will be automatically injected.
- Monobehaviours cannot be injected as dependency anymore (that was a bad idea).
- Dynamically created monobehaviours have always dependencies injected through factories (MonoBehaviourFactory and GameObjectFactory are part of the framework).
- Now all the contracts are always injected through “providers”, this simplifies the code and makes it more solid. Also highlights the importance of providers in this framework.
- A type injection cache has been developed, therefore injecting dependencies of the same type is way faster than it used to be.
- It’s now possible to create new instances for each dependency injected, if the factory MultiProvider is used explicitly.
- You can create your own provider for special cases.
- Improved type safety of the code. It’s not possible anymore to bind contracts to wrong types. For this reason AsSingle() has been substituted by BindSelf().
- Various improvements and bug fixes.
- Dependencies can be injected as weak references automatically.
What is still need to do:
- Improve the documentation and explain the bad practices
- Add the possibility to create hierarchical context and explain why they are necessary
- Add the possibility to inject dependencies by construction, in order to reduce the necessity to hold references.
- Explain how to exploit custom providers.
The new project can be found at this link: https://github.com/sebas77/Svelto-IoC