All Projects → gustavopsantos → Reflex

gustavopsantos / Reflex

Licence: MIT license
Minimal dependency injection framework for Unity

Programming Languages

C#
18002 projects

Projects that are alternatives of or similar to Reflex

Container Ioc
Inversion of Control container & Dependency Injection for Javascript and Node.js apps powered by Typescript.
Stars: ✭ 89 (-66.16%)
Mutual labels:  ioc, dependency-injection, container, injection, di
vesselize
⛵ A JavaScript IoC container that works seamlessly with Vue.js and React.
Stars: ✭ 22 (-91.63%)
Mutual labels:  ioc, dependency-injection, injection, ioc-container, di
Hiboot
hiboot is a high performance web and cli application framework with dependency injection support
Stars: ✭ 150 (-42.97%)
Mutual labels:  ioc, dependency-injection, container, di
CNeptune
CNeptune improve productivity & efficiency by urbanize .net module with meta-code to lay foundation for frameworks
Stars: ✭ 30 (-88.59%)
Mutual labels:  ioc, container, injection, di
waiter
Dependency injection, Inversion of control container for rust with compile time binding.
Stars: ✭ 71 (-73%)
Mutual labels:  ioc, dependency-injection, container, di
inject
[Archived] See https://github.com/goava/di.
Stars: ✭ 49 (-81.37%)
Mutual labels:  ioc, dependency-injection, ioc-container, di
React Ioc
Hierarchical Dependency Injection with new React 16 Context API
Stars: ✭ 133 (-49.43%)
Mutual labels:  ioc, dependency-injection, injection, ioc-container
DependencyInjector
Lightweight dependency injector
Stars: ✭ 30 (-88.59%)
Mutual labels:  ioc, dependency-injection, injection, ioc-container
di
🛠 A full-featured dependency injection container for go programming language.
Stars: ✭ 156 (-40.68%)
Mutual labels:  ioc, dependency-injection, ioc-container, di
Tsyringe
Lightweight dependency injection container for JavaScript/TypeScript
Stars: ✭ 2,761 (+949.81%)
Mutual labels:  ioc, dependency-injection, container, injection
Singularity
A extremely fast ioc container for high performance applications
Stars: ✭ 63 (-76.05%)
Mutual labels:  ioc, dependency-injection, ioc-container, di
Typhoon
Powerful dependency injection for Objective-C ✨✨ (https://PILGRIM.PH is the pure Swift successor to Typhoon!!)✨✨
Stars: ✭ 2,711 (+930.8%)
Mutual labels:  ioc, dependency-injection, ioc-container, di
Container
A lightweight yet powerful IoC container for Go projects
Stars: ✭ 160 (-39.16%)
Mutual labels:  ioc, dependency-injection, container, ioc-container
Kangaru
🦘 A dependency injection container for C++11, C++14 and later
Stars: ✭ 297 (+12.93%)
Mutual labels:  ioc, dependency-injection, injection, ioc-container
Poodinis
A dependency injection framework for D with support for autowiring.
Stars: ✭ 57 (-78.33%)
Mutual labels:  ioc, dependency-injection, injection, ioc-container
di
🐑 A flexible dependency injection container; It is an implementation of PSR-11
Stars: ✭ 20 (-92.4%)
Mutual labels:  container, injection, ioc-container, di
typeioc
Dependency injection container for typescript / javascript
Stars: ✭ 32 (-87.83%)
Mutual labels:  ioc, container, injection
ashley
Ashley is a dependency injection container for JavaScript.
Stars: ✭ 23 (-91.25%)
Mutual labels:  ioc, dependency-injection, ioc-container
Python Dependency Injector
Dependency injection framework for Python
Stars: ✭ 1,203 (+357.41%)
Mutual labels:  ioc, dependency-injection, ioc-container
iocgo
A lightweight Inversion of Control (IoC) (Dependency Injection) container for Golang
Stars: ✭ 36 (-86.31%)
Mutual labels:  ioc, dependency-injection, ioc-container

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
  • [Inject] 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=/Assets/Reflex/#3.5.1"

Install manually (using .unitypackage)

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

Getting Started

Installing Bindings

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

public class ProjectInstaller : Installer
{
    public override void InstallBindings(Container container)
    {
        container.BindInstance<int>(42);
        container.BindTransient<IDependencyOne, DependencyOne>();
        container.BindSingleton<IDependencyTwo, DependencyTwo>();
    }
}

MonoBehaviour Injection

Be aware that fields and properties with [Inject] 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
{
    [Inject] private readonly Container _container;
    [Inject] public IDependencyOne DependencyOne { get; private set; }

    [Inject]
    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>();
    }
}

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 when a Scene is Loaded

Events
SceneContext.Awake(DefaultExecutionOrder(-10000))
Reflex.Injectors.SceneInjector.Inject
MonoBehaviour.Awake
MonoBehaviour.Start

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

Contexts

Project Context

A single prefab named ProjectContext that should live inside a Resources folder and should contain a ProjectContext component attached

Non-Obligatory to have

Scene Context

A single root gameobject per scene that should contain a SceneContext component attached

Non-Obligatory to have, but scenes without it wont be injected

Bindings

Bind Function

Binds a function to a type. Every time resolve is called to this binding, the function binded will be invoked.

Bind Instance

Binds a object instance to a type. Every time resolve is called, this instance will be returned.

Instances provided by the user, since not created by Reflex, will not be disposed automatically, we strongly recomend using BindSingleton.

Bind Transient

Binds a factory. Every time the resolve is called, a new instance will be provided.

Instances will be disposed once the container that provided the instances are disposed.

Bind Singleton

Binds a factory. Every time the resolve is called, the same instance will be provided.

The instance will be disposed once the container that provided the instance are disposed.

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).

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].