English 中文(简体)
MVVM - Using simple model as it s own view model is a bad practice?
原标题:
  • 时间:2009-12-04 05:57:25
  •  标签:
  • mvvm
  • dry

Guess we have simple model, e.g. let it be a Person { Name, Age }.

Now we want to display a list of persons.

  • Persons are read-only
  • We don t need to edit them
  • We don t need any additional stuff like presentation properties, etc.

Now question is if it is a good practice do not create PersonViewModel class that will probably be a copy of model class or will delegate all its properties? Is it a good idea to simply bind listbox to list of persons, not to their view models? It looks DRY enough, but what about idea of MVVM?

最佳回答

I have no issue with bypassing the VM and using the M directly in the View. Sometimes the models are so small and static that loading them into a wrapping VM is wasteful.

问题回答

I ve created standalone ViewModels, but generally not standalone models. The reason for this is DataBinding -- most POCOs don t implement the INotifyPropertyChanged interface and adding them in to make them pseudo-Models seems to defeat the purpose of re-using a simple class AND respecting the MVVM pattern.

Now, if you KNOW you re never going to be editing them, it may not be a bad idea. A VM with simple property redirects seems a bit pointless to me.

As far as I m concerned, if you implement INotifyPropertyChanged, then it becomes a ViewModel, and you can bind to it. :) When I wrote SoapBox Core which is all MVVM, I took the approach that everything is a ViewModel. The only Model objects were classes from third party libraries that I brought in and wrapped in a ViewModel of my own.

I wouldn’t create a ViewModel for the Person business object when the ViewModel doesn’t introduce new required functionality.

You might be interested that there is a second approach in the MVVM community: Create a ViewModel per View and not per Business Object.

Sample applications that follow this approach can be found on the WPF Application Framework (WAF) website.





相关问题
Is Form validation and Business validation too much?

I ve got this question about form validation and business validation. I see a lot of frameworks that use some sort of form validation library. You submit some values and the library validates the ...

Looping javascript get requests

I ve got a javascript 101 question. I m using a nice graphing library (flot), and plotting multiple graphs on a single page. Each graph is a div, with a attribute set that tells the graph where to ...

Django DRY URLs for Model access

Reader s Digest version: How do I get data (either single row if specified or full table) from a model by taking a URL argument of the model s name without hardcoding the URLconfs to match each model? ...

Drying up my rails models

I have a couple of models which share a has_many associations, named scopes and validations. What s the best way of drying these models up so they can share the same code? Create a parent class and ...

wpf DataGridTextColumn refactoring

I am using a DataGrid in a WPF app that has several (literally one for each day of the week) columns which differ only in their data index. A sample of the xaml is below. How can I refactor this into ...

Why isn t DRY considered a good thing for type declarations?

It seems like people who would never dare cut and paste code have no problem specifying the type of something over and over and over. Why isn t it emphasized as a good practice that type information ...

热门标签