What is MVPVM design pattern? How to implement in Winforms application

+1 vote
asked Jun 9, 2013 in Winforms by Selva

How Model View Presenter View Model(MVP-VM) design pattern works? What is the responsibility of each part of MVP-VM. What are the benefits we will get by this pattern and How to implement using Winform?

Share

1 Answer

+1 vote
answered Jun 10, 2013 by anonymous
selected Jun 11, 2013 by administrator
 
Best answer

MVP-VM is one of the design pattern used to implement separation of concerns. This is similar to MVC, MVVM and MVP. More specifically, a modified version of MVP (Model View Presenter.)

MVP-VM will be almost same as MVP but in addition to that, it will also have View Model in the design. The View model is nothing but a model that is specific to the View, to provide what is required to display in view and what has to be passed back to Model.

Model is nothing but our business object. So, this will have properties and functionality which are very specific to Business Domain. That is what the responsibility for Model.

View is nothing but some which are visible to users and its responsibility is to display information to the user and read information from the user. Modify the appearance of the screen etc. It should not do anything except this.

Presenter will be acting like a bridge between, View, ViewModel and Model. This will take care of getting data from Model and pass it to View through View model. It may be little confusing because it will look like both ViewModel and Presenter both are doing the same functionality. But there is a small difference. ViewModel will have only the properties which are used to Bind data to View and hold data that comes from View. Presenter will have other implementations to talk to business layer and get domain model and update view model to pass the information to View.

Actually the MVP-VM can be implemented in different ways. But the main goal is to separate the functionalities. This majorly helps to do unit testing and better code maintainability for long run.

In this design, View will not know anything about the model. So, View does not care about the data source.
Presenter will not know anything about the view. Instead, it will would with an interface which is implemented by the View. So, my view can be my Winforms or Unit testing or any other interface which is implementing the same interface.
View Model does not need to have any private properties to hold information. Instead it can use the Domain Model properties in the Get and Set method of ViewModel Properties. Below is the small example to understand how this design pattern can be implemented.

View:

namespace MVPVM
{
    public partial class Form1 : Form, IView
    {
        CustomerInfoPresenter _presenter = null;

        public Form1()
        {
            InitializeComponent();

            _presenter = new CustomerInfoPresenter(this);

        }

        public CustomerInfoViewModel ReadData()
        {
            return new CustomerInfoViewModel() { FirstName = "TestName" };
        }
    }
}

Presenter:

namespace MVPVM
{
    public class CustomerInfoPresenter
    {
        IView _view = null;

        public CustomerInfoPresenter(IView view)
        {
            _view = view;
        }

        public void Save()
        {
            CustomerInfoViewModel viewModel = _view.ReadData();
            CustomerInfoModel customerInfoModel = viewModel.CustomerInfoModel;
            //Call Business Layer by passing customerInfoModel
        }
    }
}

View Model:

namespace MVPVM
{
    public class CustomerInfoViewModel
    {
        readonly CustomerInfoModel _customerInfoModel = null;

        public CustomerInfoViewModel() : this(new CustomerInfoModel())
        {
        }

        public CustomerInfoViewModel(CustomerInfoModel customerInfoModel)
        {
            _customerInfoModel = customerInfoModel;
        }

        public string FirstName 
        { 
            get
            {
                return _customerInfoModel.FirstName;
            }
            set
            {
                _customerInfoModel.FirstName = value;
            }
        }

        public CustomerInfoModel CustomerInfoModel 
        { 
            get
            {
                return _customerInfoModel;
            }
        }
    }
}

Model:

namespace MVPVM
{
    public class CustomerInfoModel
    {
        public string FirstName { get; set; }
    }
}

Hope this sample helps you to understand. Please feel free to post comment if you have any more question.

Your answer

Preview

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
site design / logo / content © 2013 - 2015 pinfaq.com
...