# curli-di

> Lightweight dependency injection container for JavaScript/TypeScript without using decorators

Latest version **1.0.1** (published 2020-04-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install curli-di
pnpm add curli-di
yarn add curli-di
bun add curli-di
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2020-04-06 |
| First published | 2020-03-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 6.0.0 |
| Dependencies | 0 |
| Unpacked size | 62.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Carlos Juan Craviotto |
| Maintainers | curlirojo |
| Keywords | dependency injection, dependency, injection, container, javascript, typescript, simple di, di, ioc |

## Links

- npm: https://www.npmjs.com/package/curli-di
- Repository: https://github.com/CarlosCraviotto/curli-di
- Homepage: https://github.com/CarlosCraviotto/curli-di#readme
- Issues: https://github.com/CarlosCraviotto/curli-di/issues
- npm.io page: https://npm.io/package/curli-di

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2020-04-06
- 1.0.0-beta.2 (beta) — 2020-04-06
- 1.0.0 — 2020-04-06
- 1.0.0-beta.1 — 2020-04-06
- 1.0.0-beta.0 — 2020-04-06
- 0.0.6 — 2020-04-02
- 0.0.5 — 2020-03-31
- 0.0.4 — 2020-03-31
- 0.0.3 — 2020-03-31
- 0.0.2 — 2020-03-31
- 0.0.1 — 2020-03-31

## README

# Curli-DI


[![Build Status](https://travis-ci.org/CarlosCraviotto/curli-di.svg?branch=master)](https://travis-ci.com/github/CarlosCraviotto/curli-di)
[![Coverage Status](https://coveralls.io/repos/github/CarlosCraviotto/curli-di/badge.svg?branch=master&cach=ff)](https://coveralls.io/github/CarlosCraviotto/curli-di?branch=master)
[![npm version](https://badge.fury.io/js/curli-di.svg)](https://badge.fury.io/js/curli-di)
[![Dependency Status](https://david-dm.org/CarlosCraviotto/curli-di.png)](https://david-dm.org/CarlosCraviotto/curli-di)


A Dependency Injection (DI) library in typescript without using decorators.


## Installation

Install by `npm`

```sh
npm install --save curli-di
```
#### Basic Usage

```typescript
import {DependencyInjection} from "curli-di";
import {Database} from "database";

class Foo {
  constructor(private database: Database) {}
}

const container = new DependencyInjection();

//register the dependencies for the class
container.registerService("dataBase", [], Database);
container.registerService("foo", ["dataBase"], Foo);

//creating the service
const foo = container.get("foo");

```

### Registering external dependencies/properties:

Some time we need to inject properties or other kind of values into our services, for this we use the class ExternalDependencies like here: 

```typescript
import {DependencyInjection, ExternalDependencies} from "curli-di";
import {Database} from "database";

class Foo {
  constructor(private database: Database) {}
}

// Register previous dependencies already instantiated
const externalDependencies = new ExternalDependencies();
externalDependencies.add("dataBaseUser", "root");
externalDependencies.add("dataBasePass", "");

const container = new DependencyInjection(externalDependencies);

//register the dependencies for the class
container.registerService("dataBase", ["@dataBaseUser", "@dataBasePass"], Database);
container.registerService("foo", ["dataBase"], Foo);

//creating the service
const foo = container.get("foo");

```

As this example shows, to access to any external dependency or property we need to add the prefix @.

Also to inject or use a external dependency we can use the registerServiceBuilded method in the container after we initialized it:
  ```typescript
import {DependencyInjection} from "curli-di";

const container = new DependencyInjection();
container.registerServiceBuilded("dataBaseUser", "root");

const dataBaseUser: string = container.get("@dataBaseUser");

 ```

### Registering external dependencies/properties with an object in bulk mode:

```typescript
import {DependencyInjection, ExternalDependencies} from "curli-di";
import {Database} from "database";
import {Oauth} from "oauth";
import {Lang} from "lang";

class Foo {
  constructor(private database: Database) {}
}

// Register previous dependencies already instantiated
const externalDependencies = new ExternalDependencies();
externalDependencies.bulk({
    "dataBaseUser": "root",
    "dataBasePass": "",
    "useOauth": false,
    "languagesSupported": ['en-GB','es-MX', 'zh-HK']
});

const container = new DependencyInjection(externalDependencies);

//register the dependencies for the class
container.registerService("dataBase", ["@dataBaseUser", "@dataBasePass"], Database);
container.registerService("foo", ["dataBase"], Foo);
container.registerService("lang", ["@languagesSupported"], Lang);
container.registerService("oauth", [], Oauth);

if (container.get("@useOauth")) {
    container.get("oauth").start();
}

//creating the service
const foo = container.get("foo");

```

### Circular dependency:

To create a circular dependency between two services, one of the then will receive their dependencies with a different method than the constructor. 

```typescript
import {DependencyInjection} from "curli-di";

class Foo {
    constructor(private database: Database, private taa: Taa) {}
}


class Taa {
    private foo: Foo;

    public injectDependencies(foo: Foo) {
        this.foo = foo;
    }
}

const container = new DependencyInjection();

//registering of dependencies with the classes
container.registerService("foo", ["dataBase", "taa"], Foo);
container.registerService("taa", ["foo"], Taa, false, Taa.prototype.injectDependencies);

//creating the service
const foo = container.get("foo");

```

### Registering services descriptions in different sides of the application:

If we want to split the responsibility of register services in different sides of the application we can perform it using the ExternalServicesRegister class:

```typescript
import {DependencyInjection, ExternalServicesRegister} from "curli-di";
import {Database} from "database";

class Foo {
  constructor(private database: Database) {}
}

// Register previous dependencies already instantiated
const container = new DependencyInjection(),
      externalServicesRegister = new ExternalServicesRegister();


//register the dependencies for the class
externalServicesRegister.registerService("dataBase", ["@dataBaseUser", "@dataBasePass"], Database);
externalServicesRegister.registerService("foo", ["dataBase"], Foo);

container.registerExternalServiceRegister(externalServicesRegister);

//creating the service
const foo = container.get("foo");

```


### Instantiate one or more services in a specific moment (like when we start the app) using autoInit option:

```typescript
import {DependencyInjection} from "curli-di";
import {Database} from "database";
import {Oauth} from "oauth";
import {Lang} from "lang";
import {Foo} from "foo";

const container = new DependencyInjection();

//register the dependencies for the class
container.registerService("dataBase", [], Database, true);
container.registerService("foo", [], Foo);
container.registerService("lang", [], Lang);
container.registerService("oauth", [], Oauth, true);


//start dataBase service and oauth
container.callAllServicesWithAutoInit();

```

---
_Source: https://npm.io/package/curli-di · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
