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.
- Blazing fast
- IL2CPP Friendly
- Minimal code base
- Contructor injection
[MonoInject]
Property, field and method injection attribute
Resolving ten thousand times a transient dependency with four levels of chained dependencies. See NestedBenchmarkReflex.cs.
Mono | IL2CPP | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
Mono | IL2CPP | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
The performance on
IL2CPP (AOT)
backend is not so good because the expressions are actually interpreted, unlikeMono (JIT)
, where they are actually compiled.
I'm investigating whether dealing with IL Reweaving is worth the complexity it brings.
Requires Unity 2019+
"com.gustavopsantos.reflex": "https://github.com/gustavopsantos/reflex.git?path=/Reflex/Assets/Reflex/#1.2.0"
- Download the .unitypackage from releases page.
- Import Reflex.X.X.X.unitypackage
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();
}
}
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));
}
}
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;
}
}
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.
Reflex is licensed under the MIT license, so you can comfortably use it in commercial applications (We still love contributions though).