You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here , when talking about factories, it states that values provided by factories are singletons and suggests using toDynamicValue if one needs to change it:
By default, Factory has scope Singleton and if you want to change the scope of your factory, you can use toDynamicValue.
The thing is, there is no toDynamicValue overload that accepts such a construct. And this case is not covered by tests.
Generally, having a fluent syntax as a first-class citizen for registering services is awkward. It lacks in a few important aspects:
There is no single point thru which all fully configured binding are added, which would have been useful if one wants to log all registrations made in a container.
In case when some registrations' cases are not covered by the provided syntax, it might be not possible to workaround it. (As it seems to be case with the example above)
Current syntax does not allow for an easy way of exposing a component as several interfaces implemented by it, while ensuring the component has the same scope across all registrations.
container.bind<ISomething>("ISomething").to<Implementor>().inSingletonScope();
container.bind<ISomethingElse>("ISomethingElse").to<Implementor>().inSingletonScope();
container.get<ISomething>("ISomething"); // creates an instance of Implementor
container.get<ISomethingElse>("ISomethingElse"); // creates a NEW instance of Implementor
Even if the above it would have worked, I would still prefer autofac-style registrations, as being more concise:
container.bind<Implementor>(Implementor).to(Implementor);
container.bind<ISomething>("ISomething").toService(Implementor);
container.bind<ISomethingElse>("ISomethingElse").toService(Implementor);
container.get<ISomething>("ISomething"); // creates an instance of Implementor
container.get<ISomethingElse>("ISomethingElse"); // returns the same instance
Note, the latter approach has a bug with regard to @preDestroy(): it will be called for each resolved instances of ISomething, ISomethingElse or Implementor, ending up calling it up to 3 times on the same instance.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Here , when talking about factories, it states that values provided by factories are singletons and suggests using
toDynamicValue
if one needs to change it:The thing is, there is no
toDynamicValue
overload that accepts such a construct. And this case is not covered by tests.Generally, having a fluent syntax as a first-class citizen for registering services is awkward. It lacks in a few important aspects:
Even if the above it would have worked, I would still prefer autofac-style registrations, as being more concise:
This is how I workaround it:
Note, the latter approach has a bug with regard to
@preDestroy()
: it will be called for each resolved instances of ISomething, ISomethingElse or Implementor, ending up calling it up to 3 times on the same instance.Beta Was this translation helpful? Give feedback.
All reactions