All Projects β†’ ashleydavis β†’ fusion

ashleydavis / fusion

Licence: MIT License
A simple automated dependency injection library for TypeScript, supporting React class and functional components.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
Batchfile
5799 projects

Projects that are alternatives of or similar to fusion

Kangaru
🦘 A dependency injection container for C++11, C++14 and later
Stars: ✭ 297 (+1550%)
Mutual labels:  ioc, dependency-injection, injection
vesselize
β›΅ A JavaScript IoC container that works seamlessly with Vue.js and React.
Stars: ✭ 22 (+22.22%)
Mutual labels:  ioc, dependency-injection, injection
Reflex
Minimal dependency injection framework for Unity
Stars: ✭ 263 (+1361.11%)
Mutual labels:  ioc, dependency-injection, injection
Tsyringe
Lightweight dependency injection container for JavaScript/TypeScript
Stars: ✭ 2,761 (+15238.89%)
Mutual labels:  ioc, dependency-injection, injection
Container Ioc
Inversion of Control container & Dependency Injection for Javascript and Node.js apps powered by Typescript.
Stars: ✭ 89 (+394.44%)
Mutual labels:  ioc, dependency-injection, injection
Poodinis
A dependency injection framework for D with support for autowiring.
Stars: ✭ 57 (+216.67%)
Mutual labels:  ioc, dependency-injection, injection
Zenject-2019
Dependency Injection Framework for Unity3D
Stars: ✭ 2,567 (+14161.11%)
Mutual labels:  ioc, dependency-injection, injection
DependencyInjector
Lightweight dependency injector
Stars: ✭ 30 (+66.67%)
Mutual labels:  ioc, dependency-injection, injection
React Ioc
Hierarchical Dependency Injection with new React 16 Context API
Stars: ✭ 133 (+638.89%)
Mutual labels:  ioc, dependency-injection, injection
tsdi
Dependency Injection container (IoC) for TypeScript
Stars: ✭ 50 (+177.78%)
Mutual labels:  ioc, dependency-injection, injection
Asp.net MVC5 DDD EF6 IoC
Asp.net C# MVC5, EF6, DDD, IoC
Stars: ✭ 14 (-22.22%)
Mutual labels:  ioc, dependency-injection
AnnotationInject
Compile-time Swift dependency injection annotations
Stars: ✭ 40 (+122.22%)
Mutual labels:  dependency-injection, injection
PopKorn
DI can be simple. Forget about modules and components. Just use it!
Stars: ✭ 139 (+672.22%)
Mutual labels:  dependency-injection, injection
refuel
Lightweight dependency injection engine and DI-driven tools.
Stars: ✭ 21 (+16.67%)
Mutual labels:  dependency-injection, injection
ioc-ts
Inversion of control container on TypeScript
Stars: ✭ 14 (-22.22%)
Mutual labels:  ioc, dependency-injection
vue3-oop
δ½Ώη”¨η±»ε’ŒδΎθ΅–ζ³¨ε…₯写vueη»„δ»Ά
Stars: ✭ 90 (+400%)
Mutual labels:  ioc, dependency-injection
inject
[Archived] See https://github.com/goava/di.
Stars: ✭ 49 (+172.22%)
Mutual labels:  ioc, dependency-injection
SwiftInjection
Dependency Injection framework for Swift
Stars: ✭ 21 (+16.67%)
Mutual labels:  ioc, dependency-injection
di
πŸ›  A full-featured dependency injection container for go programming language.
Stars: ✭ 156 (+766.67%)
Mutual labels:  ioc, dependency-injection
CNeptune
CNeptune improve productivity & efficiency by urbanize .net module with meta-code to lay foundation for frameworks
Stars: ✭ 30 (+66.67%)
Mutual labels:  ioc, injection

Fusion

A simple automated dependency injection library for TypeScript, supporting React class and functional components.

Learn more about Fusion in this blog post:

If you like this project, please star this repo and support my work

Aims

  • To have a simple dependency injection library with minimal configuration that can be used in TypeScript code and with React.

Features

  • Less than 400 lines of code (used to be 300, but you know how it goes, I keep adding extra stuff)
  • Configuration via TypeScript decorators.
  • Injects properties into generic TypeScript class.
  • Injects properties into React class components.
  • Injects parameters into React functional components.
    • Unfortuntely decorators can't be applied to global functions (seems like a big thing missing from TypeScript??) - so the injection approach for functional components doesn't use decorators.
  • Automated dependency injection.
    • Just add mark up and let Fusion do the wiring for you.
  • Detects and breaks circular references (with an error) at any level of nesting.
    • But only when NODE_ENV is not set to "production" (to make it fast in production).
  • Unit tested.

Examples

See the examples sub-directory in this repo for runnable Node.js and React examples.

Read the individual readme files for instructions.

There's also a separate repo with separate examples for React class components and functional components.

Usage

First enable decorators in your tsconfig.json file:

"experimentalDecorators": true

Install the Fusion library:

npm install --save @codecapers/fusion

Import the bits you need:

import { InjectProperty, InjectableClass, InjectableSingleton, injectable } from "@codecapers/fusion";

Create dependencies

Create dependencies that can be injected:

log.ts

//
// Interface to the logging service.
//
interface ILog {
    info(msg: string): void;
}

//
// This is a lazily injected singleton that's constructed when it's injected.
//
@InjectableSingleton("ILog")
class Log implements ILog {
    info(msg: string): void {
        console.log(msg);
    }
}

Note: if you can't get over the magic string, please skip to the last section!

Inject properties into classes

Mark up your class to have dependencies injected:

my-class.ts

import { InjectProperty, InjectableClass } from "@codecapers/fusion";
import { ILog } from "./log";

@InjectableClass()
class MyClass {

    //
    // Injects the logging service into this property.
    //
    @InjectProperty("ILog")
    log!: ILog;

    myFunction() {
        //
        // Use the injected logging service.
        // By the time we get to this code path the logging service 
        // has been automatically constructed and injected.
        //
        this.log.info("Hello world!");
    }

    // ... Other functions and other stuff ...
}

Now instance your injectable class:

import { MyClass } from "./my-class";

// The logging singleton is lazily created at this point.
const myObject = new MyClass(); 

Injected properties are solved during construction and available for use after the consturctor has returned.

So after your class is constructed you can call functions that rely on injected properties:

myObject.myFunction();

Inject parameters into functions

This can be used for injection into React functional components.

Create a functional component that needs dependencies:

my-component.jsx

import React from "react";
import { injectable } from "@codecapers/fusion";

function myComponent(props, context, dependency1, dependency2) {

    // Setup the component, use your dependencies...

    return (
        <div>
            // ... Your JSX goes here ...
        </div>;
    );
}

Wrap your functional component in the injectable higher order component (HOC):

my-component.jsx (extended)

export default injectable(myComponent, ["IDependency1", "IDependency2"]);

The exported component will have the dependencies injected as parameters in the order specified (after props and context of course).

Getting rid of the magic strings

I like to get rid of the magic string by using constants co-located with the dependencies:

log.ts

const ILog_id = "ILog";

//
// Interface to the logging service.
//
interface ILog {
    info(msg: string): void;
}

//
// This is a lazily injected singleton that's constructed when it's injected.
//
@InjectableSingleton(ILog_id)
class Log implements ILog {
    info(msg: string): void {
        console.log(msg);
    }
}

Then use the constant to identify your dependencies:

my-class.ts

@InjectableClass()
class MyClass {

    //
    // Injects the logging service into this property.
    //
    @InjectProperty(ILog_id)
    log!: ILog;

    // ... Other properties and methods ...
}

Have fun! There's more to it than this of course, but getting started is that simple.

See the blog post to learn more.

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