Skip to content

Commit

Permalink
add scoped container in docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Mendele committed May 14, 2021
1 parent c0edbf9 commit c154898
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions docs/guide/usages.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,26 @@ const container = builder.build()
const myService = container.get<MyService>("my service")
```

## Scoped container

Sometimes you want your container so be used only for a given http request.
You can create those kinds of container as following:

```typescript
import {createContainerBuilder, createScopedContainerBuilder, LifeCycle} from '@botflx/dependency-injection-container'

// Create a global container
const globalContainer = createContainerBuilder()
.addFactory('foo', () => 'foo', LifeCycle.Singleton)
.addFactory('bar', () => 'bar', LifeCycle.Singleton)
.build()

// Create a container builder from the global container
const perRequestBuilder = createScopedContainerBuilder(globalContainer)
.addFactory('foobar', provider => `${provider.get('foo')}${provider.get('bar')}`, LifeCycle.Singleton)

// Create a container per request.
const perRequestContainer1 = perRequestBuilder.build()
const perRequestContainer2 = perRequestBuilder.build()
const perRequestContainer3 = perRequestBuilder.build()
```

0 comments on commit c154898

Please sign in to comment.