Model View Presenters: Introduction

26 Jan 2012

Table of Contents

What is MVP?

I first came across MVP in Jeremy Miller’s Build Your Own Cab series, and have been using and improving how I work with this style ever since. Model View Presenters tend to come in one of two forms: Passive View, and Supervising Controller. I am a fan of the Passive View variety, primarily for the testing aspect, but also as I find it provides me with the best level of separation.

The code ends up structured like this:

MVP

The View contains only code that enables control population and feedback. This means the odd For loop or similar to fill a grid from a property, or feedback object construction, along the lines of new SelectedRowData {ID = (int)row.Tag, Name = row[0].Value}. However, it would not contain any decision logic.

The Presenter contains code to transform Model data to something the View can display, and vice-verse. It also contains any view logic, such as if a CheckBox is selected, then a MenuItem becomes disabled.

The Model is the data to be displayed. This can either be an abstraction that encompasses several entities and business logic, or can be some entities themselves.

« Working with XmlTextWriter Model View Presenters: Presenter to View Communication »