OBSEV: Model View Presenter w/Command (sadek)
I believe in the dumb view, a view that doesn’t contain any logic, not even data binding.
Martin’s MVP together with the MSDN’s article answered several questions, and showed ways to keep the view really clean, but we sacrifice ASP.net’s productivity (DataSource xml mappers, binding mechanism…etc )
But here comes a question, should we really sacrifice all of that? The answer is yes and no!
In my point of view, yes we should avoid putting logic in the page handler, I know that wizards are easier, but they are not healthier! This binding and mapping mechanism live in the code behind, which is not unit tested, that means a source code that can grow buggy, and yet not covered by the tests.
But the good news is, No we don’t have to forget about productivity, as long as we are capable of creating our healthy ways to do it.
In my observation I will try to suggest solutions to these kind of problems, u are free to take it, to like it, or not. Anyway any comment will be helpful to develop the solution.
My solution is inherited from the Command Pattern (GoF).
Back to the dumb view, so we move all the logic to the presenter, and we leave the code behind with properties and events, and we subscribe the presenter to these events.
The DataBinding is done by a command object, actually I am calling it command just to remind that all commands share the same interface, with one method (the Execute() method) and these commands encapsulate the logic.
I don’t guess that sticking to the strict definition of the command pattern is important, we can have the Interface having more than one method, the most important thing, is to share the interface with one or more executing methods .
Following sketch is a class diagram and a sequence diagram that illustrates the interaction between the the page (view) , the presenter, the command and the model.
The benefits of such a model is to avoid code duplication by providing the data binding logic , encapsulating it inside a class, and providing the possibility of implementing an xml mapping to the model (using app.config or even custom configuration files that look like the asp.net DataSource mapping).
Later I will provide another observation , with a more sophisticated example, illustrating more functionalities (update,delete,edit).



Sadek,
Did you heard about Presenter First variation of the MVP pattern? IMHO in non-Web world it’s more natural to create an instance of non-visual component (Presenter class) and after that create a brand new View (or reference an existing View).
Regards,
Igor
Comment by Igor Rozenberg — May 1, 2008 @ 3:25 am