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
TheServiceContainer 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 usescreate_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):
create_context_validator_minimal_factory() for fast mode (only tone, syntactic, and question structure strategies).
OptionalServicesFactory
Theoptional_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:
provider -> symspell -> context_checker (optional). If context checking is enabled, the composite is wrapped in ContextSuggestionStrategy for context-aware re-ranking.
ComponentFactory
The high-levelComponentFactory orchestrates component creation:
Creating Custom Factories
Implement theComponentFactoryProtocol to provide custom component construction:
Dependency Resolution
Dependencies are resolved through the container:Testing with DI
DI makes testing easier by allowing mock injection:Service Registration
TheServiceContainer provides service registration and discovery:
Best Practices
- Singleton for expensive resources: Use singleton=True for database connections, caches
- Transient for stateful services: Use singleton=False for services with request-specific state
- Factory functions: Keep factory functions simple and focused
- Dependency declaration: Explicitly resolve dependencies in factory functions
- Configuration access: Use
container.get_config()for configuration values
Architecture
See Also
- Configuration Guide - Configuration options
- Architecture - System architecture overview
- Customization Guide - Extending the library