C # simple implementation of winform MVP Mode,

Source: Internet
Author: User

C # simple implementation of winform MVP Mode,

The MVP mode is a design mode similar to the MVC mode. Recently, I encountered a problem in the project learning process. After a long time, I finally got some eyebrows. This is some notes in the learning process.
MVP refers to the entity object Model, view Viw, and business processing Presenter. The role of MVP is to decouple the relationships between UI rendering, business logic, and data entities. In a common winform, the business and interface are written together. Generally, they are in the same Load or Click method, so that the View and Controller are closely linked. In MVP, we place the interface rendering in the View, that is, the form application class of winfrom, and the business relationship in the Presenter class, which is the business data class of MVP; finally, the interaction between the data entity and the database is placed in the Model, and the three perform their respective duties.
In general MVPs, we actively use the View in the Presenter, that is, the interface control form is controlled by the Presenter. The connection between the two is to register the View event in the Presenter. When a user triggers an event on the interface, the event is transmitted to the Presenter through the View, and call the Model data method in the Presenter. Finally, the Presenter calls the View instance referenced in the class to change the Interface format. Below are some implementation methods, the relationship between Presenter and View is highlighted here.
First, we define the IView interface class of the View, which references a data object:

1 public interface IView <T>: IView2 {3 T Model {get; set;} 4}Public interface IView: IView

 

Next, define the next interface of the View. Here we define some controls and events of the View:

1 public interface IMainForm <T>: IView <T> 2 {3 Button TestButton {get;} // The Button defining MainFrom references 4 TextBox TestTextBox {get ;} // define the MianForm text box to reference 5 event EventHandler ViewLoadEvent; // define the event 6 event EventHandler ButtonSubmitEvent after the form is loaded; // define the button event 7 void ShowSubmitDialog (); // custom event 8}Public interface IMainForm: IView

 

Finally, the View implementation class contains the implemented interface methods and attributes, including a button and a text box. Here there is an inherited MvpForm class and PresenterBinding feature. Let's talk about it later:

1 [PresenterBinding (typeof (MainFormPresenter)] 2 public partial class MainForm: MvpForm, IMainForm <MainFormModel> 3 {4 public MainForm () 5 {6 InitializeComponent (); 7} 8 9 public MainFormModel Model {get; set;} 10 public TextBox TestTextBox {get {return txtText;} 11 public Button TestButton {get {return btnSubmit ;}} 12 13 public event EventHandler ViewLoadEvent; 14 public event EventHa Ndler ButtonSubmitEvent; 15 16 private void MainForm_Load (object sender, EventArgs e) 17 {18 if (ViewLoadEvent! = Null) ViewLoadEvent (sender, e); 19} 20 21 public void ShowSubmitDialog () 22 {23 MessageBox. Show ("to submit? "); 24} 25 26 private void btnSubmit_Click (object sender, EventArgs e) 27 {28 if (ButtonSubmitEvent! = Null) ButtonSubmitEvent (sender, e); 29} 30}Public partial class MainForm: MvpForm, IMainForm

 

After defining the View content, you can take a look at the Presenter. Similarly, there are interfaces first and then implementations. First, define the Presenter interface:

1 public interface IPresenter <T>: IPresenter where T: class, View. IView2 {3 T View {get;} 4}Public interface IPresenter: IPresenter where T: class, View. IView

 

A Presenter abstract class is defined here to unify the Presenter classes corresponding to different views. Its definition is as follows:

1 public abstract class Presenter <T>: IPresenter <T> where T: class, View. IView 2 {3 private readonly T view; 4 5 // the view here is used as a reference to get the View instance 6 protected presenter (T view) 7 {8 this. view = view; 9} 10 11 public T View {get {return view;} 12}Public abstract class Presenter: IPresenter where T: class, View. IView

 

Finally, it is the Presenter class of the corresponding View:

1 class MainFormPresenter: Presenter <View. IMainForm <Model. mainFormModel> 2 {3 public MainFormPresenter (View. IMainForm <Model. mainFormModel> view) 4: base (view) 5 {6 view. model = new Model. mainFormModel (); 7 8 view. viewLoadEvent + = On_ViewLoad; 9 view. buttonSubmitEvent + = On_ButtonSubmitClick; 10 init (); 11} 12 13 public void init () 14 {15 // To Do something... 16} 17 18 public void On_ViewLoad (object sender, EventArgs e) 19 {20 // To Do something... 21} 22 23 public void On_ButtonSubmitClick (object sender, EventArgs e) 24 {25 View. showSubmitDialog (); // use the view instance to call the view method to change the control format 26} 27}Class MainFormPresenter: Presenter <View. IMainForm>

 

The Presenter and View interfaces and implementations are defined here. The following describes how to combine the two different modules. Here, the features and reflection of. net are used.
First, establish the PresenterBindingAttribute feature class. By marking the feature class and the specified Presenter class in the View implementation class, you can obtain the correspondence between the View and Presenter, the AttributeUsage class and the inherited Attribute class must also be marked, and two Attribute parameters must be defined at the same time:

1 [AttributeUsage (AttributeTargets. class, AllowMultiple = true)] 2 public sealed class PresenterBindingAttribute: Attribute 3 {4 public Type PresenterType {get; private set;} 5 6 public Type ViewType {get; set ;} 7 8 public PresenterBindingAttribute (Type presenterType) 9 {10 PresenterType = presenterType; 11 ViewType = null; 12} 13}Public sealed class PresenterBindingAttribute: Attribute

 

Next, we will establish the relationship between the view and the presenter through reflection. Here we will establish the javasmbinding class:

1 public IPresenter extends mbinding (IView viewInstance) 2 {3 IPresenter presenter = null; 4 Type t = viewInstance. getType (); // obtain the class type of the view. 5 object [] attrs = t. getCustomAttributes (typeof (PresenterBindingAttribute), false); // get the additional feature set on this class 6 // traverse the feature set to find additional features of the Presenter type, use this feature to create instance 7 foreach (PresenterBindingAttribute pba in attrs) 8 {9 Type newt = pba. presenterType; // obtain the Presenter class type 10 // create a Presenter instance. The construction parameter here is the View Object, so that the two establish a connection 11 Object obj = Activator. createInstance (pba. presenterType, viewInstance); 12 presenter = obj as IPresenter; 13} 14 return presenter; 15}Public IPresenter extends mbinding (IView viewInstance)

 

In this case, where to use this kind of ingress mbinding, the instance is generally registered when the application View is started. Here, an intermediate class is added to remove the strong coupling between classes. In the previous View implementation class, it is inherited from an MvpForm class. This MvpForm enables the class to register the relationship between View and Presenter. Next, let's look at the implementation of MvpForm:

1 public partial class MvpForm: Form, IView2 {3 private readonly PresenterBinder presenterBinder = new PresenterBinder (); 4 public MvpForm () 5 {6 presenterBinder. performBinding (this); // registration link 7} 8}Public partial class MvpForm: Form, IView

 

In this way, the relationship between View and Presenter is established. Each time the View page is started, the constructor of the parent class MvpForm is executed to register the relationship between View and Presenter, the corresponding logic can be written in the Presenter. The View function is to render the UI. When you add a View and Presenter to implement a class, you only need to inherit and implement the corresponding classes and interfaces, and add corresponding class features to the View implementation class to implement the MVP design relationship.

Related Article

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.