I'm working on a project with MVVM. Some ViewModels have more than one repository.
- Is ViewModels with more than one repository bad?
- Please provide with why it's good/bad.
The purpose of the ViewModel is to abstract away where the data is stored, so that the View can display data in a way that makes sense for the user, but not necessarily data that matches the back-end. If you, for example have some data coming from a query in SQL, and some coming from a webservice to augment that data, then push that to the View using a single ViewModel, then I say you're doing exactly what it was designed for. You're abstracting away the complexity of the data being stored in multiple repositories for the view, so that whatever view you decide to use, you have an easy to use, concise ViewModel.
I have some data I need to gather and display:
I roll all that up into a ViewModel:
I want to display all that information on a single page, so the single ViewModel approach gives me a single object that I can display in my View. If I have multiple ViewModels for a single View, or try to do that aggregation and calculation in the View itself, then I undermine the benefits of the pattern, and I might as well go back to regular ASP.NET/Silverlight/whatever. If the Model changes, then I can change how those repositories get to the ViewModel, but the View doesn't care (nor should it), because it only cares about the ViewModel, not where the data is coming from.