Singleton versus instance
When you have a viewmodel that is actually used multiple times, such as the one backing a foreach
loop, using an instance is the only option. When there is only one instance of the viewmodel, such as the one backing an entry form or a page in an SPA, the choice might not be as simple.
A good rule of thumb is to think about the lifetime of the object. If the object's lifetime doesn't end, such as the viewmodel for an ever-present navigation bar, using a singleton is appropriate. If the object's lifetime is short, such as a page viewmodel in an SPA, then using a singleton means that the object cannot be garbage collected even after it is no longer being actively used. In this situation, a disposable instance is recommended.
Another rule of thumb is to consider whether or not it has an internal state. Without an internal state that needs to be managed, there is little danger that multiple uses of the object or its methods will result in errors. If an object has no internal...