MVVM Architecture – Understanding MVVM and Service Pattern

Architecturedependency-injectionmvvm

I'm building a WPF application using the MVVM pattern. Right now, my viewmodels calls the service layer to retrieve models (how is not relevant to the viewmodel) and convert them to viewmodels. I'm using constructor injection to pass the service required to the viewmodel.

It's easily testable and works well for viewmodels with few dependencies, but as soon as I try to create viewModels for complex models, I have a constructor with a LOT of services injected in it (one to retrieve each dependencies and a list of all available values to bind to an itemsSource for example). I'm wondering how to handle multiple services like that and still have a viewmodel that I can unit test easily.

I'm thinking of a few solutions:

  1. Creating a services singleton (IServices) containing all the available services as interfaces. Example: Services.Current.XXXService.Retrieve(), Services.Current.YYYService.Retrieve(). That way, I don't have a huge constructor with a ton of services parameters in them.

  2. Creating a facade for the services used by the viewModel and passing this object in the ctor of my viewmodel. But then, I'll have to create a facade for each of my complexe viewmodels, and it might be a bit much…

What do you think is the "right" way to implement this kind of architecture ?

Best Answer

In fact, both of these solutions are bad.

Creating a services singleton (IServices) containing all the available services as interfaces. Example: Services.Current.XXXService.Retrieve(), Services.Current.YYYService.Retrieve(). That way, I don't have a huge constructor with a ton of services parameters in them.

This is essentially the Service Locator Pattern, which is an anti-pattern. If you do this, you will no longer be able to understand what the view model actually depends on without looking at its private implementation, which will make it very difficult to test or refactor.

Creating a facade for the services used by the viewModel and passing this object in the ctor of my viewmodel. But then, I'll have to create a facade for each of my complexe viewmodels, and it might be a bit much...

This isn't so much an anti-pattern but it is a code smell. Essentially you're creating a parameter object, but the point of the PO refactoring pattern is to deal with parameter sets that are used frequently and in a lot of different places, whereas this parameter would only ever be used once. As you mention, it would create a lot of code bloat for no real benefit, and wouldn't play nice with a lot of IoC containers.

In fact, both of the above strategies are overlooking the overall issue, which is that coupling is too high between view models and services. Simply hiding these dependencies in a service locator or parameter object does not actually change how many other objects the view model depends on.

Think of how you would unit-test one of these view models. How big is your setup code going to be? How many things need to be initialized in order for it to work?

A lot of people starting out with MVVM try to create view models for an entire screen, which is fundamentally the wrong approach. MVVM is all about composition, and a screen with many functions should be composed of several different view models, each of which depends on only one or a few internal models/services. If they need to communicate with each other, you do so via pub/sub (message broker, event bus, etc.)

What you actually need to do is refactor your view models so that they have fewer dependencies. Then, if you need to have an aggregate "screen", you create another view model to aggregate the smaller view models. This aggregate view model doesn't have to do very much by itself, so it in turn is also fairly easy to understand and test.

If you've done this properly, it should be obvious just from looking at the code, because you'll have short, succinct, specific, and testable view models.