poodinis
poodinis copied to clipboard
Documentation unclear whether DependencyContainer.resolve objects are shared or not
The documentation and examples for DependencyContainer
all show it to be used as a shared object. However, it is not clear in the documentation for resolve or register whether objects that are returned are also shared, and whether they need to make synchronized protections for their methods.
Could it be clarified whether resolved objects (especially those registered using the default option singleInstance
) are thread-local or shared in the documentation?
Returned objects, especially via resolve, are supposed to be shared. In particular singletons. I will see where I can clarify this in the docs.
Thank you for the clarification! I wasn't sure if it was meant to be a singleton specific to each thread or a global singleton.
Looking through the unittests, this test does a good job of illustrating how the objects are related: https://github.com/mbierlee/poodinis/blob/8bd88fbcb9d603aa1de8f3d58c8d8f6225b692a6/test/poodinis/containertest.d#L389
It was confusing at first, because when you use DependencyContainer.resolve
, it doesn't actually return a shared object (which makes sense, because many of the objects that are added do not have a shared constructor), so it was unclear at first whether those objects that lack a shared type were actually the same instance or not. But the test linked above makes it absolutely clear that they are indeed the same instances, and it is up to the user to protect relevant blocks of code with synchronized
or other mechanisms.