dioc 0.1.2 copy "dioc: ^0.1.2" to clipboard
dioc: ^0.1.2 copied to clipboard

outdated

Inversion of control based on dependency injection through containers.

dioc #

Inversion of control based on dependency injection through containers.

Usage #

A simple usage example:

final container = Container();
container.register<Service>((c) => MockService());
container.register<Service>((c) => Controller(c.create(Service)));

final created = container.create<Controller>();
final singleton = container.singleton<Controller>();

You can also name registrations if multiple instances or factories of the same type are needed. The default behaviour when registering is InjectMode.Singleton (the instance will be resolved on first access and returned on successive calls).

final container = Container();
container.register<Service>((c) => WebService());
container.register<Service>((c) => MockService(), name : "demo");

final web = container<Service>(); 
final demo = container<Service>(creator: "demo");

The default get behaviour can be changed at registration time with defaultMode :

final container = Container();
container.register<Service>((c) => WebService(), defaultMode: InjectMode.Create);

final web = container<Service>(); 
final web2 = container<Service>(); // A new instance is created on each call.

It is also possible to call explicitely singleton or create method for a specific resolution.

final container = Container();
container.register<Service>((c) => WebService());

final web = container.singleton<Service>();
final web2 = container.singleton<Service>();
final web3 = container.create<Service>();
// web == web2 but web != web3

If you want to reset your container use the unregister or reset methods.

Code generation #

A dioc_generator package is also available for simplifying injections based on constructor analysis.

Notes #

All dependency resolution isn't based on mirrors because it is intended to be used with strong mode.

Features and bugs #

Please file feature requests and bugs at the issue tracker.

3
likes
0
pub points
0%
popularity

Publisher

unverified uploader

Inversion of control based on dependency injection through containers.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

More

Packages that depend on dioc