none
MVP and Command pattern RRS feed

  • Question

  •  

    Hi

    I've been reading about the MVP pattern and the Command pattern, always in separate articles. Both seems like a good "idea"

     

    My question is, how do you combine the MVP and Command pattern in a WPF Application.  Any hints and or ideas are much appreciated.

     

    //regards

    //lasse

    Thursday, December 20, 2007 6:35 PM

Answers

  • I remember doing that stuff in the dim distant past...  Interesting stuff, as I remember?

     

    Yes, you are correct in your assumption regarding the MVP pattern.  It allows you to test a Presenter class instead of trying to somehow drive integration tests across the UI, and associated code-behind implementation.

     

    To unit test the command pattern, you would still use interfaces, and quite probably write something similar to a "command manager" to do the command invocation for you.  In that instance your presenter would be invoking code through an interface, which can be mocked, allowing a proper unit test to be performed.  So in the example of an implementation I give above, you would mock out the command manager object, which would be injected into the presenter.  Then you can also seperately unit test your command manager objects in another test class.

     

    Does that help at all?

     

    Martin Platt.

    Tuesday, January 8, 2008 2:51 AM

All replies

  • Lasse,

     

    That seems like an open ended question?

     

    The thing with patterns is that they don't directly have anything to do with the technology that you use to implement them.

     

    I could quote you the information on how these things work, and what their source code might look like, but instead, here's a link that I'm assuming that you haven't actually seen:

     

    http://www.dofactory.com/Patterns/PatternCommand.aspx

     

    If you're still stuck, please ask some more specific question, for example what exactly you would want to achieve with the pattern, in terms of functionality is particular that you might need to implement.

     

    I hope this helps,

     

    Martin Platt.

    Wednesday, January 2, 2008 1:37 AM
  • It is a open ended question.. 

     

    I've seen the page you refered to.

     

    I hoped for some good examples på how to mix the MVP pattern and the Command Pattern, and still keeping a good architecture with "separation of concern", "testability",,, etc. 

     

    For example,,, who invokes the command,,, the View or the Presenter. (my guess,,, the View) ....

     

    (I'm a hardware designer for profession,,,, so this is just a hobby of mine, I'm not a pro.)

     

    //lasse

     

     

     

     

     

    Wednesday, January 2, 2008 10:53 AM
  •  

     

    The presenter would invoke the command pattern code for you.  The view is simply something that shows you a representation of your model, and allows you to indicate that you want something to change.  The presenter does all the work.

     

    What hardware do you design, out of interest?  A long time ago, I studied electrical and electronic engineering, then saw it fit to work as a programmer!

     

    Martin Platt.

    Monday, January 7, 2008 4:14 AM
  • I am working as an manager for a electronic design group on consultant company. During my work as an designer I mostly did ASIC and FPGA designs. Programming WPF and C#, is a hobby for me, keeping one leg in the "real world" besides all the daily adminstrative as an manager......... It is not easy to design ASICS's at home,,,,, so I went for software design... 

     

    One the purpose with  MVP pattern, as I understand, is to isolate the functionality of the Presenter in that way it becomes easy to test (basically building the presenter using interfaces and dependency injection). If the presenter invokes the commands,, how do you build your unit test?? All WPF examples I have seen, using wpf command structure, are built around static classes,,,,,,,,,,,, can you use dependency injection/interfaces in that case

     

    //lasse

     

    Monday, January 7, 2008 8:17 PM
  • I remember doing that stuff in the dim distant past...  Interesting stuff, as I remember?

     

    Yes, you are correct in your assumption regarding the MVP pattern.  It allows you to test a Presenter class instead of trying to somehow drive integration tests across the UI, and associated code-behind implementation.

     

    To unit test the command pattern, you would still use interfaces, and quite probably write something similar to a "command manager" to do the command invocation for you.  In that instance your presenter would be invoking code through an interface, which can be mocked, allowing a proper unit test to be performed.  So in the example of an implementation I give above, you would mock out the command manager object, which would be injected into the presenter.  Then you can also seperately unit test your command manager objects in another test class.

     

    Does that help at all?

     

    Martin Platt.

    Tuesday, January 8, 2008 2:51 AM
  •  

    Helps alot.......

    Many tanks...

    //lasse

    Thursday, January 10, 2008 8:52 PM
  • So when the Presenters event handlers are invoked,
    they inturn invoke the Invoker (which you call the command manager) component of the Command pattern that you are saying will be injected into the presenter?

    The Invoker component (generally a class) holds a list of Commands.
    Considering the Presenter has the bisiness logic, should the Presenter implement a Command interface (alternativly, derive from a Command base class)?
    This way the MVP pattern should be keept realativly un-adultarated.
    The Invoker (which is owned by the Presenter) would then just create an instance of the Presenter class (which as we know, owns it's own View and Model interfaces) and call it's Execute method and then add it to it's list for future undo's.
    Or should the Presenter not be a Command?
    If the Presenter is not a Command, how does the concrete Command access the Presenters Model and View?
    Monday, February 2, 2009 11:34 AM