dime 0.3.0-rc dime: ^0.3.0-rc copied to clipboard
Dime is Dependency getion for Dart, allows to define modules with signle/creator inejction factories, allows String tagging and simple Scope tree.
Dime is Dart based Dependency getion framework.
Dime allows to create modules and get based on interfaces, provides way to specify factory methods and 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());
}
Setup #
Add package dependency to pubspec.yaml: #
depedency:
...
dime: ^0.3.0-rc
...
Define module: #
Create a module and how it creates its dependencies:
class MyModule extends BaseAppgetorModule {
@override
void updateInjections() {
/// define injection factories - below for examples
}
}
Below are examples that can be used inside updategetions
method.
Singleton per type
get single value by its class type:
addSingle(MyTitleService());
get singleton value by implementing interface:
addSingle<TitleService>(MyTitleService());
Singleton per type with tag
get single value by its class type:
addSingle(MyTitleService(), tag: "home-title");
addSingle(MyTitleService(), tag: "details-title");
get singleton value by implementing interface:
addSingle<TitleService>(MyTitleService(), tag: "home-title");
Creator on-demand getion, it uses type of Creator
This is creator - which will create an object at time of getion.
typedef Creator<T> = T Function(String tag);
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()}"));
Creator on-demand getion with singleton storage - delayed singleton.
Similar to above example with addCreator
, however created instance will be cached per tag.
addSingleByCreator((tag)=>MyDescriptionService());
Create your own factory.
You can always create your own factory by extending getFactory<T>
and add those to the module.
addFactory(MyTooltipService, MyCustomFactory());
Note: There are some other Factories already provided to be used - like:
getTagFactory
for create method with a TagTaggedSingletongetFactory
- for tagged singletons withCloseable
interfaceSinglegetFactory
- single get factory withClosable
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());
Features and bugs #
Please file feature requests and bugs at the issue tracker.