singleton_manager 2.2.2
singleton_manager: ^2.2.2 copied to clipboard
High-performance singleton manager for Dart with DI annotations and lazy loading.
Singleton Manager Examples #
Three focused examples showing how to use the singleton_manager package.
1. Basic registry (1_basic_registry.dart) #
RegistryManager.instance— the shared registryconnectInstance<InterfaceType, InstanceType>(factory)— connect an interface to a concrete factory, invoked lazily on first accessgetInstance<T>()— resolve (and cache) the connected instance; throwsRegistryNotFoundErrorif nothing is connectedgetInstanceNullable<T>()— same, but returnsnullinstead of throwing
2. Dependency injection factory (2_dependency_injection_factory.dart) #
Shows the @dependencyInjectable annotation and the dependencyInjectionFactory()
factory constructor the generator produces for an annotated class, resolving mandatory
constructor parameters via getInstance and nullable named parameters via
getInstanceNullable.
3. Subkey-disambiguated dependency injection (3_subkey_dependency_injection.dart) #
Shows the @Subkey('...') annotation, used to resolve dependency injection
ambiguity when multiple constructor parameters share the same interface
type. Each parameter is tagged with the subkey it must be resolved with
(e.g. ipv4/ipv6), and connectInstance/getInstanceNullable are called
with the matching subkey to keep the two registrations independent.
Running the examples #
cd packages/singleton_manager
dart run example/1_basic_registry.dart
dart run example/2_dependency_injection_factory.dart
dart run example/3_subkey_dependency_injection.dart