I understand the benefits of injecting dependencies into objects, but have not found much guidance on the types of objects to inject and when to make them singletons.
If we take as as example an application with the following types of objects:
- Services (including repositories and business services such as calculateprice)
Injecting services is the typical example given and it is clear these are not registered as singletons in the container as they are statelesss.
Was it the original intention that only services should be injected by containers?
Views/ViewModels & other stateful long lasting objects
The general way to access a long lasting object in several places has been to use a registry (or similar), assuming we cannot pass it around as a parameter. Now we have the option to register the object as a singleton in a container and inject it where needed.
I can't see any problem using the singleton in a container approach beyond the issue of removing the object from the container when its lifecycle ends (not sure how easy that is going to be - I use Unity).
Would be interested to hear of any problems others can forsee.
As an interesting aside, the MVVM Light framework uses a ViewModelLocator rather than injecting viewmodels.