# advanced-service-locator

> Object oriented ServiceLocator for use with TypeScript or JS

Latest version **1.0.0-dev.1** (published 2022-05-05) · ISC license · 0 weekly downloads

## Install

```sh
npm install advanced-service-locator
pnpm add advanced-service-locator
yarn add advanced-service-locator
bun add advanced-service-locator
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0-dev.1 |
| Published | 2022-05-05 |
| First published | 2022-05-05 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 14.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Roman Gaikov |
| Maintainers | capricorngaikov |
| Keywords | service, service locator, decoupling, flexible, extendable, design pattern, object oriented |

## Links

- npm: https://www.npmjs.com/package/advanced-service-locator
- npm.io page: https://npm.io/package/advanced-service-locator

## Recent versions

- 1.0.0-dev.1 (latest) — 2022-05-05

## README

![github CI](https://github.com/Gaikov/service-locator/actions/workflows/node.js.yml/badge.svg)

# advanced-service-locator
Class based ServiceLocator for use with TypeScript or JS.

[More about ServiceLocator pattern](https://en.wikipedia.org/wiki/Service_locator_pattern)

### Install
```text
npm i advanced-service-locator
```

### Import
```TypeScript
//es5
const {ServiceLocator, ILocatable, ILocator} = require("advanced-service-locator");
//es6+ and ts
import {ServiceLocator, ILocatable, ILocator} from "advanced-service-locator";
```

### Example classes
```TypeScript
class TestClass implements ILocatable {

    init(locator: ILocator): void {
        //will be called after instantiation
    }

    getMessage(): string {
        return "Hello from ServiceLocator!";
    }
}

class TestClassCustom extends TestClass {
    getMessage(): string {
        return "This is custom message!";
    }
}
```

### Locate an instance
```TypeScript
const locator = new ServiceLocator();
const a = locator.locate(TestClass);
```

### Class mapping
```TypeScript
const locator = new ServiceLocator();
locator.mapClass(TestClass).to(TestClassCustom);

const a = locator.locate(TestClass);

console.log(a.getMessage()); //will output "This is custom message!"
```

### Map class as a singleton
```TypeScript
const locator = new ServiceLocator();
locator.mapClass(TestClass).asSingleton();

const a = locator.locate(TestClass);
const b = locator.locate(TestClass);
console.log(a === b); //will output "true"
```

### Recommend to use as a singleton
```TypeScript
class Locator {
    static readonly instance = new ServiceLocator();
}

const a = Locator.instance.locate(TestClass);
```

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