0 purchases
dime
Dime is Dart Dependency "injection framework", it is more like factory and static lookup but with nice wrap.
Dime allows to create modules that define injection types and their InjectFactory implementations.
It can easily base on interfaces which allows to pick different implementations.
Supports for tag tag based same type instances.
Support for multiple modules and scopes with Closable interface to cleanup resources.
Get it from pub page: Pub dime page
Note:
All examples below are from example file file. Go there for high level view.
Usage #
A simple usage example:
import 'package:dime/dime.dart';
void main() {
/// Service Module does include details how to create the objects.
dimeInstall(ServiceModule());
MyTitleService titleService = dimeGet();
// or
var titleService = dimeGet<MyTitleService>();
print(titleService.text());
}
copied to clipboard
Setup #
Add package dependency to pubspec.yaml: #
depedency:
...
dime: ^0.7.0
...
copied to clipboard
Define module: #
Create a module and how it creates its dependencies:
class MyModule extends BaseAppgetorModule {
@override
void updateInjections() {
/// define injection factories - below for examples
}
}
copied to clipboard
Below are examples that can be used inside updateinjections method.
Singleton per type
get single value by its class type:
addSingle(MyTitleService());
copied to clipboard
get singleton value by implementing interface:
addSingle<TitleService>(MyTitleService());
copied to clipboard
Singletons per type with tag
get single value by its class type:
addSingle(MyTitleService(), tag: "home-title");
addSingle(MyTitleService(), tag: "details-title");
copied to clipboard
get singleton value by implementing interface:
addSingle<TitleService>(MyTitleService(), tag: "home-title");
copied to clipboard
Creator on-demand injection, it uses type of Creator
This is creator - which will create an object at time of injection.
typedef Creator<T> = T Function(String tag);
copied to clipboard
The Creator provides optional String tag that may be used to create the tagged instance.
addCreator<TextService>((tag) =>
MyTitleService(title: "Test title: $tag: now: ${DateTime.now()}"));
copied to clipboard
Async Creator on-demand injection with Future
We can make the Creator function to return Future
addSingleByCreator((tag) async {
return Future.delayed(Duration(seconds: 2), () {
return My2TitleService();
});
});
copied to clipboard
With this setup we can use new method to fetch Future of the value:
var value = await dimeGetAsync<My2TitleService>();
// or
My2TitleService service;
/// (some code)
service = await dimeGetAsync();
copied to clipboard
or old style which was always supported
var value =await dimeGet<Future<My2TitleService>>();
copied to clipboard
The dimeGetAsync helps to cast to known type without defining type in < >
Creator on-demand injection with singleton storage - delayed singleton.
Similar to above example with addCreator, however created instance will be cached per tag.
addSingleByCreator((tag)=>MyDescriptionService());
copied to clipboard
Create your own factory.
You can always create your own factory by extending getFactory<T> and add those to the module.
addFactory(MyTooltipService, MyCustomFactory());
copied to clipboard
Note:
There are some other Factories already provided to be used - like:
getTagFactory for create method with a Tag
TaggedSingletongetFactory - for tagged singletons with Closeable interface
SinglegetFactory - single get factory with Closable interface
Add modules to the scope (global too) #
You can add modules to the main global scope of Dime or into the opened scopes.
When a scope closes all modules in that scope will also close (clean up) by calling each of its Closeable factory close() method to cleanup resources.
Add Module to Global Dime scope.
dimeInstall(ServiceModule());
copied to clipboard
Dime Scope fetch up the tree examples: #
Test class to be referenced by main scope_test.dart tests.
Modules:
Module
Instance
ModuleA
AA()
AB()
AC()
ModuleB
BA()
BB()
BC()
ModuleC
CA()
CB()
CC()
ModuleXX
AA()
BB()
CC()
Scope graph:
Root Scope
Modules/Scopes
Modules/Scopes
Modules/Scopes
Dime
- ModuleC
- ModuleXX (override)
- scope1
- ModuleA
- ModuleB
- scope2
- ModuleA
- ModuleC
- scope 21
- ModuleC
- scope 22
- ModuleA
- ModuleB
Example injections:
scope2.inject
Dime.inject
scope1.inject
scope1.inject
scope2.inject
scope2.inject
Dime.inject
Dime.inject
Dime.inject
scope21.inject
scope21.inject
scope22.inject
scope22.inject
scope21.inject
Features and bugs #
Please file feature requests and bugs at the issue tracker.
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.