or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

binding.mdconditional.mdcontainer.mddecorators.mdindex.mdlifecycle.mdmodules.md
tile.json

container.mddocs/

0

# Container Management

1

2

The Container class is the core of InversifyJS, providing dependency injection, service resolution, and binding management functionality. It maintains a registry of service bindings and handles the complete lifecycle of dependency resolution.

3

4

## Container Class

5

6

```typescript { .api }

7

class Container {

8

constructor(containerOptions?: ContainerOptions);

9

10

// Binding management

11

bind<T>(serviceIdentifier: ServiceIdentifier<T>): BindInFluentSyntax<T>;

12

rebind<T>(serviceIdentifier: ServiceIdentifier<T>): BindInFluentSyntax<T>;

13

unbind(serviceIdentifier: ServiceIdentifier): void;

14

unbindAll(): void;

15

isBound(serviceIdentifier: ServiceIdentifier, options?: IsBoundOptions): boolean;

16

17

// Service resolution

18

get<T>(serviceIdentifier: ServiceIdentifier<T>, options?: GetOptions): T;

19

getAll<T>(serviceIdentifier: ServiceIdentifier<T>, options?: GetAllOptions): T[];

20

getNamed<T>(serviceIdentifier: ServiceIdentifier<T>, name: string): T;

21

getAllNamed<T>(serviceIdentifier: ServiceIdentifier<T>, name: string): T[];

22

getTagged<T>(serviceIdentifier: ServiceIdentifier<T>, key: string, value: any): T;

23

getAllTagged<T>(serviceIdentifier: ServiceIdentifier<T>, key: string, value: any): T[];

24

25

// Module management

26

load(...modules: ContainerModule[]): void;

27

unload(...modules: ContainerModule[]): void;

28

29

// Container hierarchy

30

createChild(containerOptions?: ContainerOptions): Container;

31

32

// State management

33

snapshot(): void;

34

restore(): void;

35

}

36

```

37

38

## Container Options

39

40

```typescript { .api }

41

interface ContainerOptions {

42

skipBaseClassChecks?: boolean;

43

autoBindInjectable?: boolean;

44

defaultScope?: BindingScope;

45

}

46

```

47

48

## Basic Container Usage

49

50

```typescript

51

import { Container } from "inversify";

52

53

// Create container with default options

54

const container = new Container();

55

56

// Create container with custom options

57

const customContainer = new Container({

58

skipBaseClassChecks: true,

59

autoBindInjectable: true,

60

defaultScope: "Singleton"

61

});

62

```

63

64

## Service Binding

65

66

### Basic Binding

67

68

```typescript

69

// Bind interface to implementation

70

container.bind<IWarrior>("Warrior").to(Ninja);

71

72

// Bind class to itself

73

container.bind(Katana).toSelf();

74

75

// Bind to constant value

76

container.bind<string>("DatabaseUrl").toConstantValue("mongodb://localhost");

77

78

// Bind to dynamic value

79

container.bind<Date>("CurrentTime").toDynamicValue(() => new Date());

80

```

81

82

### Rebinding Services

83

84

```typescript

85

// Replace existing binding

86

container.rebind<IWarrior>("Warrior").to(Samurai);

87

```

88

89

### Checking Bindings

90

91

```typescript

92

// Check if service is bound

93

if (container.isBound("Warrior")) {

94

const warrior = container.get<IWarrior>("Warrior");

95

}

96

97

// Check with options

98

const isBound = container.isBound("Warrior", {

99

searchParent: false

100

});

101

```

102

103

### Removing Bindings

104

105

```typescript

106

// Remove all bindings for identifier

107

container.unbind("Warrior");

108

109

// Remove specific binding

110

container.unbind("Warrior");

111

```

112

113

## Service Resolution

114

115

### Single Service Resolution

116

117

```typescript

118

// Get single service instance

119

const warrior = container.get<IWarrior>("Warrior");

120

121

// Get with options

122

const warrior = container.get<IWarrior>("Warrior", {

123

skipBaseClassChecks: true

124

});

125

126

// Async resolution

127

const warrior = await container.getAsync<IWarrior>("Warrior");

128

```

129

130

### Multiple Service Resolution

131

132

```typescript

133

// Get all services bound to identifier

134

const weapons = container.getAll<IWeapon>("Weapon");

135

136

// Async resolution of all services

137

const weapons = await container.getAllAsync<IWeapon>("Weapon");

138

```

139

140

## Container Hierarchy

141

142

```typescript

143

// Create child container

144

const childContainer = container.createChild();

145

146

// Child inherits parent bindings but can override

147

childContainer.bind<IWarrior>("Warrior").to(Samurai);

148

149

// Child-specific binding doesn't affect parent

150

const parentWarrior = container.get<IWarrior>("Warrior"); // Ninja

151

const childWarrior = childContainer.get<IWarrior>("Warrior"); // Samurai

152

```

153

154

## Container Snapshots

155

156

```typescript

157

// Save current container state

158

container.snapshot();

159

160

// Make changes

161

container.bind<IWeapon>("Weapon").to(Sword);

162

container.bind<IShield>("Shield").to(Shield);

163

164

// Restore to saved state

165

container.restore();

166

```

167

168

## IsBound Options

169

170

```typescript { .api }

171

interface IsBoundOptions {

172

searchParent?: boolean;

173

}

174

```

175

176

## Resolution Options

177

178

```typescript { .api }

179

interface GetOptions {

180

skipBaseClassChecks?: boolean;

181

}

182

183

interface OptionalGetOptions extends GetOptions {

184

defaultValue?: any;

185

}

186

```

187

188

## Container Events

189

190

The container provides hooks for monitoring binding and resolution events:

191

192

```typescript

193

// Binding created

194

container.onActivation<IWarrior>("Warrior", (context, warrior) => {

195

console.log("Warrior activated");

196

return warrior;

197

});

198

199

// Binding destroyed

200

container.onDeactivation<IWarrior>("Warrior", (warrior) => {

201

console.log("Warrior deactivated");

202

});

203

```

204

205

## Error Handling

206

207

Common container errors and their meanings:

208

209

- **No matching bindings found**: Service identifier not bound

210

- **Ambiguous match**: Multiple bindings found for identifier without disambiguation

211

- **Circular dependency**: Dependency cycle detected during resolution

212

- **Invalid binding**: Binding configuration is invalid or incomplete