WPF, MVVM and Prism modularity
I am still learning to use MVVM and Prism and have some general questions:
I have a grid in my view. Lets say I have a button that when I click 开发者_开发知识库I want it to auto size the grid columns. Where would that code go? Resizing grid columns is a view thing and the view model shouldn't know about it. So in this case would I be adding the button click handler in the view's code behind? I have the same question with grid editing and validation. The view model can see when a value is edited with two-way binding but if it decides that the value is invalid, how can it notify the grid to cancel the edit?
Lets say my view has a number of user controls and each user control needs to bind to data from a different object. Would my view model for this view just be a huge class with all of the data I would need for all of the different components of the view?
Regarding Prism and modular design, I am trying to figure out what a "module" is. My understanding is that a module is self contained, meaning if I pick up my module and drop it in another app, it should work. So if I have a class that makes some service calls (lets say SOAP calls to a server get some info) and populates a grid, my module would need to include both the MVVM components and my service layer, right? If I have multiple modules that use the same service layer, does each one need to include a copy of the service layer classes in order to be considered a complete module?
Thanks for any advice/info.
I'll try to go over these items separately.
- As you said, resizing the grid columns is more of a view thing, so keeping that as part of the UI logic would probably be the best thing. With the intention of keeping the code behind as clean as possible, you can use different things such as behaviors and in some scenarios routed commands to avoid having your code-behind filled with code. As for the validation logic, you should probably place that in the VM/M and use WPF's validation capabilities (such as IDataErrorInfo and others).
- You can have a single view model for all user controls or take a hierarchichal view model approach. The latest Prism drop, shows this scenario in the MVVM RI.
- Your understanding of modules is accurate. One thing to consider, is that if you have a common service for the entire application, the usual approach is placing its interface in the Infrastructure project and reference it when necessarry (the concrete implementation is usually obtained via DI or Mef imports). The module that the concrete implementation belongs to can choose to register the service in the container/expose the export as required. This thread has more information about Modules, what they are, and how they should be used.
If you are getting started with Prism and have any other questions, you might find the codeplex forum useful, as perhaps your question has been answered there by the Prism SE team.
I hope this answer helps.
Thanks, Damian
精彩评论