Skip to content

Minimal dependency injection framework for Unity

License

Notifications You must be signed in to change notification settings

Hlibix/Reflex

 
 

Repository files navigation

Reflex is an Dependency Injection framework for Unity. Making your classes independent of its dependencies, granting better separation of concerns. It achieves that by decoupling the usage of an object from its creation. This helps you to follow SOLID’s dependency inversion and single responsibility principles. Making your project more readable, testable and scalable.

License: MIT
Unity
PullRequests
Releases

Features

  • Blazing fast
  • IL2CPP Friendly
  • Minimal code base
  • Contructor injection
  • [MonoInject] Property, field and method injection attribute

Performance

Resolving ten thousand times a transient dependency with four levels of chained dependencies. See NestedBenchmarkReflex.cs.

Android

MonoIL2CPP
GC Time
Reflex 54KB 10ms
Zenject 464KB 73ms
VContainer 128KB 51ms
GC Time
Reflex 70KB 15ms
Zenject 480KB 77ms
VContainer 128KB 18ms

Windows

MonoIL2CPP
GC Time
Reflex 109KB 1ms
Zenject 900KB 7ms
VContainer 257KB 3ms
GC Time
Reflex 140KB 1ms
Zenject 900KB 7ms
VContainer 257KB 2ms

The performance on IL2CPP (AOT) backend is not so good because the expressions are actually interpreted, unlike Mono (JIT), where they are actually compiled.

I'm investigating whether dealing with IL Reweaving is worth the complexity it brings.

Installation

Requires Unity 2019+

Install via UPM (using Git URL)

"com.gustavopsantos.reflex": "https://github.com/gustavopsantos/reflex.git?path=/Reflex/Assets/Reflex/#1.2.0"

Install manually (using .unitypackage)

  1. Download the .unitypackage from releases page.
  2. Import Reflex.X.X.X.unitypackage

Getting Started

Installing Bindings

Create a MonoInstaller to install your bindings in the project context, and remember to add this component in the ProjectContext prefab, and reference it in the Mono Installers list of the ProjectContext. See ProjectContext.prefab.

public class ProjectInstaller : MonoInstaller
{
    public override void InstallBindings(Container container)
    {
        container.BindSingleton<int>(42);
        container.Bind<IDependencyOne>().To<DependencyOne>().AsTransient();
        container.Bind<IDependencyTwo>().To<DependencyTwo>().AsSingletonLazy();
        container.BindGenericContract(typeof(SetupAsset<>)).To(
            typeof(SetupEnemy),
            typeof(SetupLevel)
        ).AsSingletonNonLazy();
    }
}

MonoBehaviour Injection

Be aware that fields and properties with [MonoInject] are injected only into pre-existing MonoBehaviours within the scene after the SceneManager.sceneLoaded event, which happens after Awake and before Start. See MonoInjector.cs.

If you want to instantiate a MonoBehaviour/Component at runtime and wants injection to happen, use the Instantiate method from Container.

public class MonoBehaviourInjection : MonoBehaviour
{
    [MonoInject] private readonly Container _container;
    [MonoInject] public IDependencyOne DependencyOne { get; private set; }

    [MonoInject]
    private void Inject(Container container, IDependencyOne dependencyOne)
    {
        var dependencyTwo = container
            .Resolve(typeof(IDependencyTwo));
    }

    private void Start()
    {
        var dependencyTwo = _container
            .Resolve(typeof(IDependencyTwo));

        var answerForLifeTheUniverseAndEverything = _container
            .Resolve<int>();

        var setupForDynamicType = _container
            .ResolveGenericContract<SetupScriptableObject>(
                typeof(SetupAsset<>), typeof(Enemy));
    }
}

Non MonoBehaviour Injection

public class NonMonoBehaviourInjection
{
    private readonly Container _container;
    private readonly IDependencyOne _dependencyOne;
    private readonly int _answerForLifeTheUniverseAndEverything;

    public NonMonoBehaviourInjection(Container container, IDependencyOne dependencyOne, int answerForLifeTheUniverseAndEverything)
    {
        _container = container;
        _dependencyOne = dependencyOne;
        _answerForLifeTheUniverseAndEverything = answerForLifeTheUniverseAndEverything;
    }
}

Order of Execution

Events
UnityEngine.SceneManagement.SceneManager.sceneLoaded
MonoBehaviour.Awake
Reflex.Injectors.SceneInjector.Inject
MonoBehaviour.Start

Reflex.Injectors.SceneInjector.Inject injects fields and properties decorated with [MonoInject] attribute.

Author

Twitter

LinkedIn

License

Reflex is licensed under the MIT license, so you can comfortably use it in commercial applications (We still love contributions though).

About

Minimal dependency injection framework for Unity

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%