Skip to Content

@contextualize

Contexts serve as containers for managing providers. By default, providers are stored in the global context (Context.global) and can be injected anywhere using @provide. To limit provider availability to specific areas, use the @contextualize decorator.

This decorator enables precise control over dependency injection, supporting modularity, security, and organized management by keeping certain providers accessible only within designated contexts.

Example

In this example, we define two contexts: DATABASE and ORDER.

import { application, contextualize, provide, provider } from 'aspectra'
 
enum ContextId {
  DATABASE = 'database',
  ORDER = 'order',
}
 
@contextualize(ContextId.DATABASE)
@provider
class DatabaseProvider {
  public getById(id: number) {
    // run sql query
  }
}
 
@contextualize(ContextId.DATABASE, ContextId.ORDER)
@provider
class OrderProvider {
  @provide(DatabaseProvider)
  private readonly database!: DatabaseProvider
 
  public process(id: number) {
    const product = this.database.getById(id)
    // process order
  }
}
 
@contextualize(ContextId.ORDER)
@application
class CommerceApplication {
  @provide(OrderProvider)
  private readonly order!: OrderProvider
 
  // This will fail as `CommerceApplication` and `DatabaseProvider` have no shared contexts
  // @provide(DatabaseProvider)
  // private readonly database!: DatabaseProvider
 
  public start() {
    this.order.process(1)
  }
}
💡

By assigning DatabaseProvider and CommerceApplication to different contexts, only OrderProvider can access DatabaseProvider, preserving strict boundaries between providers.

Last updated on