Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ Runtime exports:
- `Inject`
- `Token`

`Container` exposes the runtime API for resolution and low-level registration,
including `of()`, `get()`, `set()`, `has()`, `remove()`, and `reset()`.

Type-only exports:

- `Constructable` / `AbstractConstructable`
Expand Down Expand Up @@ -239,16 +242,64 @@ This is especially useful in tests.

Registers or replaces a bound value or provider for a service identifier.

Use `set()` when you want manual registration without decorating a class.
This is the supported low-level API for values, classes, and factories.

Supported forms today:

- `container.set(id, value)`
- `container.set(id, { useValue: value })`
- `container.set(id, { useClass: Class, scope? })`
- `container.set(id, { useClass: Class, scope?, injections? })`
- `container.set(id, { useFactory: (container) => value, scope? })`

This is the low-level API for manual value, class, and factory registration.
For decorator-driven classes, prefer `@Service()`.

Value example:

```ts
const REQUEST_ID = 'request-id';

Container.of().set(REQUEST_ID, { useValue: crypto.randomUUID() });

const requestId = Container.of().get<string>(REQUEST_ID);
```

Class example:

```ts
interface Logger {
log(message: string): void;
}

class ConsoleLogger implements Logger {
public log(message: string) {
console.log(message);
}
}

Container.of().set<Logger>('logger', { useClass: ConsoleLogger, scope: 'singleton' });
```

Factory example:

```ts
class Connection {
constructor(public readonly requestId: string) {}
}

Container.of().set('request-id', { useValue: 'request-1' });
Container.of().set(Connection, {
useFactory: (container) => new Connection(container.get('request-id')),
scope: 'container',
});
```

For named containers, values bound in the default container are available through
fallback. Provider objects are detected structurally, so plain object values that
contain `useValue`, `useClass`, or `useFactory` should be wrapped with
`{ useValue: value }` if you want them treated as values.

## Internal architecture

The implementation is intentionally small and split into a few focused modules:
Expand Down
6 changes: 5 additions & 1 deletion src/container/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export class Container {
* @returns `true` when the current container has a local registration.
*/
public has(id: ServiceIdentifier): boolean {
return this.metadataMap.has(id);
return this.bindingMap.has(id) || this.metadataMap.has(id);
}

/**
Expand Down Expand Up @@ -235,6 +235,10 @@ export class Container {
let metadata = this.metadataMap.get(id) as Metadata<T> | undefined;

if (!metadata && !this.isDefault()) {
if (ContainerRegistry.defaultContainer.bindingMap.has(id)) {
return ContainerRegistry.defaultContainer.bindingMap.get(id) as T;
}

const defaultMetadata = ContainerRegistry.defaultContainer.metadataMap.get(id) as Metadata<T> | undefined;

if (!defaultMetadata) {
Expand Down
22 changes: 22 additions & 0 deletions test/container/container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ describe('Container', () => {
expect(Container.of().get(BoundService)).toBe(bound);
});

test('falls back to a value bound on the default container', () => {
const requestContainer = Container.of('container-binding-fallback');

class BoundService {}

const bound = new BoundService();

Container.of().set(BoundService, bound);

expect(requestContainer.get(BoundService)).toBe(bound);
});

test('registers a class provider for a custom identifier', () => {
interface Logger {
log(message: string): string;
Expand Down Expand Up @@ -192,6 +204,16 @@ describe('Container', () => {
expect(defaultInstance).toBe(requestInstance);
expect(created).toBe(1);
});

test('reports local bindings through has()', () => {
class BoundService {}

const bound = new BoundService();

Container.of().set(BoundService, bound);

expect(Container.of().has(BoundService)).toBe(true);
});
});

describe('remove', () => {
Expand Down
Loading