Scaldi - binding to single/multiple instances

608 views Asked by At

I recently switched from SubCut to Scaldi and there's a feature I find undocumented/missing:

In SubCut, when you define a binding, you can choose between different binding modes - toSingle, toProvider, to moduleInstanceOf, toModuleSingle, which decide if the injected instances will be created once, or each time the bind is performed. I don't see the analogous behaviours explicitly defined in the Scaldi documentation, so I would like to make sure I understand how the different behaviours can be achieved in Scaldi:

By default the to method is lazy and creates the injected instance the first time it's "requested". There's a toNonLazy in the api which I guess creates the instance even before the first time it's "requested". And there's a toProvider, which in the following example...

bind [Widget] toProvider new ParticularWidget()

...would create a new ParticularWidget every time it is injected in an Injectable.

Am I understanding this correctly?

1

There are 1 answers

0
tenshi On BEST ANSWER

Yes, it works exactly as you described. If binding is defined with toProvider, then inject will always create new instances.

You can see its behavior in action in this spec:

https://github.com/scaldi/scaldi/blob/master/src/test/scala/scaldi/WordBinderSpec.scala#L100


Update

You can find more info in the documentation:

http://scaldi.org/learn/#define-bindings