Learn the NET MVC framework Secret Note-Example demo: Application of SC Mode

Source: Internet
Author: User

Example Demo:SCapplication of the pattern

For MVP in SC mode , especially for View and Presenter in this mode There is a deep understanding of the way of interaction between us and now we are going to do an example demonstration. We use the Employee query scenario to build this simple application with ASP. NET Web Forms .

Let's start by defining the data type of the employee, employee to represent an employee, with 5 attributes:ID, name, gender, date of birth, and department.

public class Employee    {public        string Id {get; private set;}        public string Name {get; private set;}        public string Gender {get; private set;}        Public DateTime BirthDate {get; private set;}        public string Department {get; private set;}        Public Employee (string ID, string name, String gender,datetime birthDate, string department)        {this            . id = ID;            This. name = name;            This. Gender = Gender;            This. BirthDate = BirthDate;            This. Department = Department;        }

The Model, which contains the application state and State action behavior, is representedby a simple employeerepository type as follows. Data that represents all employee lists is maintained through a static field, and the getemployees method returns the list of employees for the specified department. If no filter department is specified or the specified department character is empty, the method returns all employee lists directly.

public class Employeerepository    {        private static ilist<employee> employees;        Static employeerepository ()        {            employees = new list<employee> ();            Employees. ADD (New Employee ("001", "Zhang San", "male", New DateTime (1981, 8, 24), "Sales Department"));            Employees. ADD (New Employee ("002", "John Doe", "female", New DateTime (1982, 7, 10), "personnel"));            Employees. ADD (New Employee ("003", "Harry", "Male", New DateTime (1981, 9, 21), "personnel"));        Public ienumerable<employee> getemployees (String department = "")        {            if (string. IsNullOrEmpty (department))            {                return employees;            }            Return employees. Where (E = = E.department = = Department). ToArray ();        }    }

Next we look at the ASViewinterface ofIemployeeviewthe definition. The interface defines thebindemployeesand thebinddepartmentstwo methods that are used to bind a list of departments based on theDropDownListand based on the employee listGridView. In addition,IemployeeviewThe interface also defines an eventdepartmentselected, the event is triggered when the user selects the filter department and the standalone query button. departmentselectedThe event argument type is a customdepartmentselectedeventages, the PropertiesDepartmentrepresents the department selected by the user.

Public interface Iemployeeview    {        void bindemployees (ienumerable<employee> employees);        void Binddepartments (ienumerable<string> departments);        Event eventhandler<departmentselectedeventargs> departmentselected;    } public class Departmentselectedeventargs:eventargs    {public        string Department {get; private set;}        Public Departmentselectedeventargs (String department)        {this            . Department = Department;        }    }

Presenter , as the core of the MVP Triangle, is represented by employeepresenter . It contains two attributes, one Viewthat represents the read-only property of the iemployeeview interface , and the other represents the read-only property of the Model employeerepository the Repositoryof the object, All two properties are initialized in the constructor.

public class Employeepresenter {public Iemployeeview View {get; private set;}        Public employeerepository Repository {get; private set;} Public Employeepresenter (Iemployeeview view) {this.            view = view; This.            Repository = new Employeerepository (); This.        view.departmentselected + = ondepartmentselected; public void Initialize () {ienumerable<employee>-employees = this.            Repository.getemployees (); This.            View.bindemployees (employees);            String[] departments = new string[] {"", "Sales", "purchasing department", "personnel", "IT department"}; This.        View.binddepartments (departments); } protected void Ondepartmentselected (object sender, Departmentselectedeventargs args) {string department = args.            Department; var employees = this.            Repository.getemployees (department); This.        View.bindemployees (employees); }    }

We register the departmentselected event of View in the constructor as the ondepartmentselected of the event handler method by calling the Repository (i.e. Model ) has a list of employees under the user Selection department and is bound to the View on.

Let's take a look at the Web page of view .

web   page, which he implemented defined in  iemployeeview   interface methods and events. 

Public partial class Default:System.Web.UI.Page, Iemployeeview {public employeepresenter Presenter {get; pri Vate set;        } public event eventhandler<departmentselectedeventargs> departmentselected; Public Default () {this.        Presenter = new Employeepresenter (this); } protected void Page_Load (object sender, EventArgs e) {if (!this. IsPostBack) {this.            Presenter.initialize (); }} protected void Buttonsearch_click (object sender, EventArgs e) {string department = th Is.            Dropdownlistdepartments.selectedvalue;            Departmentselectedeventargs EventArgs = new Departmentselectedeventargs (department);            if (null! = departmentselected) {departmentselected (this, EventArgs); }} public void Bindemployees (ienumerable<employee> employees) {this. Gridviewemployees.dataSource = employees; This.        Gridviewemployees.databind (); } public void Binddepartments (ienumerable<string> departments) {this.            Dropdownlistdepartments.datasource = departments; This.        Dropdownlistdepartments.databind (); }    }








Learn the NET MVC framework Secret Note-Example demo: Application of SC Mode

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.