Core Concepts
DependyProvider
A DependyProvider<T> defines how to create an instance of type T. Each provider has:
- A factory function that creates the instance.
- An optional
dependsOnset declaring which types it needs. - An optional
disposecallback for cleanup. - An optional
tagfor registering multiple providers of the same type. - An optional
transient: trueflag for creating a fresh instance on every resolution. - An optional
decoratorslist for wrapping the resolved instance.
DependyProvider<LoggerService>(
(_) => ConsoleLoggerService(),
);A provider with dependencies:
DependyProvider<ApiService>(
(resolve) async {
final logger = await resolve<LoggerService>();
return ApiService(logger);
},
dependsOn: {LoggerService},
);DependyModule
A DependyModule is a container for providers. Modules can nest other modules, letting you organize services hierarchically.
final appModule = DependyModule(
providers: {
DependyProvider<LoggerService>((_) => ConsoleLoggerService()),
DependyProvider<ApiService>(
(resolve) async => ApiService(await resolve<LoggerService>()),
dependsOn: {LoggerService},
),
},
);Combining Modules
Group related services into separate modules and compose them:
final dbModule = DependyModule(
providers: {
DependyProvider<DatabaseService>((_) => DatabaseService()),
},
);
final appModule = DependyModule(
providers: {
DependyProvider<ApiService>(
(resolve) async => ApiService(await resolve<DatabaseService>()),
dependsOn: {DatabaseService},
),
},
modules: {dbModule},
);Providers in appModule can resolve types from dbModule.
Scopes and Lifetimes
Singleton (default)
Created once, cached, shared across all resolutions:
DependyProvider<LoggerService>((_) => LoggerService());Transient
A new instance on every resolution. Nothing is cached:
DependyProvider<FormValidator>(
(_) => FormValidator(),
transient: true,
);Captive dependency
A singleton provider cannot depend on a transient provider. Dependy detects this at module construction and throws DependyCaptiveDependencyException.
Scoped
Services tied to a specific module's lifetime. When the module is disposed, its scoped services are disposed too. This is especially useful in Flutter with ScopedDependyMixin and ScopedDependyProvider.
Tagged Instances
Register multiple providers of the same type using tag:
DependyProvider<HttpClient>(
(_) => HttpClient('https://api.example.com'),
tag: 'api',
),
DependyProvider<HttpClient>(
(_) => HttpClient('https://cdn.example.com'),
tag: 'cdn',
),Resolve by tag:
final apiClient = await module<HttpClient>(tag: 'api');
final cdnClient = await module<HttpClient>(tag: 'cdn');Disposing
Dispose all providers in a module:
module.dispose();To also dispose submodules:
module.dispose(disposeSubmodules: true);Using a disposed module
Resolving from a disposed module throws DependyModuleDisposedException. Always check a module is still active before resolving.
Keys
Keys identify providers and modules in error messages and debug output:
DependyProvider<MyService>(
(_) => MyService(),
key: 'my_service',
);Next Steps
- Exceptions: Errors Dependy can throw
- Guides: Transient providers, tagged instances, decorators, and more
- Examples: See these concepts in practice
