views:

386

answers:

2

Hi, I've been reading up on the MVVM pattern, and I would like to try it out on a relatively small WPF project. The application will be single-user. Both input and output data will be stored in a "relational" XML file. A schema (XSD file) with Keys and KeyRefs is used to validate the file.

I have also started to get my feet wet with Linq and LinqToXml, and I have written a couple pretty complex queries that actually work (small victories :)).

Now, I'm trying to put it all together, and I'm finding that I'm a little bit confused as to what should go in the Model versus the ViewModel. Here are the options I've been wrestling with so far:

  1. Should I consider the Model the XML file itself and place all the LinqToXml queries in the ViewModel? In other words, not even write a class called Model?
  2. Should I write a Model that is just a simple wrapper for the XML file and XSD Schema Set and performs validation, saves changes, etc.?
  3. Should I put "basic" queries in the Model and "view-specific" queries in the ViewModel? And if so, where should I draw the line between these two categories?

I realize there is not necessarily a "right" answer to this question...I'm just looking for advice and pros/cons, and if anyone is aware of a code example for a similar scenario, that would be great.

Thanks,

-Dan

A: 

Scott Hanselmen has a podcast that goes over this very topic in detail with Ian Griffiths, an individual who is extremely knowledgeable about WPF, and who co-wrote an O'Reilly book titled, "Programming WPF."

Windows Presentation Foundation explained by Ian Griffiths
http://hanselminutes.com/default.aspx?showID=184

The short (woefully incomplete) answer is that the view contains visual objects and minimal logic for manipulating them, while the View Model contains the state of those objects.

But listen to the podcast. Ian says it better than I can.

Robert Harvey
+2  A: 

For a small application, having separate Data Access, Domain Model and Presentation Model layers may seem like overkill, but modeling your application like that will help you decide what goes where. Even if you don't want to decompose your application into three disitinct projects/libraries, thinking about where each piece of functionality should go can help you decide.

In that light, pure data access (i.e. loading the XML files, querying and updating them) belongs in the data access layer, since these are technology specific.

If you have any operations that don't relate to your particular data access technology, but could rather be deemed universally applicable within your application domain, these should go into the Domain Model (or what some call the Business Logic).

Any logic whose sole purpose is to provide specific functionality for a particular user interface technology (WPF, in your case) should go into the Presentation Model.

In your case, the XML files and all the LINQ to XML queries belong in the Data Access Layer.

To utilize MVVM, you will need to create a ViewModel for each View that you want to have in your application.

From your question, it is unclear to me whether you have anything that could be considered Domain Model, but stuff like validation is a good candidate. Such functionality should go into the Domain Model. No classes in the Domain Model should be directly bound to a View. Rather, it is the ViewModel's responsibility to translate between the Domain Model and the View.

All the WPF-specific stuff should go in the ViewModel, while the other classes in your application should be unaware of WPF.

Mark Seemann
Thanks, Mark. So, sounds like I need to go even further than #3 in my question and put *all* queries in the Model (Domain Model), even if I wrote the query to support a specific View. I see a couple advantages to this: (1) I could switch out my XML database for a SqlServer database, and my ViewModel wouldn't have to change (especially if I write an interface for my Model). (2) If a particular query were written to support View A, but later I realized I need the same data for View B, I wouldn't need to do any refactoring to access the data. The downside is lots of "custom" stuff in the Model.
DanM
@DanThMan: Yes, you have already listed the benefits yourself :) As always, there is always disadvantages that go with the advantages; in this case, the main disadvantage is added complexity. You need to weigh the pros and cons in the context of your app, but for all but the simplest apps, the advantages far outweigh the disadvantages. My only correction would be that all queries need to go into the Data Access layer and be hidden behind opaque methods - not in the Domain Model.
Mark Seemann