Some questions regarding MVVM

I am having some trouble understanding how I should structure my program in a clean way with the MVVM-pattern. I am dealing with models that have a rather deep hierarchy, and is a tree structure. Most examples I find use very simple scenarios, where a ViewModel neatly wraps a Model that has no children elements, with a view bound to the ViewModel.

In a kinda contrived example, this is what I am dealing with:

public class Office { public Address OfficeLocation { get; set; } List<IPerson> People {get; set; } } public interface IPerson { public Address HomeAddress { get; set; } public List<Project> AssignedProjects { get; set; } } public class Project { public string ProjectName { get; set; } public DateTime Deadline { get; set; } } 

So in this application, I would want a Master/Detail for every "level". At the top, I would want a list of Offices, so I would then presumably create a OfficeViewModel:

public class OfficeViewModel { public ObservableCollection<Office> Offices { get; set; } public Office SelectedOffice { get; set; } public void AddOffice(Office office) { .. } public void RemoveOffice(Office office { .. } } 

After selecting an Office in the View, a Detail-view should display the single SelectedOffice instance. Though then I should need a viewmodel for a single Office, to bind to this view?

And keep going, in this Detail-view, I would want to display a list of people belonging to that Office, again with a Master/Detail view. A person has an address, so I would want to be able to change the selected person's address. So the Address-object needs a ViewModel? And this could keep going.

So how do you go about solving something like this using MVVM? Do I need to create a separate tree of ViewModels, that take their respective Models in their constructor? I may not be on the right track at all, but this seems rather cluttery.

If you know of any more in-depth examples of MVVM use in an "actual" application, please share.

by Sweetpipe via /r/csharp

Leave a Reply