Helpers for creating contexts with minimal boilerplate.

const productsContext = ContextHelpers.createSimpleContext('Products', {
description: 'Products domain logic',
commands: [
{
name: 'CreateProduct',
commandType: CreateProductCommand,
handler: new CreateProductHandler(repo)
}
],
queries: [
{
name: 'GetProductById',
queryType: GetProductByIdQuery,
handler: new GetProductByIdHandler(repo)
}
],
repositories: [
{
token: 'productRepository',
instance: new InMemoryProductRepository(),
singleton: true
}
]
});

Constructors

Methods

  • Creates a simple context with minimal boilerplate.

    This helper eliminates the need to create a new class for simple contexts. Just provide the context name and definitions, and you get a fully functional context.

    Parameters

    • name: string

      The name of the context (e.g., 'Products', 'Orders')

    • options: SimpleContextOptions = {}

      Context configuration options

    Returns BaseContext

    A new context instance

    // Before: Manual class creation
    class ProductsContext extends BaseContext {
    readonly metadata = { name: 'products-context', ... };
    readonly name = 'Products';
    getCommands() { return [...]; }
    getQueries() { return [...]; }
    // etc...
    }

    // After: One-liner with ContextHelpers
    const productsContext = ContextHelpers.createSimpleContext('Products', {
    commands: [...],
    queries: [...]
    });
  • Creates a context with only repositories (useful for shared data contexts).

    Parameters

    • name: string

      The name of the context

    • repositories: ContextRepositoryDefinition[]

      Repository definitions

    • Optionaldescription: string

      Optional context description

    Returns BaseContext

    A new context with only repositories

    const sharedDataContext = ContextHelpers.createRepositoryContext('SharedData', [
    {
    token: 'userRepository',
    instance: new InMemoryUserRepository(),
    singleton: true
    },
    {
    token: 'settingsRepository',
    instance: new InMemorySettingsRepository(),
    singleton: true
    }
    ]);
  • Creates a read-only context with only queries (useful for reporting/analytics).

    Parameters

    • name: string

      The name of the context

    • queries: ContextQueryDefinition<Query, unknown>[]

      Query definitions

    • Optionalrepositories: ContextRepositoryDefinition[]

      Optional repository definitions

    • Optionaldescription: string

      Optional context description

    Returns BaseContext

    A new read-only context

    const analyticsContext = ContextHelpers.createReadOnlyContext('Analytics', [
    {
    name: 'GetSalesReport',
    queryType: GetSalesReportQuery,
    handler: new GetSalesReportHandler(repo)
    },
    {
    name: 'GetUserMetrics',
    queryType: GetUserMetricsQuery,
    handler: new GetUserMetricsHandler(repo)
    }
    ]);