Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/f3ath/klizma
A minimalistic DI (Dependency Injection) container for Dart.
https://github.com/f3ath/klizma
dart dependency-injection flutter
Last synced: 2 days ago
JSON representation
A minimalistic DI (Dependency Injection) container for Dart.
- Host: GitHub
- URL: https://github.com/f3ath/klizma
- Owner: f3ath
- License: mit
- Created: 2020-12-26T21:38:30.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2022-12-13T06:25:58.000Z (almost 2 years ago)
- Last Synced: 2024-08-01T12:27:56.843Z (3 months ago)
- Topics: dart, dependency-injection, flutter
- Language: Dart
- Homepage: https://pub.dev/packages/klizma
- Size: 17.6 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# klizma
A minimalistic DI (Dependency Injection) container for Dart. Inspired by [injector](https://pub.dev/packages/injector).- Extra lightweight
- No external dependencies
- Supports singletons and named factories
```dart
import 'package:klizma/klizma.dart';void main() {
// Create the DI container:
final di = Klizma();
// Add a factory for type `Engine`:
di.provide((_) => Engine('Vroom!'));
// Add a named factory for type `Engine`:
di.provide((_) => Engine('Whoosh!'), name: 'electric');
// Add a factory for type `Horn`:
di.provide((_) => Horn('Honk!'));
// Add a factory for type `Car` using default instances of `Engine` and `Horn`:
di.provide((_) => Car(_(), _()));
// Add a named factory for type `Car` using the named factory for `Engine`
// and the default factory for `Horn`:
di.provide((_) => Car(_('electric'), _()), name: 'tesla');// Build an instance of type `Car`
final car = di.get();
/*
Prints the following:
=====================
Engine created.
Horn created.
Car created.
*/print(car.sound); // Vroom! Honk!
final tesla = di.get('tesla');
/*
Prints the following. Note that the horn is reused:
=====================
Engine created.
Car created.
*/print(tesla.sound); // Whoosh! Honk!
}class Engine {
Engine(this.sound) {
print('Engine created.');
}final String sound;
}class Horn {
Horn(this.sound) {
print('Horn created.');
}final String sound;
}class Car {
Car(this.engine, this.horn) {
print('Car created.');
}final Engine engine;
final Horn horn;String get sound => '${engine.sound} ${horn.sound}';
}
```