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:
-
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.
-
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 ?
5
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.
6
I could write a book about this…in fact I am 😉
First off, there is no universally “right” way to do things. You have to take other factors into consideration.
It’s possible that your services are too fine grained. Wrapping the Services with Facades that provide the interface that a specific Viewmodel or even a cluster of related ViewModels would use might be a better solution.
Simpler still would be to wrap the services into a single Facade that all viewmodels use. Of course that can potentially be a very big interface with a lot of unnecessary functions for the average scenario. But I would say it’s no different than a message router that handles every message in your system.
In fact, what I’ve seen a lot of architectures eventually evolve into is a message bus built around something like the Event Aggregator pattern. Testing is easy there because your test class just registers a listener with the EA and fires the appropriate event in response. But that’s an advance scenario that takes time to grow to. I say start with the unifying facade and go from there.
8
Why not combine both?
Create a facade and put all services your viewmodels use. Then you can have single facade for all yourviewmodels without the bad S word.
Or you can use property injection instead of constructor injection. But then, you need to ensure that those are getting injected properly.
1