Simple implementation of WinForm's MVP model in C #

Source: Internet
Author: User

MVP mode is similar to the MVC pattern of a design pattern, recently in the project learning process encountered, took a long time finally have some clues, this is the learning process of some notes.
MVP refers to the entity Object Model, view VIW, and business processing presenter. MVP's role is to decouple the relationship between UI rendering, business logic, and data entities. In a common WinForm, the business and interface are written together, typically in the same load or click Method, making the view and controller closely related. In the MVP, we put the interface rendering in the view, that is, the Winfrom form application class, put the business relation in the presenter class, this is the MVP business data class, the final data entity and the database interaction in the model, is the three of them.
In the general MVP, we are actively using the view in the presenter, that is, the interface control form is controlled by the presenter actively. The connection between the two is to register the view in the presenter event, when the interface occurs when the user triggers the event, the event is passed through the view to the presenter, and in the presenter call model data method, Finally presenter calls the instance of the view referenced in the class to change the interface shape, here are some methods of implementation, here highlights the relationship between presenter and view.
First, we define the view's interface class iview, which is a reference to a data entity:

1  Public Interface Iview<t>: IView2{3     getset;} 4 }
Public Interface Iview:iview

Next, define the next-level interface for the view, here are some controls and events that define the views:

1  Public InterfaceImainform<t>: iview<t>2 {3Button Testbutton {Get;}//Define a Mainfrom button reference4TextBox Testtextbox {Get; }//Define a text box reference for Mianform5     EventEventHandler viewloadevent;//Defining a form loading complete execution event6     EventEventHandler buttonsubmitevent;//Defining button Events7     voidShowsubmitdialog ();//to define a custom event8}
Public Interface Imainform:iview

Finally, the implementation of the view class, which is the implementation of the interface methods and properties, including a button and a text box, there is an inherited Mvpform class and presenterbinding features, one will say:

1[Presenterbinding (typeof(Mainformpresenter))]2  Public Partial classMainform:mvpform, imainform<mainformmodel>3 {4      Publicmainform ()5     {6 InitializeComponent ();7     }8 9      PublicMainformmodel Model {Get;Set; }Ten      PublicTextBox Testtextbox {Get{returnTxttext;} } One      PublicButton Testbutton {Get{returnbtnsubmit;} } A  -      Public EventEventHandler viewloadevent; -      Public EventEventHandler buttonsubmitevent; the  -     Private voidMainform_load (Objectsender, EventArgs e) -     { -         if(Viewloadevent! =NULL) viewloadevent (sender, E); +     } -  +      Public voidShowsubmitdialog () A     { atMessageBox.Show ("To submit?"); -     } -  -     Private voidbtnSubmit_Click (Objectsender, EventArgs e) -     { -         if(Buttonsubmitevent! =NULL) buttonsubmitevent (sender, E); in     } -}
Public partial class Mainform:mvpform, Imainform

Define the content of the view, you can look at the presenter, the same, the first interface, and then the implementation, first define the presenter interface:

1  Public Interface where class , View.iview 2 {3     Get ; } 4 }
Public interface Ipresenter:ipresenter where T:class, View.iview

Here we define a presenter abstract class, which is used to unify the presenter classes for each of the different view types, which are defined as follows:

1  Public Abstract classPresenter<t>: ipresenter<t>whereT:class, View.iview2 {3     Private ReadOnlyT view;4 5     //The view here is used as a reference to get the instance of the view in the presenter6     protectedPresenter (T view)7     {8      This. View =view;9     }Ten  One      PublicT View {Get{returnview;} } A}
Public abstract class Presenter:ipresenter where T:class, View.iview

The last is the presenter class for the corresponding view:

1 classMainformpresenter:presenter<view.imainform<model.mainformmodel>>2 {3      PublicMainformpresenter (view.imainform<model.mainformmodel>view)4:Base(view)5     {6View. Model =NewModel.mainformmodel ();7 8View. Viewloadevent + =On_viewload;9View. Buttonsubmitevent + =On_buttonsubmitclick;Ten init (); One     } A  -      Public voidInit () -     { the         //To do something ... -     } -  -      Public voidOn_viewload (Objectsender, EventArgs e) +     { -         //To do something ... +     } A  at      Public voidOn_buttonsubmitclick (Objectsender, EventArgs e) -     { -View.showsubmitdialog ();//to change the shape of a control by invoking the view's method from the view instance -     } -}
class mainformpresenter:presenter<view.imainform>

This defines the interface and implementation of the presenter and view, and here's how to combine the two different modules together, using. NET Features and reflections.
First, the attribute class Presenterbindingattribute is established, and the corresponding relationship between the view and the presenter can be obtained by marking the attribute class and specifying the corresponding presenter class in the implementation class of the view. The class also labels attributes AttributeUsage and inherits the attribute class, and defines two attribute parameters:

1[AttributeUsage (attributetargets.class,allowmultiple =true)]2  Public Sealed classPresenterbindingattribute:attribute3 {4      PublicType Presentertype {Get;Private Set; }5 6      PublicType ViewType {Get;Set; }7 8      PublicPresenterbindingattribute (Type presentertype)9     {TenPresentertype =Presentertype; OneViewType =NULL; A     } -}
Public sealed class Presenterbindingattribute:attribute

The next step is to establish the relationship between the view and the presenter through reflection, which establishes the Performbinding class:

1  PublicIPresenter performbinding (IView viewinstance)2 {3IPresenter presenter =NULL;4Type t = Viewinstance.gettype ();//gets the class type of the view5     Object[] Attrs = T.getcustomattributes (typeof(Presenterbindingattribute),false);//gets a collection of additional attributes on this class6     //iterate through the collection of attributes, find the attributes attached to the presenter type, create an instance of this attribute7     foreach(Presenterbindingattribute PBAinchattrs)8     {9Type Newt = PBA. Presentertype;//Get the Presenter class typeTen         //Create a Presenter instance, where the construction parameter is the object of the view, so that the two establish a connection OneObject obj =Activator.CreateInstance (PBA. Presentertype, viewinstance); APresenter = obj asIPresenter; -     } -     returnpresenter; the}
Public IPresenter performbinding (IView viewinstance)

Well, where is this class performbinding used, generally in the application view launch is to register the instance, here in order to relieve the strong coupling between classes, add an intermediate class. In the implementation class of the preceding view, is the class that inherits from a mvpform, this mvpform makes the class that registers the view and presenter relations, next see Mvpform's implementation:

1  Public Partial classMvpform:form,iview2 {3     Private ReadOnlyPresenterbinder Presenterbinder =NewPresenterbinder ();4      PublicMvpform ()5     {6Presenterbinder.performbinding ( This);//Registration Relationship7     }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, the relationship between the view and the presenter is registered, and the corresponding logic can be written in the presenter. The function of view is to render as UI. When you add the view and presenter implementation classes later, you only need to inherit and implement the corresponding classes and interfaces, and add corresponding class attributes to the view implementation class to achieve the design relationship of the MVP.

Simple implementation of WinForm's MVP model in C #

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.