Geeks With Blogs

News Google

Nick Harrison Blog<Nick>.Next()

Model View Presenter / Passive View Benefits

A hot topic near and dear to my heart is Model View Presenter (MVP) or Passive View design pattern.   This design pattern forms a good model for keeping a strong separation between UI logic and business logic.

In the MVP model, the data access objects constitute the model, the View is the UI components kept as simple as they can be and the Presenter is where the business logic is.   These three components interact with other through interfaces to further reinforce the logical separation.  

The presenter should have no idea where the Model gets its data or how the data is persisted.    The Model defines an interface that must be implemented.   Anything that implements this interface can serve as the model.  This means that we can switch out the data storage strategy without having to change anything in the presenter.

The presenter should also have no idea about the nature of the View.    The View implements an interface that exposes all of the methods, properties, and events that the Presenter will need and the Presenter implements the business logic through this interface.   This means that View may actually been a Win Form, Web Form, a server control, a user control, a SharePoint web part, or even a class library with no UI components.   The only requirement is that the View must implement the interface.

Among other things, this gives us these benefits:

·        Simplified testing.   The Presenter can be tested without having to worry about the database or the UI.   Both the Model and View can be replaced with “Mock” objects designed to simplify testing
·        Business logic implemented in the Presenter can easily be retargeted to any number of UIs.    Once the business logic is implemented and tested, the original View can be converted from a Win Form to Web Form or a Mobile Web Form, etc without impacting the Presenter as long as the new Views continue to implement the same interface
·        The Model can be switched out to for demo purposes to read from local flat files or static XML files.   As long as the new Models implement the original interfaces, neither the Presenter nor the Views needs to change.
·        If you have multiple lines of business with similar business logic but very different data sources, new Models can be implemented targeting the new data sources.   As long as the new models implement the original interfaces, neither the Presenter nor the View needs to change.  
Future posts will explore what some of these benefits mean as well as ferret out some additional benefits.

What benefits do you see with the MVP pattern?  What hurdles have you had to face?

 
Posted on Monday, September 22, 2008 6:44 PM | Back to top


Comments on this post: Model View Presenter / Passive View Benefits

# re: Model View Presenter / Passive View Benefits
Requesting Gravatar...
I can clearly see the advantages of Passive View. I keep getting confused about what should implement an interface. During a recent code review the other developers asked my why I had implemented an interface for the view, model and presenter. I couldn't explain it well. Please explain what the implementation of the interfaces does in passive view? I am used to thinking of interfaces being used to enforce a pattern in similar objects but it seems the interface is used to bring something else to the table in passive view. Currently much of the code I maintain is the old "everything in the code behind style". I am having a hard time convincing others that using this design pattern is worth the extra time it takes to get a peice of functionality out the door.
Left by technodrone on Sep 24, 2008 5:54 AM

# re: Model View Presenter / Passive View Benefits
Requesting Gravatar...
Hi Nick,

I personally like the MVP more than MVC too. A detailed comparison of those two can be found here as a part of the MVC# project.

Kind regards,
--
Oleg Zhukov
Left by Oleg Zhukov on Oct 30, 2008 6:31 AM

comments powered by Disqus

Copyright © Nick Harrison | Powered by: GeeksWithBlogs.net