MVP(Do not use MVC) for Windows Form Applications

MVP is a derivation of the MVC pattern.

  • M - Model
  • V - View
  • P - Presenter

In MVP, view receives the events from the UI and calls the presenter as needed.Presenter is responsible for updating the model and view as needed.

MVP vs MVC
MVPMVC
Model-View-PresenterModel-View-Presenter
Presenter is responsible for binding model and viewControllers are based on behaviors and can be shared across views
Usually view to presenter map one to one. Complex views may have multi presenters.Can be responsible for determining which view to display
The view is in charge.The controller is in charge.

There are two types in MVP.This image explains those perfectly.

Now lets look at the example (Passive View),
We are going look at a Inventory controller system.Lets take the "Item Purchasing" screen as the example.



Below is the code for the view.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MangoIC.Plugins;
using MangoIC.controllers;

namespace MangoIC.views
{
    public partial class PurchasingView : Form, IPurchasingView
    {
        public PurchasingView()
        {
            InitializeComponent();
        }

        public string Name
        {
            get { return this.name.Text; }
            set { this.name.Text = value; }
        }
        public string Model
        {
            get { return this.model.Text; }
            set { this.model.Text = value; }
        }
        public string Make
        {
            get { return this.make.Text; }
            set { this.make.Text = value; }
        }
        public string Color
        {
            get { return this.color.Text; }
            set { this.color.Text = value; }
        }
        public string Size
        {
            get { return this.size.Text; }
            set { this.size.Text = value; }
        }
        public string Price
        {
            get { return this.price.Text; }
            set { this.price.Text = value; }
        }
        public string SalesPrice
        {
            get { return this.salesPrice.Text; }
            set { this.salesPrice.Text = value; }
        }
        public PurchasingPresenter Presenter
        { private get; set; }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            Presenter.savePurchase();
        }


    }
}

Getters and Setters are used by the presenter to retrieve data from the UI and also to populate data in the UI.


Below is the code for the Presenter
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MangoIC.views;
using MangoIC.models;
using MangoIC.dao;

namespace MangoIC.controllers
{
    public class PurchasingPresenter: IPurchasingPresenter
    {
        private readonly PurchasingView _view;
        private readonly PerchasingDao _pdao;

        public PurchasingPresenter(PurchasingView view)
        {
            _view = view;
            view.Presenter = this;
            _pdao = new PerchasingDao();
        }


        public void savePurchase()
        {
            Console.WriteLine("saving purchase..");
            
            StockItem stockItem = new StockItem
            {
                Name = _view.Name,
                Model = _view.Model,
                Make = _view.Make,
                Color = _view.Color,
                Size = _view.Size,
                Price = _view.Price,
                SalesPrice = _view.SalesPrice
            };
            _pdao.saveStock(stockItem);
        }
    }
}

In here we are creating a Item using the details we retrieve from the view and save it the DB using a DAO layer inside the savePurchase() function.

Download the example (Visual Studio 2010) : Link
(You may have to download some of the dependencies when executing the program)

Query for the table creation;
CREATE TABLE `stocks1` (
  `Id` int(11) NOT NULL,
  `Name` varchar(45) DEFAULT NULL,
  `Make` varchar(45) DEFAULT NULL,
  `Color` varchar(45) DEFAULT NULL,
  `Size` varchar(45) DEFAULT NULL,
  `Price` varchar(45) DEFAULT NULL,
  `Sales_Price` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`Id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;


Comment below, if you have any questions.
SHARE

Harsha Jayamanna

    Blogger Comment
    Facebook Comment

3 comments: