Base implementation for contexts.

Provides automatic registration of commands, queries, event handlers, and repositories during the initialize phase.

Subclasses only need to:

  1. Define metadata (name, dependencies)
  2. Set name property
  3. Implement getCommands(), getQueries(), getEventHandlers(), getRepositories()

The base class handles all the wiring automatically.

Note: Context is different from Plugin:

  • Plugin: Infrastructure extensions (Postgres, Redis, RabbitMQ)
  • Context: Domain/business logic contexts (Orders, Products, Inventory)
export class ProductsContext extends BaseContext {
readonly metadata: ContextMetadata = {
name: 'products-context',
description: 'Products Domain Context',
requiredPlugins: ['postgres'],
requiredContexts: []
};

readonly name = 'Products';

private productRepository!: ProductRepository;

getCommands(): ContextCommandDefinition[] {
return [
{
name: 'CreateProduct',
commandType: CreateProductCommand,
handler: new CreateProductHandler(this.productRepository)
}
];
}
}

Implements

  • Context

Constructors

Methods

  • Returns all command definitions for this context. Override to provide commands for this context.

    Returns ContextCommandDefinition<Command, unknown>[]

    Array of command definitions (empty by default)

  • Returns all query definitions for this context. Override to provide queries for this context.

    Returns ContextQueryDefinition<Query, unknown>[]

    Array of query definitions (empty by default)

  • Returns all event handler definitions for this context. Override to provide event handlers for this context.

    Returns ContextEventHandlerDefinition<Event>[]

    Array of event handler definitions (empty by default)

  • Returns all repository definitions for this context. Override to provide repositories for this context.

    Returns ContextRepositoryDefinition[]

    Array of repository definitions (empty by default)

  • Initializes the context.

    This method:

    1. Registers all repositories in the DI container
    2. Registers all commands with the command bus
    3. Registers all queries with the query bus
    4. Subscribes all event handlers to the event bus

    Subclasses can override this method but should call super.initialize() to ensure automatic registration happens.

    Parameters

    • config: ContextConfig

      The context configuration

    Returns Promise<void>

  • Starts the context. Override if your context needs to start external resources.

    Returns Promise<void>

    async start(): Promise<void> {
    await super.start();
    // Start any context-specific resources
    await this.myService.connect();
    }
  • Stops the context. Override if your context needs to clean up resources.

    Returns Promise<void>

    async stop(): Promise<void> {
    await this.myService.disconnect();
    await super.stop();
    }
  • Health check for the context. Default implementation returns healthy. Override to provide custom health checks.

    Returns Promise<HealthCheckResult>

    Health check result

    async healthCheck(): Promise<HealthCheckResult> {
    try {
    await this.productRepository.findAll();
    return { status: HealthStatus.UP };
    } catch (error) {
    return {
    status: HealthStatus.DOWN,
    message: `Repository error: ${error.message}`
    };
    }
    }

Properties

metadata: ContextMetadata

Context metadata (name, dependencies). Must be implemented by subclasses.

name: string

The name of the context. Must be implemented by subclasses.

config?: ContextConfig

Reference to the context configuration, available after initialize.