Search Unity

Are there any plans to allow .Net dependency injection frameworks?

Discussion in 'General Discussion' started by zerophase, Dec 22, 2015.

  1. zerophase

    zerophase

    Joined:
    Nov 5, 2013
    Posts:
    25
    I'm mostly wondering about SimpleInjector. I get that I could break my business logic out into a separate assembly and be free to use SimpleInjector as I see fit. Believe I still can't use the latest version.

    So, are there any plans within the year to update to a more recent version of Mono? {I also wouldn't mind having access to C# 6.0 features)

    I remember reading one of the Unity developers blog posts on the new testing tools, and in the discussion it was mentioned there is development going on for pushing MonoBehaviour towards being implemented as in interface, or having an interface underneath it. That would really help out with testing gameobjects. Also wondering if there's any plans to support constructor injection?

    http://stackoverflow.com/questions/...ctor-1-in-unity3d-under-net-3-5?__=319891597#
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    I don't see why you cannot inject code today. We do it all the time ourselves and it works just fine. Of course, you cannot use libraries that use higher than .NET 3.5 profile.

    We're working on upgrading Mono, but we don't have any timelines to share yet.
     
    AndrewGrayGames likes this.
  3. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    It also depends on how you configure your DI. When I setup DI, I like to use boostrap classes and marker interfaces and/or attributes. Then during a one time initialization, use reflection to bind my concrete implementations to the interface types in the DI Container (generally AutoFac but sometimes Unity - the IoC container, not the game engine).

    But, the convention over configuration approach would really break down in Unity because of stripping. Especially on platforms like Android and IOS and IL2CPP in general where every release seems to make the stripping engine more aggressive. You'd need to take care to make sure your desired concrete implementations don't get stripped. You'd either need to use the link.xml to prevent stripping, which defeats the purpose of separating concrete from abstract implementations, or, my preferred method, use a Preserve attribute to make sure you classes don't get stripped, however this attribute doesn't hold any meaning outside of Unity so it does muddy up the code just a bit, but not unreasonably so, if you're using those libraries in non-Unity applications.

    Of course, then there are always third party components that you don't have the capability to add attributes to, in which case you're back to having to use link.xml.
     
  4. AndrewGrayGames

    AndrewGrayGames

    Joined:
    Nov 19, 2009
    Posts:
    3,821
    I would love to be able to use string interpolation; right now I have a FormattedDebugMessage method specifically to avoid having to perform string concatenation to build useful error messages; being able to use an interpolated string would mean I could cut that method from my codebase, as it would be more convenient just to use my existing DebugMessage method, with the interpolated string as the argument.

    String Interpolation looks like this:

    Code (csharp):
    1. // Old Pre-C#6:
    2. var foo = string.Format("Some {0} string", bar);
    3.  
    4. // C#6 Interpolated string:
    5. var foo = $"Some {bar} string";
     
  5. SLASH24

    SLASH24

    Joined:
    Sep 20, 2012
    Posts:
    144
    damn, I fear this thread is way too much technical for me :( !