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.
Features
- Blazing fast
- IL2CPP Friendly
- Minimal code base
- Contructor injection
[MonoInject]
Property, field and method injection attribute
Performance
10,000 iterations solving a non nested transient dependency.
GC Alloc | More Alloc Than Best | Time | Slower Than Best Best | |
---|---|---|---|---|
Reflex | 235KB | 1x | 5ms | 1x |
VContainer | 512KB | 2x | 13ms | 2.6x |
Zenject | 2355KB | 10x | 48ms | 9.6x |
Installation
Requires Unity 2019+
Install via UPM (using Git URL)
"com.gustavopsantos.reflex": "https://github.com/gustavopsantos/reflex.git?path=/Reflex/Assets/Reflex/#1.0.0"
Install manually (using .unitypackage)
- Download the .unitypackage from releases page.
- 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(IContainer container)
{
container.BindSingleton<int>(42);
container.Bind<IDependencyOne>().To<DependencyOne>().AsTransient();
container.Bind<IDependencyTwo>().To<DependencyTwo>().AsSingleton();
container.BindGenericContract(typeof(SetupAsset<>)).To(
typeof(SetupEnemy),
typeof(SetupLevel)
).AsSingleton();
}
}
MonoBehaviour Injection
Be aware that fields and properties with [MonoInject] are only injected into pre-existing MonoBehaviours within the scene after the SceneManager.sceneLoaded event, which happens after Awake and before Start. See MonoInjector.cs.
public class MonoBehaviourInjection : MonoBehaviour
{
[MonoInject] private readonly IContainer _container;
[MonoInject] public IDependencyOne DependencyOne { get; private set; }
[MonoInject]
private void Inject(IContainer 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 IContainer _container;
private readonly int _answerForLifeTheUniverseAndEverything;
public NonMonoBehaviourInjection(IContainer container, int answerForLifeTheUniverseAndEverything)
{
_container = container;
_answerForLifeTheUniverseAndEverything = answerForLifeTheUniverseAndEverything;
}
}
Contributing
Here’s how we suggest you go about proposing a change to this project:
- Fork this project to your account.
- Create a branch for the change you intend to make.
- Make your changes to your fork.
- Send a pull request from your fork’s branch to our
master
branch.
Using the web-based interface to make changes is fine too, and will help you by automatically forking the project and prompting to send a pull request too.
Author
License
Reflex is licensed under the MIT license, so you can comfortably use it in commercial applications (We still love contributions though).