MVP getting started

Source: Internet
Author: User
Shortly after being transferred to C # development, I found a model view presenter model similar to the MVC pattern in Java. I personally feel that the MVP pattern is really similar to the MVC pattern, I will not explain MVC here. I will focus on MVP. The m in MVP is actually the same as m in MVC, it is a model that encapsulates the computing relationship between core data, logic, and functions (we will not focus on it here), while V is a view (form ), P in my opinion, it encapsulates all the operations in the form, and responds to user input and output, events, etc. My personal feeling is similar to C in MVC. The difference is that MVC is a system-level architecture, MVP is used on a specific page. That is to say, MVP is much more flexible than MVC, and its implementation is extremely simple. Next, let's talk about an instance. Let's first look at the source code: a total of three files:
"Interface: itestmvpview", "form: frmtestmvp", "presenter tew.vppresenter"
First look at the itestmvpview code:

Namespace test_mvp
{
// Declare the delegate
Public Delegate void button#click ();

Interface itestmvpview
{
// Declare controls
Textbox textbox1 {Get ;}
// Event
Event button#click click;
}
}

Form code:

Namespace test_mvp
{
Public partial class frmte1_vp: form, itestmvpview
{
Private testmvppresenter _ tew.vppresenter;

Public frmte1_vp ()
{
Initializecomponent ();
// Note that when constructing a presenter, you need to pass it over.
This. _ tew.vppresenter = new tew.vppresenter (this );
}

// Click the button event
Private void button#click (Object sender, eventargs E)
{
If (Click! = NULL)
{
Click ();
}
}

# Region itestmvpview Member

// Method for implementing interface attributes
Public textbox textbox1
{
Get {return this. textbox1 ;}
}

// Delegate event
Public new event button#click;

# Endregion
}
}

Presenter code:

Namespace test_mvp
{
 
Class tew.vppresenter
{
Private itestmvpview _ tew.vpview;

// Constructor to pass in the View Interface
Public tew.vppresenter (itestmvpview tew.vpview)
{
This. _ tew.vpview = tew.vpview;
This. initevent ();
}

// Load the delegate event
Private void initevent ()
{
This. _ tew.vpview. Click + = new button=click (_ tew.vpview_click );
}

// Process the event
Void _ tew.vpview_click ()
{
If (checkvalue ())
{
This. showmessage (this. _ tew.vpview. textbox1.text );
}
Else
{
This. showmessage ("the input value cannot be blank! ");
This. _ tew.vpview. textbox1.focus ();
}
}

// Check whether the input value of testbox1 is valid
Private bool checkvalue ()
{
If (this. _ tew.vpview. textbox1.text. tostring () = "")
{
Return false;
}
Return true;
}

Private void showmessage (string message)
{
MessageBox. Show (Message );
}
}

From the code above, we can see that the events and controls declared in the interface must process the information in the form in the presenter, so do the Textbox Control and delegate events, they all need to be processed in P. It is important that the form must implement the iview interface and create a new P, and pass itself as a parameter to P, so that members of the form can be accessed using polymorphism in P. In addition, we can use delegation or other technologies in the form to put all responses to user input and output and events in P for processing. Because p does not know the form, but only iview, we can create multiple different forms to correspond to one P, as long as their business logic and event processing are the same, in other words, P does not know whether the form is windwosform or webform. He only knows the iviw interface, as long as the form implements the iview interface.

Therefore, if you can use MVP for programming, the form will become very simple, and even allow inexperienced coding personnel to take charge of the UI design of the form, which is really convenient, in addition, it will lay a good technical foundation for mutual conversion between webform and windowsform in the future. Even if you can develop two sets of things in parallel, adopting the MVP mode will make this extremely simple.

The example mentioned in this article can be downloaded from csdn:
Http://download.csdn.net/source/279995
If you are interested in the MVP design model, you can search for it on msnd. Microsoft has a special article to introduce it. You are also welcome to leave a message to discuss it with me.

 

 

 

 

I think you are not an MVP mode, but a simple variant of the MVC mode. The MVP mode is used to strip the UI Layer to achieve Test-driven development.
#Mc1035 published on 11:43:48 IP: 60.208.111 .*
Yes, when I first wrote this article, I did write something similar to MVC. However, I want to express that the UI Layer is stripped, And I think MVPs must follow one thing, it means that no code directly controls the UI control appears in the presenter, And the meaning is lost.
#Mc1035 published on 11:45:00 IP: 60.208.111 .*
According to the following standards, I have written an error in this MVP code. You are welcome to correct it.
# Yundao tianxiaoPosted on 15:55:15 IP: 219.238.183 .*
It means that no code directly controls the UI control appears in the presenter, And the meaning is lost.

I also think it is. In this way, the UI is involved, so we cannot test it independently. We can perform this test to see if your MVP application's prensenter code on the web and winform needs to be changed.

#Mc1035 published on 16:55:02 IP: 60.208.111 .*
I guess the code above won't work. You can try
Textbox textbox1 {Get ;}
Changed to string textboxtext {Get; set ;}
In the UI (view), replace
String textboxtext
{
Get {return this. textbox1.text ;}
Set {This. textbox1.text = value}
}
Then replace the presenter Code accordingly, for example
This. _ tew.vpview. textbox1.text is replaced by this. _ tew.vpview. textboxtext, so OK.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.