Skip to content
This repository has been archived by the owner on Dec 6, 2017. It is now read-only.

Commit

Permalink
feat(module): new binding syntax
Browse files Browse the repository at this point in the history
bind(Foo, annotatedWith: a, toValue: new Foo(), visibility: v);
bind(Foo, annotatedWith: a, toFactory: (i) => new Foo(), visibility: v);
bind(Foo, annotatedWith: a, toImplementation: FooImpl, visibility: v);
bind(Foo, annotatedWith: a, visibility: v);

type, value and factory methods are marked as deprecated.

Closes #90
  • Loading branch information
pavelgj committed Apr 25, 2014
1 parent ae66fcd commit 36357b5
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 76 deletions.
82 changes: 69 additions & 13 deletions lib/src/module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,71 @@ class Module {
return _providersCache;
}

/**
* Registers a binding for a given [type].
*
* The default behavior is to simply instantiate the type.
*
* The following parameters can be specified:
*
* * [toImplementation]: The given type will be instantiated using the [new]
* operator and the resulting instance will be injected.
* * [toFactory]: The result of the factory function is the value that will
* be injected.
* * [toValue]: The given value will be injected.
* * [withAnnotation]: Type decorated with additional annotation.
* * [visibility]: Function which determines if the requesting injector can
* see the type in the current injector.
*
* Up to one (0 or 1) of the following parameters can be specified at the
* same time: [toImplementation], [toFactory], [toValue].
*/
void bind(Type type, {dynamic toValue, FactoryFn toFactory,
Type toImplementation, Type withAnnotation, Visibility visibility}) {
bindByKey(new Key(type, withAnnotation), toValue: toValue,
toFactory: toFactory, toImplementation: toImplementation,
visibility: visibility);
}

/**
* Same as [bind] except it takes [Key] instead of
* [Type] [withAnnotation] combination.
*/
void bindByKey(Key key, {dynamic toValue, FactoryFn toFactory,
Type toImplementation, Visibility visibility}) {
_checkBindArgs(toValue, toFactory, toImplementation);
_dirty();
if (toValue != null) {
_providers[key.id] = new ValueProvider(key.type, toValue, visibility);
} else if (toFactory != null) {
_providers[key.id] = new FactoryProvider(key.type, toFactory, visibility);
} else {
_providers[key.id] = new TypeProvider(
toImplementation == null ? key.type : toImplementation, visibility);
}
}

_checkBindArgs(toValue, toFactory, toImplementation) {
int count = 0;
if (toValue != null) count++;
if (toFactory != null) count++;
if (toImplementation != null) count++;
if (count > 1) {
throw 'Only one of following parameters can be specified: '
'toValue, toFactory, toImplementation';
}
return true;
}

/**
* Register a binding to a concrete value.
*
* The [value] is what actually will be injected.
*/
@Deprecated("Use bind(type, toValue: value)")
void value(Type id, value, {Type withAnnotation, Visibility visibility}) {
_dirty();
Key key = new Key(id, withAnnotation);
_providers[key.id] = new ValueProvider(id, value, visibility);
bind(id, toValue: value, withAnnotation: withAnnotation,
visibility: visibility);
}

/**
Expand All @@ -81,28 +137,28 @@ class Module {
* * [visibility]: Function which determines fi the requesting injector can see the type in the
* current injector.
*/
@Deprecated("Use bind(type, implementedBy: impl)")
void type(Type type, {Type withAnnotation, Type implementedBy, Visibility visibility}) {
_dirty();
Key key = new Key(type, withAnnotation);
_providers[key.id] = new TypeProvider(
implementedBy == null ? type : implementedBy, visibility);
bind(type, withAnnotation: withAnnotation, visibility: visibility,
toImplementation: implementedBy);
}

/**
* Register a binding to a factory function.
*
* The [factoryFn] will be called and all its arguments will get injected.
* The result of that function is the value that will be injected.
* The [factoryFn] will be called and the result of that function is the value
* that will be injected.
*/
@Deprecated("Use bind(type, toFactory: factory)")
void factory(Type id, FactoryFn factoryFn, {Type withAnnotation,
Visibility visibility}) {
factoryByKey(new Key(id, withAnnotation), factoryFn,
visibility: visibility);
bind(id, withAnnotation: withAnnotation, visibility: visibility,
toFactory: factoryFn);
}

@Deprecated("Use bindByKey(type, toFactory: factory)")
void factoryByKey(Key key, FactoryFn factoryFn, {Visibility visibility}) {
_dirty();
_providers[key.id] = new FactoryProvider(key.type, factoryFn, visibility);
bindByKey(key, visibility: visibility, toFactory: factoryFn);
}

/**
Expand Down
Loading

0 comments on commit 36357b5

Please sign in to comment.