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:| Service Name | Type | Description |
|---|---|---|
provider | DictionaryProvider | Dictionary storage backend |
segmenter | Segmenter | Text segmentation |
phonetic_hasher | PhoneticHasher | Phonetic matching (optional) |
ranker | SuggestionRanker | Suggestion ranking |
symspell | SymSpell | SymSpell algorithm |
context_checker | NgramContextChecker | N-gram checking (optional) |
syllable_validator | SyllableValidator | Syllable validation |
word_validator | WordValidator | Word validation |
context_validator | ContextValidator | Context validation |
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:
ComponentFactory
The high-levelComponentFactory orchestrates component creation:
Creating Custom Factories
Implement custom factories for your components: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