5

I am currently learning how to make advanced usage of WPF via the Prism (Composite WPF) project.

I watch many videos and examples and the demo application StockTraderRI makes me ask this question:

What is the exact role of each of the following part?

  • SomethingService: Ok, this is something to manage data
  • SomethingView: Ok, this is what's displayed
  • SomethingPresentationModel: Ok, this contains data and commands for the view to bind to (equivalent to a ViewModel).
  • SomethingPresenter: I don't really understand it's usage
  • SomethingController: Don't understand too

I saw that a Presenter and a Controller are not necessary but I would like to understand why they are here. Can someone tell me their role and when to use them?

1
  • 1
    I agree, things a confusing freaking mess.
    – Pierreten
    Commented Mar 5, 2010 at 0:57

2 Answers 2

7

I had exactly the same problem when I first went through Prism.

Controllers are basically for logic that spans an entire module, whereas Presenters are for logic that is specific to a View.

For example, a Presenter would respond to a command that results in a button in the view being disabled. A Controller would respond to a command that results in the View (and Presenter) being changed entirely, or perhaps loading a different View/Presenter in a different region in the module's shell.

Edit: As for when to use them, you can skip the Controller entirely if you have no need for the orchestration mentioned above. The simplest application will just have a:

  • Module: registers the view/presenter into the Region
  • Presenter: responds to commands from the view and modifies the ViewModel.
  • ViewModel: adapter between Presenter and View that implements INotifyPropertyChanged
  • View: binds to ViewModel and displays UI

Edit: As for Presenter vs ViewModel, most of your logic should be in your Presenter. Think of your ViewModel as housing the logic for your view, but the Presenter as dealing with the consequences of interacting with the view.

For example, the user clicks the "Search" button in your View. This triggers an ICommand, which is handled by your Presenter. The Presenter begins the search and sets the ViewModel.IsSearching property, which fires the PropertyChanged notification for CanSearch. CanSearch is a readonly property that is based on several other properties (eg. IsSearchEnabled && !IsSearching). The "Search" button in the View has its Enabled property bound to CanSearch.

3
  • Oh, so it's just a separation between what happens in the current view (Presenter) and what happens in the module (Controller). I actually do all logic stuff in the ViewModel. Is this wrong or is this just a strategic choice?
    – SandRock
    Commented Mar 1, 2010 at 10:17
  • Prism is just a recommendation. Having said that, I've updated my answer with some VM vs Presenter info. Commented Mar 1, 2010 at 10:21
  • Great, everything is now clear in my head. I'll try this now. Thank you for your time :)
    – SandRock
    Commented Mar 1, 2010 at 10:31
0

In my opinion Controller in here refers to Application Controller

1
  • This is true for Prism if there is only one Module. Commented Mar 1, 2010 at 10:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.