jump to navigation

Winforms – Model-View-Presenter – Part IV the Presenter January 29, 2009

Posted by wesaday in Programming.
Tags: , ,
trackback

This is the fourth in a series describing the MVP pattern in Winforms.

The Presenter

Add two classes to the Presenters folder. One named Presenter and the other named DataPresenter. We are going to implement the Presenter as a generic so in case you change the Model or the View we could use the same Presenter or interchange Presenter classes. This will make enhancing the example as a base application easier. So declare the Presenter class as a Generic class where the parameter implements the IBaseInterface interface:

    public class Presenter<T> where T : IBaseInterface

    {

 

    }

Now declare two class properties, one for the Model and one for the View. Since the Presenter, and derivations, is the only one that will be using these properties, make them protected.

        protected static IModel Model { get; private set; }

        protected T View { get; private set; }

Now we are going to create two contructors, a static constructor and an public constructor that takes a parameter. The static constructor is going to initialize the Model for us. The other constructor is going to set the view for us. There also other ways to do this. I have seen where the Model is set in the constructor when the class is instantitated. It’s a matter of style. This way seems “cleaner” to me as the view does not have to know about the Model so the Presenters can use different Models and the View does need to know anything about it.

So our base Presenter class now looks like this:

using Common.Lib.Interfaces;

using Common.Lib.Models;

 

namespace Common.Lib.Presenters

{

    public class Presenter<T> where T : IBaseInterface

    {

        protected static IModel Model { get; private set; }

        protected T View { get; private set; }

        static Presenter()

        {

            Model = new Model();

        }

 

        public Presenter(T view)

        {

            View = view;

        }

    }

}

Okay now. For the real Presenter class. Open up the DataPresenter class. The first thing to do is to modify the class definition to derive from the Presenter class and define what the interface for our view that we are going to handle.

    public class DataPresenter : Presenter<IMainView>

    {

    }

Then this class simply needs to define a constructor and a public method to call to get the data from the Model and give it to the View.

 

        public DataPresenter(IMainView view)

            : base(view)

        {         

        }

 

        public void Display(string filename)

        {

            if (View != null) View.CSVData= Model.GetData(filename);

        }

The constructor is passed a IMainView interface which is also passed along to the base class. This also give access to the Model that is instantiated in the base class. The method simply calls the Model’s GetData method passing along the filename to get the data from and giving that back to the View. This is it for the class library. All we need to do now to finish this up is to finish our main view.

This is the Visual Studio 2008 project so far. Change the name from .doc to .zip before you open it. This is a wordpress requirement. presenterzip

Advertisements

Comments»

1. Winforms - Model-View-Presenter - A tutorial, the introduction « Wes Aday’s Weblog - March 4, 2009

[…]       Part IV […]

2. Winforms – Model-View-Presenter – A tutorial « Wes Aday’s Weblog - May 11, 2009

[…]        Part IV […]

3. vikram - January 21, 2010

Nice article on MVP. I am going to create a small application using this pattern and was looking for examples; your code really helps. Thanks.

wesaday - January 21, 2010

Thank you. Glad it was of help to you.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: