---
meta:
 - name: keywords
   description: api typescript node.js documentation DIContext class
---
# DIContext - @tsed/di

## Usage

```typescript
import { DIContext } from "@tsed/di";
```

> See [/packages/di/src/common/domain/DIContext.ts](https://github.com/tsedio/tsed/blob/v8.26.2/packages/di/src/common/domain/DIContext.ts#L0-L0).

## Overview

```ts
class DIContext {
    opts: DIContextOptions;
    [x: string]: any;
    readonly PLATFORM: string;
    constructor(opts: DIContextOptions);
    get logger(): ContextLogger;
    get id(): string;
    get dateStart(): Date;
    get injector(): InjectorService;
    get env(): any;
    get container(): LocalsContainer;
    destroy(): Promise<any>;
    cache<Value = any>(key: string, cb: () => Value): Value;
    cacheAsync<Value = any>(key: string, cb: () => Promise<Value>): Promise<Value>;
    delete(key: any): boolean;
    get<T = any>(key: any): T;
    has(key: any): boolean;
    set(key: any, value: any): this;
}
```

<!-- Description -->

## Description

Execution context for dependency injection operations.

Represents a single execution scope (typically an HTTP request) with its own
scoped provider instances, logger, and lifecycle management. Provides access to
request-scoped services and context-specific data.

### Usage

```typescript
import {DIContext} from "@tsed/di";

const context = new DIContext({id: "req-123"});

context.logger.info("Processing request");
const service = await context.injector.invoke(MyService, context.container);

await context.destroy(); // Cleanup when done
```

<!-- Members -->

## opts

```ts
opts: DIContextOptions;
```

## \[x: string]

```ts
[x: string]: any;
```

## readonly PLATFORM

```ts
readonly PLATFORM: string;
```

## get logger

```ts
get logger(): ContextLogger;
```

Logger attached to the context request.

## get id

```ts
get id(): string;
```

Request id generated by [ContextMiddleware](/ai/api/graphql/typegraphql/types/middlewares/const-context-middleware.md).

::: tip
By default Ts.ED generate uuid like that `uuidv4().replace(/-/gi, ""))`.
Dash are removed to simplify tracking logs in Kibana
:::

::: tip
Request id can by customized by changing the server configuration.

```typescript
@Configuration({
  logger: {
    reqIdBuilder: createUniqId // give your own id generator function
  }
})
class Server {

}
```

:::

## get dateStart

```ts
get dateStart(): Date;
```

## get injector

```ts
get injector(): InjectorService;
```

## get env

```ts
get env(): any;
```

## get container

```ts
get container(): LocalsContainer;
```

The request container used by the Ts.ED DI. It contains all services annotated with `@Scope(ProviderScope.REQUEST)`

## destroy

```ts
destroy(): Promise<any>;
```

## cache

```ts
cache<Value = any>(key: string, cb: () => Value): Value;
```

## cacheAsync

```ts
cacheAsync<Value = any>(key: string, cb: () => Promise<Value>): Promise<Value>;
```

## delete

```ts
delete(key: any): boolean;
```

## get

```ts
get<T = any>(key: any): T;
```

## has

```ts
has(key: any): boolean;
```

## set

```ts
set(key: any, value: any): this;
```
