Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Dependency Injection

Discussion in 'Open Projects' started by stanislavchankov94, Oct 6, 2020.

?

Would you like to work with decoupled, stateless modules to be provided via IOC Container?

  1. Definetly

    63.4%
  2. No

    19.5%
  3. What is Dependency Injection?

    17.1%
Thread Status:
Not open for further replies.
  1. stanislavchankov94

    stanislavchankov94

    Joined:
    Aug 25, 2020
    Posts:
    2
    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.
     
    Last edited: Oct 6, 2020
  2. MileyUnity

    MileyUnity

    Joined:
    Jan 3, 2020
    Posts:
    92
    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. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,095
    It's not a good definition.
    It is actually not even telling us what Dependency Injection is doing.
     
    trul and kcastagnini like this.
  4. kcastagnini

    kcastagnini

    Joined:
    Dec 14, 2019
    Posts:
    61
    The inspector is already a great tool for dependency injection IMO.
     
    MileyUnity, Neonage and MUGIK like this.
  5. Neonage

    Neonage

    Joined:
    May 22, 2020
    Posts:
    288
    ScriptableObject is the best injection :p
     
  6. kcastagnini

    kcastagnini

    Joined:
    Dec 14, 2019
    Posts:
    61
    You do really like them lol
     
    cirocontinisio and Neonage like this.
  7. MileyUnity

    MileyUnity

    Joined:
    Jan 3, 2020
    Posts:
    92
    Yep, we're using the inspector for dependency injection through ScriptableObjects :D
     
  8. pmikstacki

    pmikstacki

    Joined:
    Aug 26, 2020
    Posts:
    2
  9. MUGIK

    MUGIK

    Joined:
    Jul 2, 2015
    Posts:
    482
    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.
     
    Last edited: Oct 12, 2020
    trul likes this.
  10. stanislavchankov94

    stanislavchankov94

    Joined:
    Aug 25, 2020
    Posts:
    2
    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?
     
    luispedrofonseca, GhAyoub and Neonage like this.
  11. GhAyoub

    GhAyoub

    Joined:
    Dec 7, 2018
    Posts:
    10
  12. bartix96pl

    bartix96pl

    Joined:
    May 31, 2019
    Posts:
    7
    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.
     
  13. TheNullReference

    TheNullReference

    Joined:
    Nov 30, 2018
    Posts:
    268
    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.
     
  14. TreyK-47

    TreyK-47

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    1,836
    Please avoid necroposting. Locking this thread. If you'd like to continue the conversation, please create a new thread.
     
    MileyUnity likes this.
Thread Status:
Not open for further replies.