locked
MVP Design Pattern RRS feed

  • Question

  • User1543306012 posted

    Dear All,

    My company wish to use MVP Pattern for Developing an ERP which will contain HR, Accounts, Invertory, Purchase, Engineering e.t.c Modules.

    What advantage will i get using this pattern.

    Please Consider Real Life Scenario.

     

    Another. Can anyone tell me the Object Reuse Concept with real life scenario in Object Oriented Programming such as C#.

    Any Satisfactory answer will be highly appreciated.

    Wednesday, March 10, 2010 3:09 AM

Answers

  • User437720957 posted

    In this case, you could use an abstract base presenter:

    public abstract class PresenterBase<TView>

    and an abtract base Page/View

    public abstract class PresenterPage<TPresenter,TView> : Page, TView

    Please study the different approaches to this in the framworks I mentioned before.

    Also, object reuse should not be a primary goal. It may be a powerful tool, but when it becomes the primary goal it tends to lead to over architected, hard to maintain solutions.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 14, 2010 4:08 PM

All replies

  • User437720957 posted

    What advantage will i get using this pattern.

    The characteric you will gain is a clear separation between business logic, presentational logic and and view implementation. If you use that characteric properly, the advantage will be code that is testable using common unit test practices and easier to maintain. My experience is that MVP works quite well, but that it requires quite a bit of training before you feel that it's actually worth the extra work. An MVP framework such as WebFormsMVP or WCSF/MVP may be a good starting point.



    Thursday, March 11, 2010 7:17 PM
  • User1543306012 posted

    Thanks for your Reply.

    What advantage will i  get if business logic, presentation logic and view implementation is separate. Is it mean one business logic programmer would not see the presentation logic programmer work.

    since i am new can u tell me about why unit testing required.


    Friday, March 12, 2010 4:09 AM
  • User437720957 posted

    What advantage will i  get if business logic, presentation logic and view implementation is separate. Is it mean one business logic programmer would not see the presentation logic programmer work.

    No, it just means that the business code only deals with business logic, the presentation code (code behind) only with presentation logic etc. The separation is good for clarity, and most importantly, it allows you to make modifications and improvements to one part of the code, without necessarily affecting all the rest.

    since i am new can u tell me about why unit testing required.

    It's not required, but it sure is a good practice to use, in order to provide working code which can be modifed and verified to work.

    At the risk of sounding a bit rude, I think that if you don't feel comfortable with the concepts of separation and unit tests, you will not appreciate MVP at all. In fact, I think you will hate it. I'm not saying you should stay away from it, but learn the concepts of "separation of concerns", "unit tests" before taking the leap to MVP. If you don't, I'm afraid that MVP will just make you less productive.

    Friday, March 12, 2010 7:47 PM
  • User1543306012 posted

     Thank you very much gunteman.

    How the MVP Pattern meet the requirement for object reuse.

    Since i have to build separate Interface and Class for Presenter, View and Model for each form.

    How will we use object reuse concept.

    Codebehind:

    using System.Web.UI;
    using System;
    using IBS_HIS_Regency.Presentation.SubForms.Interfaces;
    using IBS_HIS_Regency.Presentation.SubForms;
    using IBS_HIS_Regency.DTO;
    using IBS_HIS_Regency.Web.Controls;
    
    public partial class ViewAddAcademicBoardInformation : Page, IViewAddAcademicBoardInfoView
    {
        private ViewAddAcademicBoardInfoPresenter _presenter;
    
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            _presenter = new ViewAddAcademicBoardInfoPresenter(this);
    
            this.lboxAcadBoard.SelectedIndexChanged += delegate
            {
                _presenter.DisplayAcademicBoardInfoDetails();
            };
    
            this.btnSave.Click += delegate
            {
                _presenter.SaveAcademicBoardInfo();
            };
    
            this.btnAdd.Click += delegate
            {
                _presenter.PrepareNewRecordMode();
            };
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                _presenter.Initialize();
            }
        }
    
        #region IViewAddAcademicBoardInfoView Members
    
        public bool IsNewRecordMode
        {
            get { return Convert.ToBoolean(hidIsNewRecordMode.Value); }
            set { hidIsNewRecordMode.Value = value.ToString(); }
        }
    
        public string LabelMessage
        {
            set { lblMsg.Text = value; }
        }
    
        public ILookupList AcademicBoardList
        {
            get { return new WebLookupList(this.lboxAcadBoard, false); }
        }
    
        public string AcademicBoardName
        {
            get { return txtAcadBoard.Text; }
            set { txtAcadBoard.Text = value; }
        }
    
        public string ListBoxAcademicBoardName
        {
            get { return lboxAcadBoard.Items[ListBoxSelectedIndex].Text; }
        }
    
        public string ListBoxAcademicBoardID
        {
            get { return lboxAcadBoard.SelectedValue; }
        }
    
        public int ListBoxSelectedIndex
        {
            get { return lboxAcadBoard.SelectedIndex; }
        }
    
        #endregion
    }
     
    Presenter Interface:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using IBS_HIS_Regency.DTO;
    
    namespace IBS_HIS_Regency.Presentation.SubForms.Interfaces
    {
        public interface IViewAddAcademicBoardInfoView
        {
            ILookupList AcademicBoardList { get; }
    
            bool IsNewRecordMode { get; set; }
            string LabelMessage { set; }
    
            string AcademicBoardName { get; set; }
    
            string ListBoxAcademicBoardName { get; }
    
            string ListBoxAcademicBoardID { get; }
    
            int ListBoxSelectedIndex { get; }
        }
    }
    

    Presenter Class:

    using System;
    using IBS_HIS_Regency.Presentation.SubForms.Interfaces;
    using IBS_HIS_Regency.Task.SubForms.Interfaces;
    using IBS_HIS_Regency.Task.SubForms;
    using IBS_HIS_Regency.Task;
    using IBS_HIS_Regency.Task.Interfaces;
    
    namespace IBS_HIS_Regency.Presentation.SubForms
    {
        public class ViewAddAcademicBoardInfoPresenter
        {
            private readonly IViewAddAcademicBoardInfoView _view;
            private readonly IAddAcademicBoardInfoTask _task;
            private readonly IEmployeeEducationInfoTask _taskEmployeeEducationInfo;
    
            public ViewAddAcademicBoardInfoPresenter(IViewAddAcademicBoardInfoView view)
                : this(view, new AddAcademicBoardInfoTask(), new EmployeeEducationInfoTask())
            {
            }
            public ViewAddAcademicBoardInfoPresenter(IViewAddAcademicBoardInfoView view, IAddAcademicBoardInfoTask task,
                IEmployeeEducationInfoTask taskEmployeeEducationInfo)
            {
                this._task = task;
                this._view = view;
                this._taskEmployeeEducationInfo = taskEmployeeEducationInfo;
            }
    
            public void DisplayAcademicBoardInfoDetails()
            {
                int? selectedAcademicBoardId = SelectedAcademicBoardID;
                if (selectedAcademicBoardId.HasValue & selectedAcademicBoardId > 0)
                    _view.AcademicBoardName = _view.ListBoxAcademicBoardName;
            }
    
            public void SaveAcademicBoardInfo()
            {
                string result = _task.SaveAcademicBoardInfo(_view.ListBoxAcademicBoardID, _view.AcademicBoardName,
                    _view.IsNewRecordMode);
                Initialize();
                _view.LabelMessage = result;
            }
    
            private int? SelectedAcademicBoardID
            {
                get
                {
                    string selectedId = _view.AcademicBoardList.SelectedItem.Value;
    
                    if (String.IsNullOrEmpty(selectedId)) return null;
    
                    int? id = null;
    
                    try
                    {
                        id = int.Parse(selectedId.Trim());
                    }
                    catch (FormatException) { }
    
                    return id;
                }
            }
    
            public void PrepareNewRecordMode()
            {
                _view.LabelMessage = string.Empty;
                _view.AcademicBoardName = string.Empty;
                _view.IsNewRecordMode = true;
            }
    
            public void Initialize()
            {
                _taskEmployeeEducationInfo.GetAcademicBoardList().BindTo(_view.AcademicBoardList);
            }
        }
    }
    


     



     

    Saturday, March 13, 2010 1:44 AM
  • User437720957 posted

    In this case, you could use an abstract base presenter:

    public abstract class PresenterBase<TView>

    and an abtract base Page/View

    public abstract class PresenterPage<TPresenter,TView> : Page, TView

    Please study the different approaches to this in the framworks I mentioned before.

    Also, object reuse should not be a primary goal. It may be a powerful tool, but when it becomes the primary goal it tends to lead to over architected, hard to maintain solutions.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 14, 2010 4:08 PM
  • User1543306012 posted

    Thank you very much, gunteman.

    It make my concept little clear.


    Saturday, March 20, 2010 6:07 AM