11.0.2 • Published 13 days ago

@furystack/inject v11.0.2

Weekly downloads
125
License
GPL-2.0
Repository
github
Last release
13 days ago

@furystack/inject

dependency injection provider for FuryStack

Injector

Injectors act as containers, they are responsible for creating / retrieving service instances based on the provided Injectable metadata. You can create an injector with simply instantiating the class

const myInjector = new Injector()

You can organize your injector(s) in trees by creating child injectors. You can use the children and services with scoped lifetime for contextual services.

const childInjector = myInjector.createChild({ owner: 'myCustomContext' })

Injectable

Creating an Injectable service from a class

You can create an injectable service from a plain class when decorating with the @Injectable() decorator.

@Injectable({
  /** Injectable options */
})
export class MySercive {
  /** ...service implementation... */

  constructor(s1: OtherInjectableService, s2: AnotherInjectableService) {}
}

The constructor parameters (s1: OtherInjectableService and s2: AnotherInjectableService) should be also decorated and will be resolved recursively.

Lifetime

You can define a specific Lifetime for Injectable services on the decorator

@Injectable({
  lifetime: 'transient',
})
export class MySercive {
  /** ...service implementation... */
}

The lifetime can be

  • transient - A new instance will be created each time when you get an instance
  • scoped - A new instance will be created if it doesn't exist on the current scope. Can be useful for injectable services that can be used for contextual data.
  • singleton - A new instance will be created only if it doesn't exists on the root injector. It will act as a singleton in other cases.

Injectables can only depend on services with longer lifetime, e.g. a transient can depend on a singleton, but inversing it will throw an error

Retrieving your service from the injector

You can retrieve a service by calling

const service = myInjector.getInstance(MySercive)

Explicit instance setup

There are cases that you have to set a service instance explicitly. You can do that in the following way

class MyService {
  constructor(public readonly foo: string)
}

myInjector.setExplicitInstance(new MyService('bar'))

Extension methods

A simple injector can easily extended from 3rd party packages with extension methods, just like the FuryStack packages. These extension methods usually provides a shortcut of an instance or sets up a preconfigured explicit instance of a service. You can build clean and nice fluent API-s in that way - you can check this logger extension method for the idea

A few things to care about

Circular imports: If two of your services are importing each other, one of them will be ignored by CommonJs. Typescript won't complain at compile time, but if you get this: Uncaught TypeError: SomeService is not a constructor - you should start reviewing how your injectables depends on each other. There is also a limitation by design: A service can depend only a service with a higher or equal lifetime then it's lifetime. That means a singleton can not depend on a transient or scoped service - you should get an exception at runtime if you try it.

11.0.2

13 days ago

11.0.1

1 month ago

11.0.0

2 months ago

10.0.0

2 months ago

9.0.0

2 months ago

8.1.8

2 months ago

8.1.7

3 months ago

8.1.6

3 months ago

8.1.5

4 months ago

8.1.4

4 months ago

8.1.3

5 months ago

8.1.0

9 months ago

8.1.2

6 months ago

8.1.1

7 months ago

8.0.8

9 months ago

8.0.7

9 months ago

8.0.6

10 months ago

8.0.5

12 months ago

8.0.4

1 year ago

8.0.1

1 year ago

8.0.0

1 year ago

8.0.3

1 year ago

8.0.2

1 year ago

7.1.7

1 year ago

7.1.9

1 year ago

7.1.8

1 year ago

7.1.6

1 year ago

7.1.5

1 year ago

7.1.4

1 year ago

7.1.3

1 year ago

7.1.2

2 years ago

7.1.1

2 years ago

7.1.0

2 years ago

7.0.5

2 years ago

7.0.4

2 years ago

7.0.3

2 years ago

7.0.0

2 years ago

7.0.2

2 years ago

7.0.1

2 years ago

6.0.1

2 years ago

6.0.3

2 years ago

6.0.2

2 years ago

6.0.0

2 years ago

5.0.7

2 years ago

5.0.6

2 years ago

5.0.5

2 years ago

5.0.3

2 years ago

5.0.2

2 years ago

5.0.0

2 years ago

4.0.76

3 years ago

4.0.75

3 years ago

4.0.74

3 years ago

4.0.73

3 years ago

4.0.70

3 years ago

4.0.72

3 years ago

4.0.71

3 years ago

4.0.69

3 years ago

4.0.68

3 years ago

4.0.67

3 years ago

4.0.66

3 years ago

4.0.63

3 years ago

4.0.62

3 years ago

4.0.65

3 years ago

4.0.64

3 years ago

4.0.61

3 years ago

4.0.60

3 years ago

4.0.59

3 years ago

4.0.58

3 years ago

4.0.57

3 years ago

4.0.56

3 years ago

4.0.55

3 years ago

4.0.54

3 years ago

4.0.52

3 years ago

4.0.53

3 years ago

4.0.51

3 years ago

4.0.50

3 years ago

4.0.49

3 years ago

4.0.48

3 years ago

4.0.47

3 years ago

4.0.45

3 years ago

4.0.46

3 years ago

4.0.44

3 years ago

4.0.43

3 years ago

4.0.42

3 years ago

4.0.41

4 years ago

4.0.40

4 years ago

4.0.39

4 years ago

4.0.38

4 years ago

4.0.37

4 years ago

4.0.36

4 years ago

4.0.35

4 years ago

4.0.34

4 years ago

4.0.33

4 years ago

4.0.32

4 years ago

4.0.31

4 years ago

4.0.30

4 years ago

4.0.29

4 years ago

4.0.28

4 years ago

4.0.27

4 years ago

4.0.26

4 years ago

4.0.25

4 years ago

4.0.24

4 years ago

4.0.21

4 years ago

4.0.20

4 years ago

4.0.19

4 years ago

4.0.18

4 years ago

4.0.16

4 years ago

4.0.17

4 years ago

4.0.15

4 years ago

4.0.14

4 years ago

4.0.13

4 years ago

4.0.12

4 years ago

4.0.10

4 years ago

4.0.9

4 years ago

4.0.8

4 years ago

4.0.7

4 years ago

4.0.6

4 years ago

4.0.5

4 years ago

4.0.4

5 years ago

4.0.3

5 years ago

4.0.2

5 years ago

4.0.1

5 years ago

4.0.0

5 years ago

3.0.8

5 years ago

3.0.7

5 years ago

3.0.4

5 years ago

3.0.3

5 years ago

3.0.2

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.0.0

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago