A Simple MVP Program with code

             Problem

Consider a Human Resource Management System (HRMS) application with functionality to manage employee information. One of the actions would be to edit the information of an existing employee. The following are the main features which be discussed in the below sections.

  1. Once the employee is selected for editing, the status should be shown in green color if the person’s retirement date is NULL
  2. There should be a mechanism to write automated unit tests against this feature.

             Solution

MVP pattern can be used to develop this presentation layer which will help solve the above problem. The following diagram shows the classes, interface and the relation among each.




The diagram consists of the following.
 IEditEmployeeView
This interface will abstract all the data input elements on the View. This will also have methods which will be called by the presenter
 EditEmployeeView
This class will be the concrete implementation of View. This could be an ASP.Net Page or a Windows Form. This class implements IEditEmployeeView. End users will interact with this view. During runtime, presenter will interface with this view to set and get data.
 MockEditEmployeeView
This class will be a mock implementation of View for unit testing purpose. It will implement all properties and methods part of IEditEmployeeView. The EmployeePresenterTest class will utilize this to perform unit testing with the help of a unit testing framework.
EmployeePresenter
This class will be the presenter. The view class (Ex: Web page) manages the controls on the page and it forwards user events to a presenter class. The presenter contains the logic to respond to the events, update the model (business logic and data of the application) and, in turn, manipulate the state of the view.
 EmployeePresenterTest
Unit test script related to EmployeePresenter will be implemented in this class. Mock views need to be utilized to send data in and out.

           Implementation

This section has the implementation details of the above solution. Source code for each component is defined below. In this example, it is assumed that there is only one data entry field on the screen. In this case that is Full Name field. The other field is going to be an indicator which shows whether the employee is active or not. This will be set by the SetStatusColor method.
 IEditEmployeeView
Create an interface with the members for data elements and operation on view.

    interface IEditEmployeeView
    {
        string FullName { get; set; }
        void SetStatusColor(System.Drawing.Color color);
    }

EditEmployeeView
Create a new view (in this case an aspx page) which implements the above view. This class will have both the property and method implemented. The First Name property sets and gets information from the Full name Text box in the page. SetStatusColor method sets the background color of the label.

    public partial class EditEmployeeView : System.Web.UI.Page, IEditEmployeeView
    {
        private EmployeePresenter _presenter = new EmployeePresenter(this);
      
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        #region Implementation of IEditEmployeeView interface

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

        public void SetStatusColor(System.Drawing.Color color)
        {
            lblStatus.BackColor = color;
        }

        #endregion
    }



    <div>
        <asp:TextBox ID="txtFullName" runat="server"></asp:TextBox>
        <br />
        <asp:Label ID="lblStatus" runat="server"></asp:Label>
    </div>
MockEditEmployeeView
This is a mock implementation of the view class. This will be used by the unit test class. Here it is implementing all the attributes in the interface along with some additional once which is going to be helpful for writing unit test. In this class, there is a new property “Color” which is introduced. This property will hold the value set in the SetStatusColor method. Later unit test can Assert this property and see whether it has the right value.

    public class MockEditEmployeeView : IEditEmployeeView
    {
        private string _firstName;
        private System.Drawing.Color _color;

        public string FirstName
        {
            get { return _firstName; }
            set { _firstName = value; }
        }

        public System.Drawing.Color Color
        {
            get { return _color; }
        }

        public void SetStatusColor(System.Drawing.Color color)
        {
            _color = color;
        }
    }

EmployeePresenter
The implementation of EmployeePresenter class will have the functionality which is going to operate on the view and the data.

    public class EmployeePresenter : BasePresenter
    {
        private IEditEmployeeView _view;

        public EmployeePresenter(IEditEmployeeView view)
        {
            _view = view;
        }

        public void Load()
        {
            if (IsEmployeeRetired())
            {
                _view.SetStatusColor(System.Drawing.Color.Red);
            }
            else
            {
                _view.SetStatusColor(System.Drawing.Color.Green);
            }
        }

        public void Save()
        {
            SaveToDatabase(_view.FullName);
        }
       
        private void SaveToDatabase(string fullName)
        {
            // Implement the persistence code
        }

        private bool IsEmployeeRetired()
        {
            // Implement the business logic and return
            // appropriate value.
            return false;
        }
    }
EmployeePresenterTest
This is an example of the unit test code which will be written for testing the Status of an employee. Here Mock view is used for testing. The instance is passed into the Presenter constructor. Then the name of the person who’s details has to be loaded is passed and method is called. Later based on the data the Color property is checked to see whether it is getting changed as per the expectation.


    [TestClass]
    public class EmployeePresenterTest
    {
        MockEditEmployeeView _view = new MockEditEmployeeView();

        [TestMethod]
        public void LoadTest()
        {
            EmployeePresenter _presenter = new EmployeePresenter(_view);
            _view.FullName = "Retired Person";
            _presenter.Load();
            Assert.Equals(_view.Color, System.Drawing.Color.Red);

            _view.FullName = "Active Person";
            _presenter.Load();
            Assert.Equals(_view.Color, System.Drawing.Color.Green );
        }

        [TestMethod]
        public void SaveTest()
        {
        }
    }

Comments

Popular posts from this blog

Introduction To Oracle10g

Insert

Except