MVVM and service pattern

Posted by alfa-alfa on Programmers See other posts from Programmers or by alfa-alfa
Published on 2013-11-04T15:21:03Z Indexed on 2013/11/04 16:10 UTC
Read the original article Hit count: 197

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 ?

© Programmers or respective owner

Related posts about architecture

Related posts about dependency-injection