Giter Site home page Giter Site logo

sisusco / init-args-lite Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 411 KB

Init(args) Lite extends the MonoBehaviour class with the ability to receive arguments during initialization. It is a free, more limited version of the Init(args) dependency injection framework.

License: MIT License

C# 100.00%

init-args-lite's Introduction

init-args-lite

What is Init(args) Lite?

Init(args) Lite is a free version of the Init(args) dependency injection framework. It extends the MonoBehaviour class with the ability to receive arguments during initialization.

You'll be able to continue using all the same commands you're used to - AddComponent and Instantiate - just with the added ability to pass arguments as well.

This small but significant change means that your components are no longer closed islands. Things that used to be pretty much impossible to do, like writing unit tests for them, are suddenly easy and frictionless.

The arguments will be received by components before the Awake and OnEnable events, so you don't have to avoid using them, or clutter them with null-checking.

Reflection-Free

Init(args) Lite works using pure dependency injection, without any reflection; generic methods and interfaces are used to provide strongly typed pipelines through which dependencies can be delivered.

This approach has various benefits, such as solid performance compared to reflection-based dependency injection solutions, and errors being caught as early as possible at compile-time.

Features

  • Instantiate with upto twelve arguments.
  • AddComponent with upto twelve arguments.
  • x != Null - easily test if an interface type variable contains a null or destroyed reference.

Example

Component

public class TextDisplayer : MonoBehaviour<StringEvent, Text>
{
    [SerializeField] StringEvent stringEvent;
    [SerializeField] Text text;
 
    protected override void Init(StringEvent stringEvent, Text text) // <- dependencies received here
    {
        this.stringEvent = stringEvent;
        this.text = text;
    }
 
    void OnEnable() => stringEvent.AddListener(OnEventRaised);
    void OnDisable() => stringEvent.RemoveListener(OnEventRaised);
    void OnEventRaised(string value) => text.text = value;
}

Test

[SetUp]
public void Setup()
{
    stringEvent = new StringEvent();
    var gameObject = new GameObject();
    text = gameObject.AddComponent<Text>();
    gameObject.AddComponent(out textDisplayer, stringEvent, text);  // <- dependencies injected here
}
 
[Test]
public void RaisingStringEventUpdatesDisplayedText()
{
    stringEvent.Raise("Test");
    Assert.AreEqual("Test", text.text);
}

Links

init-args-lite's People

Contributors

timo-sisus avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.