views:

956

answers:

1

I'm currently introducing Prism to a new Wpf application, and am using the MVVM pattern. My initial approach of structuring the Wpf application was to add one project to hold the model classes, one to hold the viewmodel classes, etc. These might be split at a later time to avoid having different logical components in the same project. However, this strikes me as a bad structure when using Prism (and parhaps in general..).

In Prism you want to structure things into different logical modules - where anything related to the same things would be placed in the same module. So this tells me I should put all related to a logical part of my application into a module for this part. This might hold some different views for this component, the related view model, and the necessary model classes. Using this approach I would however get the model scattered around my solution. As the model will be tied to a database this strikes me as a possibly bad approach after all. I am using NHibernate, so the database won't really be that "visual" though.

So I see three different structures. Are any of these typically desired? Or is there a different way I should structure my application?

  1. Projects "Model", "ViewModel", and also one to hold UserControls. Etc..
  2. One project pr logical part - including both related view, viewmodel and model for this part.
  3. One project pr logical part - including view and viewmodel, but the model being defined in a separate project. Maybe even one project for all model classes if they have a logical relationship.

Any opinions are greatly appreciated!

+1  A: 

Putting your Model in a separate project is fine. I'd say it's recommended it if it's big enough to really benefit from prism style architecture. The model is not confined to the vertical silo of the V and VM, but is a lower layer that sits below all of them.

Your Views and View Models make sense living close to each other. You might find reuse of views or of view models, but don't stress if you don't. That said, a view isn't always tied for ever and ever to a specific view model and vice versa. For example, I my have a view model that shows all sales and a view model that filters on the current quarter but I can rig both to the same view. On the flip side, I have a pie chart vs. a bar chart on the same view model. So splitting these is not as cut and dried. However, you may find larger chunks than just view/view model pairs. Sales vs customer management and such.

Ball
Thx. I agree with you, and this is the structure I've chosen for now. But is it okay to refer from one module to another, or should they ideally not know about each other? You might e.g. have a module to handle "Customer". But if you have another module that handles "Sales" you might want to refer the Customer ViewModel from this too. Is this okay, or is it a typical indication that the two modules isn't as isolated as you initially though - the solution being that you move both to the same module? What's your opinion on this?
stiank81
Your modules should not know about each other. They wouldn't be modular otherwise. Ideally your application would work just fine with one or more modules being removed.The only types I would keep in common between modules would be types considered your "Model" or other contractual types. For example, if you have a Customer object, you'd keep that in a shared assembly, but definitely not a CustomerViewModel, which contains behavioral code that is only appropriate for one particular view and not appropriate shared.
Anderson Imes