Context interface.

A Context represents a self-contained area of functionality in your application. It encapsulates all business logic, commands, queries, events, and repositories for a specific feature area.

Every context has three layers:

  • domain/: Business logic (entities, value objects, domain services)
  • application/: Use cases (commands, queries, handlers)
  • infrastructure/: Technical implementations (repositories, controllers)

Contexts are:

  • Self-contained units of deployment
  • Independent and loosely coupled
  • Easily testable in isolation
class ProductsContext extends BaseContext {
readonly metadata: ContextMetadata = {
name: 'products-context',
description: 'Products management',
version: '1.0.0',
};

readonly name = 'Products';

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

getQueries(): ContextQueryDefinition[] {
return [
{
name: 'GetProductById',
queryType: GetProductByIdQuery,
handler: new GetProductByIdHandler(this.repository)
}
];
}

getRepositories(): ContextRepositoryDefinition[] {
return [
{
token: 'productRepository',
instance: new InMemoryProductRepository(),
singleton: true
}
];
}
}
interface Context {
    metadata: ContextMetadata;
    name: string;
    getCommands(): ContextCommandDefinition<Command, unknown>[];
    getQueries(): ContextQueryDefinition<Query, unknown>[];
    getEventHandlers(): ContextEventHandlerDefinition<Event>[];
    getRepositories(): ContextRepositoryDefinition[];
    initialize(config: ContextConfig): Promise<void>;
    start(): Promise<void>;
    stop(): Promise<void>;
    healthCheck(): Promise<HealthCheckResult>;
}

Methods

  • Initializes the context.

    Called during application startup, after all required plugins and contexts are initialized. Register services in the container during this phase.

    Parameters

    Returns Promise<void>

    async initialize(config: ContextConfig): Promise<void> {
    this.repository = config.container.resolve('productRepository');
    await super.initialize(config);
    }
  • Starts the context.

    Called after all contexts are initialized, before the application starts. Connect to external resources during this phase.

    Returns Promise<void>

    async start(): Promise<void> {
    await this.service.connect();
    }
  • Stops the context.

    Called during application shutdown, in reverse dependency order. Close connections and clean up resources during this phase.

    Returns Promise<void>

    async stop(): Promise<void> {
    await this.service.disconnect();
    }
  • Performs a health check for the context.

    Returns Promise<HealthCheckResult>

    The health check result

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

Properties

metadata: ContextMetadata

Context metadata (name, version, dependencies).

name: string

The name of the context. Should be a PascalCase noun (e.g., 'Products', 'Orders', 'Users').