I’m just starting out with WPF, and trying to do things the MVVM way (following this great article).
I have a central manager class that ALL view models will need to interact with. I implemented this using a singleton, so I have my singleton class:
public class FakeManager
{
private FakeManager() {}
static FakeManager instance;
public static FakeManager Instance
{
get { return instance ?? (instance = new FakeManager()); }
}
...
}
And in my view models I interact with this like so:
public ICommand TriggerChannelChange
{
get
{
return new RelayCommand(() => FakeManager.Instance.SetupChangeRequest(_hardwareItem), () => true);
}
}
My question is – is there a better way? I know of the event mediator pattern, which is commonly used in WPF to send messages between ViewModels, is that something that would be better here? I guess my issues with what I’ve done are the fact that I’m tightly coupled to the FakeManager, plus it feels a little clumsy.
Thanks
Firstly, I agree with blindmeis that creating a new command in a property getter violates the general expectations of a property getter, including:
But that’s an aside point, so I won’t labor it.
As for your question, I would suggest looking into a service pattern, whereby you define an interface for the service and then your view models take a dependency on that service. The ways they can obtain the service are many and varied. You might want to start simple with the service locator pattern, or maybe look into dependency injection or MEF.