Tuesday, 18 March 2014

MVP (Model View Presenter)

As a developer most of us are used to write some or most of the logic in the UI layer which gave rise to issue where the code of the code-behind is not reusable because of which there is repetition of the same logic multiple times which give rise to another issue so if the logic written in one code behind has some issue the same might exist is the repeated code so there is possibility that as a developer you might need to make changes at all those places and there might be a possibility the you might forget to change the code at few places. Apart from this if there is issue you need to publish the whole website and give it to the client instead of just giving the patch MVP resolves both the issue. The code in the UI layer of an application is very difficult to test without either running the application manually important aspect of MVP pattern is to unit test your whole interface as well as business layer. The main objective of MVP is Separation of Concern or Loosely Couple the entire system, so that we can test each part of the application. 
  • The model is an interface defining the data to be displayed.
  • The view is a passive interface that displays data (the model) and routes user commands (events) to the presenter to act upon that data.
  • The presenter acts upon the model and the view. It retrieves data from repositories (the model), and formats it for display in the view.

Using the Code

In this demo I would be showing the traditional “hello world” example on click of button.












So the first thing which did I created a button and a label in the Default page


<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">

    <asp:Button ID="MessageButton" runat="server" Text="Button" />

    <ol class="round">
        <li>
            <asp:Label ID="MessageLable" runat="server"></asp:Label>
        </li>
    </ol>
</asp:Content>


The Next step is to create a view

public interface DefaultView
    {
        Button HelloMessage { get; }
        Label ClientMessege { get; }

    }


So what I have done is that I have create a view which has to attributes of the same type which I have used in the Default.aspx page

The next step is to create a presenter with a parameter of view type and implement DefaultView in the Default.aspx 


public class DefaultPresenter

    {
        DefaultView _view;
        public DefaultPresenter(DefaultView view)
        {
            _view = view;
        }
    }


public partial class _Default : Page, DefaultView

    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public Button HelloMessage
        {
            get { return MessageButton; }
        }

        public Label ClientMessege
        {
            get { return MessageLable; }
        }
    }

The next step is to create the object of the presenter and pass the _Default object as a parameter


public partial class _Default : Page, DefaultView

    {
        protected override void OnInit(EventArgs e)
        {
            DefaultPresenter presenter = new DefaultPresenter(this);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        public Button HelloMessage
        {
            get { return MessageButton; }
        }

        public Label ClientMessege
        {
            get { return MessageLable; }
        }
    }

The next step is to create the button click and set the text of the label


public class DefaultPresenter

    {
        DefaultView _view;
        public DefaultPresenter(DefaultView view)
        {
            _view = view;
            _view.HelloMessage.Click += HelloMessage_Click;
        }

        void HelloMessage_Click(object sender, EventArgs e)
        {
            _view.ClientMessege.Text = "Hello World";
        }
    }


Now run the project and click on the button











You can also use the model to display the data from the database


About Author:
Steven Pinto is technology geek and loves to write on technology. He works in Systems Plus Pvt. Ltd. and actively contributes to technology. To more of interesting topics written by Steven, follow http://mad4teck.blogspot.in/

1 comment: