gid

get-it-di

Typescript Dependency Injection Library

Showing:

Popularity

Downloads/wk

9

GitHub Stars

8

Maintenance

Last Commit

2mos ago

Contributors

1

Package

Dependencies

0

License

Type Definitions

Built-In

Tree-Shakeable

No?

Categories

Readme

npm

get-it-di

Typescript Dependency Injection Library

Installation

Install by npm:

npm install get-it-di

Install by yarn:

yarn add get-it-di

Api:

Create Container:

import {ContainerFactory} from "get-it-di";

const container = ContainerFactory.create();

register:

class Car {
    constructor(public name: string) {
    }
}

container.register<Car>(Car, () => new Car("Car1"))

// or you can register by Key
container.register<Car>("CAR_KEY", () => new Car("Car2"))

const resolvedCar = container.resolve(Car)
console.log(resolvedCar.name)
// output: Car1

const resolvedCarByKey = container.resolve<Car>("CAR_KEY")
console.log(resolvedCarByKey.name)
// output: Car2

By using this method, you'll get a new instance of the class in each resolving call

registerSingleton:

class Car {
    constructor(public name: string) {
    }
}

container.registerSingleton<Car>(Car, new Car("1"))

const resolvedCar = container.resolve(Car)
console.log(resolvedCar.name)
// output: Car1

By using this method, you'll get the same instance of the class in each resolving call. This method requires a direct object/value.

registerLazySingleton:

class Car {
    constructor(public name: string) {
    }
}

container.registerLazySingleton<Car>(Car, () => new Car("1"))

const resolvedCar = container.resolve(Car)
console.log(resolvedCar.name)
// output: Car1

By using this method, you'll get the same instance of the class in each resolving call.

Interfaces:

class ApiClient {
    
}

interface IUserRepository {
    get(id: string): User;
}

class UserRepository implements IUserRepository {

    constructor(private api: ApiClient) {
    }

    get(id: string): User {
    }
}


// Register
container.registerLazySingleton<ApiClient>(ApiClient, () => new ApiClient())

container.registerLazySingleton<IUserRepository>(
    "USER_REPOSITORY",
    (c) => {
        return new UserRepository(c.resolve<ApiClient>(ApiClient))
    }
)


// Resolve
const userRepository = container.resolve<IUserRepository>("USER_REPOSITORY")

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100