Context: WPF MVVM App using Prism
We have a ServiceLocator that is responsible for returning the singleton instances of our ViewModel classes.
For example:
var viewModel = ServiceLocator.Default.GetInstance<MyViewModel>();
Is there a reason why I shouldn't just simplify that access to something like:
class MyViewModel
{
public static MyViewModel Instance => ServiceLocator.Default.GetInstance<MyViewModel>();
}
And so in this way, usage would be:
var viewModel = MyViewModel.Instance;
What are the advantages/disadvantages?
From what I can tell:
Disadvantage: would be adding the dependency on ServiceLocator
to MyViewModel
.
Advantage: much simpler access and readability. The app already has a dependency on ServiceLocator
anyways