https://github.com/hamed-rezaee/dart_injektor
This is a lightweight dependency injection package in Dart language that provides the ability to register and resolve dependencies.
https://github.com/hamed-rezaee/dart_injektor
dart dependency-injection flutter service-locator service-locator-pattern
Last synced: 8 months ago
JSON representation
This is a lightweight dependency injection package in Dart language that provides the ability to register and resolve dependencies.
- Host: GitHub
- URL: https://github.com/hamed-rezaee/dart_injektor
- Owner: hamed-rezaee
- License: mit
- Created: 2023-03-25T16:01:30.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-06-28T14:09:51.000Z (over 2 years ago)
- Last Synced: 2025-01-03T19:31:46.407Z (9 months ago)
- Topics: dart, dependency-injection, flutter, service-locator, service-locator-pattern
- Language: Dart
- Homepage:
- Size: 34.2 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Injektor
This is a lightweight dependency injection package in Dart language that provides the ability to register and resolve dependencies.
## Usage
```dart
import 'package:dart_injektor/injektor.dart';// Fetch the dependency injector instance, or create a new one.
final injektor = Injektor();// Register a dependency instance.
final myDependency = MyDependency();
injektor.register(myDependency);// Resolve the dependency.
// Alternatively, you can use the `resolve` or `call` methods to be more explicit.
final myClass = injektor();
```## API
The `Injektor` package provides the following methods:
`register(T instance, [String identifier = 'DEFAULT'])`
Registers an instance of a dependency. The T type parameter specifies the type of the dependency. The `instance` parameter is the instance to be registered. The `identifier` parameter is an optional identifier for the instance. If not provided, it defaults to `DEFAULT`.
`lazyRegister(Function() factory, [String identifier = 'DEFAULT'])`
Registers a factory function for a dependency. The T type parameter specifies the type of the dependency. The `factory` parameter is a function that returns an instance of the dependency. The `identifier` parameter is an optional identifier for the factory. If not provided, it defaults to `DEFAULT`.
`resolve([String identifier = 'DEFAULT'])`
`call([String identifier = 'DEFAULT'])`
`([String identifier = 'DEFAULT'])`
Resolves a dependency instance. The `T` type parameter specifies the type of the dependency. The `identifier` parameter is an optional identifier for the instance or factory to resolve. If not provided, it defaults to `DEFAULT`. If an instance is registered for the given type and identifier, it is returned. Otherwise, if a factory is registered for the given type and identifier, the factory is called to create an instance and register it before returning it. An exception is thrown if neither an instance nor a factory is registered.
`dispose([String identifier = 'DEFAULT'])`
Disposes a dependency instance or factory. The T type parameter specifies the type of the dependency. The `identifier` parameter is an optional identifier for the instance or factory to dispose. If not provided, it defaults to `DEFAULT`. If an instance is registered for the given type and identifier, it is removed from the instances map. If a factory is registered for the given type and identifier, it is removed from the factories map.
`disposeAll()`
Disposes all dependency instances and factories. Removes all instances from the instances map and all factories from the factories map.
## Features
- ✅ Register instance (Eager), instance is created when register.
- ✅ Register factory (Lazy), instance is created when first resolve.
- ✅ Resolve instance, return instance if it exists.
- ✅ Dispose instance or factory, remove the instance or factory from the injector.
- ✅ Dispose All, remove all instances and factories from the injector.## License
This library is licensed under the `MIT License`.