How to apply MVVM for this scenario

I am asking myself how to properly apply MVVM in the following scenario: Suppose I have simple Master-Detail data such as a person with 0-n addresses. The addresses should appear in the ListBox and the user should be able to trigger certian actions for each address.

So, I created a virtual machine that returns a human model and address models. The problem is I want to create buttons in the DataTemplate address. But in order to make the buttons work, I need a command, opened by each address, that can be bound to the corresponding property on the button.

My question is, is this really the right approach to wrap (in this case) each address in a different object that provides the required functionality? I would like to avoid the fact that I need to maintain a second list of address view models depending on changes to the first list with model objects.

TIA Martin

+2


source to share


1 answer


This seems like the right approach.

It's always tempting to bind an interface directly to the Domain Model class, but experience has shown that you can always slide the ViewModel between the UI and the Domain Model. This will ensure that custom custom elements (such as the ICommand properties you mentioned) remain in the UI and do not invade the domain model.

In some cases, it might appear that the domain model is close to, or even completely consistent with, the intended user experience. In these cases, it is very difficult to resist the urge to simply bind the domain model directly to the view.



However, it is very likely that the requirements for a particular View may come up later and then you have to inject the ViewModel at that time - and that will often be the time when you really don't have the time to do this.

It also makes it easier to understand the architecture of the application if you can simply say, "All domain objects are wrapped in a ViewModel, which is then bound to views."

+2


source







All Articles