Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use different ways to register an object in angulardart

One can use a value, type or factory for registering an object. I have tried to find simple examples how and when to use each of a registering types, but not succeed.

It would be wonderful, if someone could give brief examples and explain the typical use cases.

Here are some links about the subject:

https://stackoverflow.com/a/21245335/2777805

http://victorsavkin.com/post/72452331552/angulardart-for-angularjs-developers-introduction-to

like image 545
grohjy Avatar asked Dec 18 '25 01:12

grohjy


1 Answers

type

// old syntax
type(SomeType); // or
type(SomeInterface, implementedBy: SomType)

// new syntax
bind(SomeType); // or
bind(SomeInterface, toImplementation: SomType)

default, DI creates an instance and all constructor parameters (if any are resolved by DI and provided)

value

// created inline or e.g. passed in from somewhere as a parameter
// old syntax
value(new SomeType('xxx', 123)); 

// new syntax
bind(SomeType, toValue: new SomeType('xxx', 123)); 

if you want to to pass a previously instantiated instance. I usually use this for configuration settings.

factory

// old syntax
factory(NgRoutingUsePushState,
    (_) => new NgRoutingUsePushState.value(false));

// or
factory(UsersRepository, (Injector inj) => new UsersRepository(inj.get(Http)));

// new syntax
bind(NgRoutingUsePushState,toFactory:
    (_) => new NgRoutingUsePushState.value(false));
bind(UsersRepository, toFactory: (Injector inj) => new UsersRepository(inj.get(Http)));

(from http://victorsavkin.com/post/72452331552/angulardart-for-angularjs-developers-introduction-to)

when you want DI to delegate the instantiation to a factory function

like image 97
Günter Zöchbauer Avatar answered Dec 20 '25 15:12

Günter Zöchbauer