-
Notifications
You must be signed in to change notification settings - Fork 718
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Singletons and OnActivation Not Working as Expected #1297
Comments
I'll have a look @kferrone :) |
11 tasks
@kferrone I've added a PR fixing this issue. @dcavanagh if you consider it a good fix I'll update the changelog and set the PR ready to be reviewed |
@kferrone the issue is fixed in the master branch! :) |
You made me so very happy! We refactored our apps with this change and everything is so smooth and clear now. I have tested this heavily and I can verify it works well. |
# for free
to join this conversation on GitHub.
Already have an account?
# to comment
To be clear, if I bind a constructor as a singleton, i.e.
bind(THING).to(SomeClass).inSingletonScope();
, this will work exactly as expected. The constructor is instantiated and cached in the container. This means theonActivation
and constructor will only execute one time andget
returns the single instance as it should.My issue is I can only get this behavior to work when it is specifically a Class in singleton scope and nothing else. For the following
toFactory
,toConstantValue
,toProvider
,toFunction
,toConstructor
, basically everything else, theonActivation
always runs any time youget
the item.Expected Behavior
In the Docs on Scope, it says
toConstantValue
is alwaysinSingletonScope
. It also says the factory and provider are singleton as well.I expect the wrapped function around
toFactory
andtoProvider
to essentially run like a constructor would. So if the docs are correct about them being true singletons I would not expect the outer part to run every singe time I get the factory or provider.for example I should not see the log output twice if a factory really is a singleton
I expect anything considered a singleton to run
onActivation
a single time, not on every singleget
.Basically I expected the value returned by the factory to be stored in the cache, i.e.
()=>true
Current Behavior
Only a class bound as
inSingletonScope
will ever truly act like a singleton when it comes toonActivation
. I have tried with everything else and every other option will runonActivation
every single time I run aget
orinject
. This behavior is what I would call transient. The factory and provider are resolved on each and every request andonActivation
is always run. WithtoConstantValue
,toConstructor
, andtoFunction
; theonActivation
on every request makes the idea ofactivation
kind of vague.Possible Solution
Cache the result after onActivation just like a class would. Maybe introduce all three scopes as options on factory and provider injections.
for example this would make a lot of sense
Steps to Reproduce (for bugs)
Try this binding
Then get it a bunch of times and it always runs onActivation
Now if you do the same with a class, the
onActivation
only ever runs once.Now try and get it twice and
onActivation
runs only once ever, as it should beContext
I really wanted to be able to
run code
when something is accessed for the first time and only the first time. This way dependencies don't need to be available up until something is actually requested. For example, I wanted to activate a database connection the first time the client is accessed. The only way this is possible is if I wrap the underlying client in my own class and bind that as a singleton. Otherwise there is no other way in inversify to only run onActivation once.When it is a class, I can activate in the constructor,
postConstruct
, oronActivation
. They all work perfectly as expected.Any deeper insight as to why only a class can be a true singleton?
The text was updated successfully, but these errors were encountered: