File tree 4 files changed +60
-3
lines changed
4 files changed +60
-3
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
12
12
13
13
### Fixed
14
14
- Fixed container to properly resolve async ` .toService ` bindings.
15
+ - Fixed ` .toService ` binding to properly disable caching any values.
15
16
16
17
## [ 6.1.4]
17
18
Original file line number Diff line number Diff line change @@ -116,14 +116,28 @@ class BindingToSyntax<T> implements interfaces.BindingToSyntax<T> {
116
116
}
117
117
118
118
public toService ( service : interfaces . ServiceIdentifier < T > ) : void {
119
- this . toDynamicValue ( ( context : interfaces . Context ) : T | Promise < T > => {
119
+ this . _binding . type = BindingTypeEnum . DynamicValue ;
120
+
121
+ // Service bindings should never ever be cached. This is just a workaround to achieve that. A better design should replace this approach.
122
+ Object . defineProperty ( this . _binding , 'cache' , {
123
+ configurable : true ,
124
+ enumerable : true ,
125
+ get ( ) {
126
+ return null ;
127
+ } ,
128
+ set ( _value : unknown ) { } ,
129
+ } ) ;
130
+ this . _binding . dynamicValue = (
131
+ context : interfaces . Context ,
132
+ ) : T | Promise < T > => {
120
133
try {
121
134
return context . container . get < T > ( service ) ;
122
135
} catch ( _error : unknown ) {
123
136
// This is a performance degradation in this edge case, we do need to improve the internal resolution architecture in order to solve this properly.
124
137
return context . container . getAsync < T > ( service ) ;
125
138
}
126
- } ) ;
139
+ } ;
140
+ this . _binding . implementationType = null ;
127
141
}
128
142
}
129
143
Original file line number Diff line number Diff line change
1
+ import { describe , it } from 'mocha' ;
2
+ import sinon from 'sinon' ;
3
+
4
+ import { Container , injectable , preDestroy } from '../../src/inversify' ;
5
+
6
+ describe ( 'Issue 1416' , ( ) => {
7
+ it ( 'should allow providing default values on optional bindings' , async ( ) => {
8
+ @injectable ( )
9
+ class Test1 {
10
+ public stub : sinon . SinonStub < unknown [ ] , void > = sinon . stub ( ) ;
11
+
12
+ @preDestroy ( )
13
+ public destroy ( ) {
14
+ this . stub ( ) ;
15
+ }
16
+ }
17
+
18
+ @injectable ( )
19
+ class Test2 {
20
+ public destroy ( ) : void { }
21
+ }
22
+
23
+ @injectable ( )
24
+ class Test3 {
25
+ public destroy ( ) : void { }
26
+ }
27
+
28
+ const container : Container = new Container ( { defaultScope : 'Singleton' } ) ;
29
+
30
+ container . bind ( Test1 ) . toSelf ( ) ;
31
+ container . bind ( Test2 ) . toService ( Test1 ) ;
32
+ container . bind ( Test3 ) . toService ( Test1 ) ;
33
+
34
+ const test1 : Test1 = container . get ( Test1 ) ;
35
+ container . get ( Test2 ) ;
36
+ container . get ( Test3 ) ;
37
+
38
+ container . unbindAll ( ) ;
39
+
40
+ sinon . assert . calledOnce ( test1 . stub ) ;
41
+ } ) ;
42
+ } ) ;
Original file line number Diff line number Diff line change 1
1
# Tagged bindings
2
2
3
3
We can use tagged bindings to fix ` AMBIGUOUS_MATCH ` errors when two or more
4
- concretions have been bound to an abstraction. Notice how the constructor
4
+ concretions have been bound to an abstraction. Notice how the constructor
5
5
arguments of the ` Ninja ` class have been annotated using the ` @tagged ` decorator:
6
6
7
7
``` ts
You can’t perform that action at this time.
0 commit comments