So I am delving into MVVM using C# with WPF (using Expression Blend) and have run into a question of how to properly implement the gathering of data from a database and displaying it in a window.
My view is just a grid with a few TextBlocks that I can update with data from a SQL query (I am reporting the number of calls that were made to our company and by our phone agents, so All Calls, Incoming Calls, etc.)
My question is, do I need a model here to get/hold the data or is it good (or at acceptable) MVVM practice to do this in the ViewModel? If I do it in the VM I can easily implement the INotifyPropertyChanged event and everything works. If I create a model to hold the data do I then have to create an INotifyPropertyChanged in the Model and the VM or is there something else that should be used to do the notifications?
This seems like a straight forward task but I just can’t seem to find a good example of just displaying pieces of data, every example I find is of a collection and being my first MVVM program I want to make sure I do it right 🙂
I read on another post someone saying “every Model requires a VM but not every VM requires a Model”. This goes along with my question above and would just like to hear the thoughts of some experienced users.
Thanks,
Brian
You *should” have a model, which you should think of as two separate pieces:
1) a domain sort of object which encapsulates business rules and logic (ie, Customer)
2) some data access sort of object which encapsulates data retrieval (ie, CustomerDao or CustomerRepository)
Both of these should be injected into your view model constructor, as opposed to newing them up somewhere inside of the view model:
You should be testing even if it seems trivial; if it only needs one or two tests to prove it works, great.
HTH,
Berryl