C# MVVM Where Does the Service Layer Sit?

Your understanding of MVVM is correct, but the "textbook description" doesn't account for services. Typically this is done with dependency injection (DI). Define an interface, IMyDevice and implement it in a MyDevice class. Then register it with your DI container IMyDevice -> MyDevice. By using a DI container (properly) you'll also take yourself out of the VM construction picture. You would have a VM something like:

public class MyViewModel : ViewModelBase
{
  public MyViewModel(IMyDevice myDevice)
  {
  }
}

to get an instance of the VM, you would do:

theDIContainer.Resolve<MyViewModel>();

and it would new up the MyViewModel class and automatically resolve and pass in the IMyDevice instance for you.

There is a lot more to DI then I covered here... just a basic 10,000 mile high answer to your question. Read up on DI and see how it comes into play with MVVM.

Tags:

C#

Wpf

Mvvm