Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it bad to have viewmodels within viewmodels?

Tags:

mvvm

wpf

So I've check this post and a couple of others that discuss similar situations, but still haven't found the best solution to the problem I'm trying to solve.

In my mvvm application each view has one viewmodel. I want to make the viewmodels as flat as possible, but it doesn't seem to be practical when it comes to binding with observable collections. For example, If I have a page that displays a client's profile information, and one of the fields is a group of checkboxs for active subscriptions. I would have a viewmodel that looks like this:

public class ClientViewModel
{
    public class SubscriptionViewModel
    {
        public SubscriptionModel Subscription {get;set;}
        public bool IsChecked {get;set;}
    }

    public string Name {get;set;}
    public string Email {get;set;}
    ...
    public ObservableCollection<SubscriptionViewModel> Subscriptions {get;set;}
}

Because of the extra IsChecked property that is not part of SubscriptionModel, I have to create a separate SubscriptionViewModel and nest it inside the client. What would be a better alternative way to do this so that I don't have to end up with viewmodels inside viewmodels?

like image 652
nemesis Avatar asked Feb 18 '15 15:02

nemesis


People also ask

Can one view have multiple ViewModels?

ViewModel is nothing but a single class that may have multiple models. It contains multiple models as a property.

Should I create ViewModel for each fragment?

It depends on the state that you need to hold. If your fragments are isolated from each other, then feel free to use a ViewModel per Fragment. If you need to share the state (and by state I mean LiveData), then it's reasonable to have a ViewModel per activity. But generally, the smaller the classes are, the better.

Should each view have its own ViewModel?

It is not necessary for any view to have a view model to function. However, Google recommends the pattern because it leads to good design. If you want to say that your app is MVVM, then you need to keep the view separated from the data that drives it.

Can a view have multiple ViewModels in WPF?

Err no - it's just convention that you have one instance, but there actually aren't any rules that you can't have multiple VM's in the XAML - the key thing to understand is that every item has it's own DataContext (that's inherited), so if you need to use a different DataContext then you just bind to the appropriate VM ...


1 Answers

For me it's perfectly fine to compose VM out of several others. I'm doing this quite a lot. Sometimes due to the need to re-use parts, sometimes just to have a better readability in the code. Every VM has still a corresponding view. So there is a view for the "inner-viewmodel" as well as a view for the "composed-viewmodel".

I would not use nested public classes though. I'm treating them as "first-class-citizen".

The scenario I've recently had, was a small product information display on a customer page as well as the invoice page. So I decided to create a ProductOverviewViewModel as well as a ProductionOverviewView, and both are used within the customer and invoice views/viewmodels.

like image 86
Martin Moser Avatar answered Nov 15 '22 15:11

Martin Moser