Skip to content

Commit

Permalink
Document an example of injecting primitives (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xapphire13 authored Oct 18, 2020
1 parent 50f342f commit 430aaa0
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ constructor injection.
- [Full examples](#full-examples)
- [Example without interfaces](#example-without-interfaces)
- [Example with interfaces](#example-with-interfaces)
- [Injecting primitive values (Named injection)](#injecting-primitive-values-named-injection)
- [Non goals](#non-goals)
- [Contributing](#contributing)

Expand Down Expand Up @@ -617,6 +618,30 @@ const client = container.resolve(Client);
// client's dependencies will have been resolved
```

## Injecting primitive values (Named injection)
Primitive values can also be injected by utilizing named injection
```typescript
import {singleton, inject} from "tsyringe";

@singleton()
class Foo {
private str: string;
constructor(@inject("SpecialString") value: string) {
this.str = value;
}
}

// some other file
import "reflect-metadata";
import {container} from "tsyringe";
import {Foo} from "./foo";

const str = "test";
container.register("SpecialString", { useValue: str });

const instance = container.resolve(Foo);
```

# Non goals
The following is a list of features we explicitly plan on not adding:
- Property Injection
Expand Down

0 comments on commit 430aaa0

Please sign in to comment.