Skip to main content
Components like providers, validators, and the grammar engine are wired together through a central ServiceContainer rather than hard-coded imports. This makes it straightforward to swap implementations for testing or customize the validation pipeline.

Overview

The DI system consists of:
  • ServiceContainer: Central registry for services
  • Factory Functions: Create components with dependencies
  • ComponentFactory: High-level factory for spell checker components

ServiceContainer

The ServiceContainer manages component lifecycle through factory functions:

Registering Services

Register factory functions that create services:

Retrieving Services

Services are created lazily on first access:

Available Services

The default container provides these services:

Thread Safety

Singleton creation is thread-safe using double-checked locking:

Container Operations

Component Factories

Individual component factories create specific services:

ProviderFactory

ProviderFactory is a type alias for Callable[[ServiceContainer], DictionaryProvider]. Use create_provider_factory() to get the factory function:

SymSpellFactory

SymSpellFactory is a type alias for Callable[[ServiceContainer], SymSpell]. Use create_symspell_factory() to get the factory function:

ValidatorsFactory

ValidatorFactory is a type alias for Callable[[ServiceContainer], Validator]. create_validators_factory() returns a dict of named factory functions:

SegmenterFactory

SegmenterFactory is a type alias for Callable[[ServiceContainer], Segmenter]. Use create_segmenter_factory() to get the factory function:

RankerFactory

The ranker factory uses create_base_ranker() which takes a RankerConfig directly (not the container pattern):

PhoneticFactory

PhoneticHasherFactory is a type alias for Callable[[ServiceContainer], Optional[PhoneticHasher]]. Use create_phonetic_hasher_factory() to get the factory function:

ContextCheckerFactory

ContextCheckerFactory is a type alias for Callable[[ServiceContainer], Optional[NgramContextChecker]]. Use create_context_checker_factory() to get the factory function:

ContextValidatorFactory

ContextValidatorFactory is a type alias for Callable[[ServiceContainer], ContextValidator]. Use create_context_validator_factory() to create a ContextValidator wired with all validation strategies (tone, syntactic, POS sequence, question structure, homophone, n-gram, semantic):
A minimal variant is also available via create_context_validator_minimal_factory() for fast mode (only tone, syntactic, and question structure strategies).

OptionalServicesFactory

The optional_services_factory module provides factory functions for services that enable advanced validation but are not required for basic spell checking. Each returns None if dependencies are unavailable (graceful degradation):

SuggestionStrategyFactory

SuggestionStrategyFactory is a type alias for Callable[[ServiceContainer], Optional[SuggestionStrategy]]. Use create_suggestion_strategy_factory() to create a CompositeSuggestionStrategy that aggregates suggestions from multiple sources (SymSpell, morphology, compound) with unified ranking:
Dependencies are resolved in order: provider -> symspell -> context_checker (optional). If context checking is enabled, the composite is wrapped in ContextSuggestionStrategy for context-aware re-ranking.

ComponentFactory

The high-level ComponentFactory orchestrates component creation:

Creating Custom Factories

Implement the ComponentFactoryProtocol to provide custom component construction:
Register with the container using a factory function:

Dependency Resolution

Dependencies are resolved through the container:

Testing with DI

DI makes testing easier by allowing mock injection:

Service Registration

The ServiceContainer provides service registration and discovery:

Best Practices

  1. Singleton for expensive resources: Use singleton=True for database connections, caches
  2. Transient for stateful services: Use singleton=False for services with request-specific state
  3. Factory functions: Keep factory functions simple and focused
  4. Dependency declaration: Explicitly resolve dependencies in factory functions
  5. Configuration access: Use container.get_config() for configuration values

Architecture

See Also