Dependency class
A lightweight dependency injection system for managing singleton instances.
Dependency provides a centralized store for managing object lifecycles in your application. It supports both eager and lazy initialization, optional tagging for multiple instances, and automatic cleanup.
Key features:
- Singleton pattern enforcement
- Lazy initialization support
- Tagged instances for multiple controllers
- Automatic ReactiveController lifecycle management
- Phoenix mode for auto-recreation after deletion
Example:
// Register a controller
final controller = Dependency.put(MyController());
// Retrieve it anywhere
final ctrl = Dependency.find<MyController>();
// Lazy registration
Dependency.lazyPut<DataService>(() => DataService());
// Conditional registration
Dependency.putIfAbsent<CacheService>(() => CacheService());
// Lazy conditional registration
Dependency.lazyPutIfAbsent<ApiClient>(() => ApiClient());
// With tags for multiple instances
Dependency.put(UserController(), tag: 'admin');
Dependency.put(UserController(), tag: 'guest');
final admin = Dependency.find<UserController>(tag: 'admin');
Constructors
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
delete<
T> ({String? tag}) → bool - Removes a dependency from the store.
-
find<
T> ({String? tag}) → T - Retrieves a registered dependency instance.
-
isRegistered<
T> ({String? tag}) → bool - Checks if a dependency is registered.
-
lazyPut<
T> (T builder(), {String? tag, bool fenix = false}) → void - Registers a lazy dependency builder.
-
lazyPutIfAbsent<
T> (T builder(), {String? tag, bool fenix = false}) → void - Registers a lazy dependency builder only if it doesn't already exist.
-
put<
T> (T dependency, {String? tag}) → T - Registers a singleton instance in the dependency store.
-
putIfAbsent<
T> (T builder(), {String? tag}) → T - Registers a dependency only if it doesn't already exist.
-
reset(
) → void - Removes all dependencies and resets the store.