12.0.6 • Published 8 months ago

@furystack/inject v12.0.6

Weekly downloads
125
License
GPL-2.0
Repository
github
Last release
8 months 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.

12.0.5

9 months ago

12.0.6

8 months ago

12.0.4

9 months ago

12.0.3

9 months ago

12.0.0

11 months ago

12.0.1

11 months ago

12.0.2

10 months ago

11.0.3

1 year ago

11.0.2

1 year ago

11.0.1

1 year ago

11.0.0

1 year ago

10.0.0

1 year ago

9.0.0

1 year ago

8.1.8

1 year ago

8.1.7

1 year ago

8.1.6

1 year ago

8.1.5

1 year ago

8.1.4

1 year ago

8.1.3

1 year ago

8.1.0

2 years ago

8.1.2

2 years ago

8.1.1

2 years ago

8.0.8

2 years ago

8.0.7

2 years ago

8.0.6

2 years ago

8.0.5

2 years ago

8.0.4

2 years ago

8.0.1

2 years ago

8.0.0

2 years ago

8.0.3

2 years ago

8.0.2

2 years ago

7.1.7

2 years ago

7.1.9

2 years ago

7.1.8

2 years ago

7.1.6

2 years ago

7.1.5

2 years ago

7.1.4

2 years ago

7.1.3

3 years ago

7.1.2

3 years ago

7.1.1

3 years ago

7.1.0

3 years ago

7.0.5

3 years ago

7.0.4

3 years ago

7.0.3

3 years ago

7.0.0

3 years ago

7.0.2

3 years ago

7.0.1

3 years ago

6.0.1

3 years ago

6.0.3

3 years ago

6.0.2

3 years ago

6.0.0

3 years ago

5.0.7

3 years ago

5.0.6

3 years ago

5.0.5

3 years ago

5.0.3

3 years ago

5.0.2

3 years ago

5.0.0

4 years ago

4.0.76

4 years ago

4.0.75

4 years ago

4.0.74

4 years ago

4.0.73

4 years ago

4.0.70

4 years ago

4.0.72

4 years ago

4.0.71

4 years ago

4.0.69

4 years ago

4.0.68

4 years ago

4.0.67

4 years ago

4.0.66

4 years ago

4.0.63

4 years ago

4.0.62

4 years ago

4.0.65

4 years ago

4.0.64

4 years ago

4.0.61

4 years ago

4.0.60

4 years ago

4.0.59

4 years ago

4.0.58

4 years ago

4.0.57

4 years ago

4.0.56

4 years ago

4.0.55

4 years ago

4.0.54

4 years ago

4.0.52

4 years ago

4.0.53

4 years ago

4.0.51

4 years ago

4.0.50

4 years ago

4.0.49

4 years ago

4.0.48

4 years ago

4.0.47

4 years ago

4.0.45

4 years ago

4.0.46

4 years ago

4.0.44

5 years ago

4.0.43

5 years ago

4.0.42

5 years ago

4.0.41

5 years ago

4.0.40

5 years ago

4.0.39

5 years ago

4.0.38

5 years ago

4.0.37

5 years ago

4.0.36

5 years ago

4.0.35

5 years ago

4.0.34

5 years ago

4.0.33

5 years ago

4.0.32

5 years ago

4.0.31

5 years ago

4.0.30

5 years ago

4.0.29

5 years ago

4.0.28

5 years ago

4.0.27

5 years ago

4.0.26

5 years ago

4.0.25

5 years ago

4.0.24

5 years ago

4.0.21

5 years ago

4.0.20

5 years ago

4.0.19

5 years ago

4.0.18

5 years ago

4.0.16

5 years ago

4.0.17

5 years ago

4.0.15

5 years ago

4.0.14

5 years ago

4.0.13

5 years ago

4.0.12

5 years ago

4.0.10

5 years ago

4.0.9

5 years ago

4.0.8

5 years ago

4.0.7

5 years ago

4.0.6

5 years ago

4.0.5

5 years ago

4.0.4

6 years ago

4.0.3

6 years ago

4.0.2

6 years ago

4.0.1

6 years ago

4.0.0

6 years ago

3.0.8

6 years ago

3.0.7

6 years ago

3.0.4

6 years ago

3.0.3

6 years ago

3.0.2

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago

2.0.0

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago