Take notes on learning ASP. net mvc Framework-Example demonstration: Application in SC Mode, asp. netmvc

Source: Internet
Author: User

Take notes on learning ASP. net mvc Framework-Example demonstration: Application in SC Mode, asp. netmvc
Example: Application in SC Mode

In order to have a deep understanding of MVP in SC Mode, especially the interaction between View and Presenter in this mode, we will present an example. We use the employee query scenario and ASP. NET Web Forms to establish this simple application.

Let's first define the data type of an Employee. Employee represents an Employee and has five attributes: ID, name, gender, birth date, 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;        }

As a Model that contains application status and state operation behavior, it is embodied by a simple EmployeeRepository type. The data in the employee list is maintained by a static field, and the GetEmployees method returns the employee list of the specified department. If no filtering department is specified or the specified department character is blank, this method returns the list of all employees directly.

Public class EmployeeRepository {private static IList <Employee> employees; static EmployeeRepository () {employees = new List <Employee> (); employees. add (new Employee ("001", "Michael", "male", new DateTime (1981, 8, 24), "sales"); employees. add (new Employee ("002", "Li Si", "female", new DateTime (1982, 7, 10), "Personnel Department"); employees. add (new Employee ("003", "Wang Wu", "male", new DateTime (1981, 9, 21), "Personnel Department "));} public IEnumerable <Employee> GetEmployees (string department = "") {if (string. isNullOrEmpty (department) {return employees;} return employees. where (e => e. department = department ). toArray ();}}

Next, let's take a look at the definition of IEmployeeView as the View interface. This interface defines BindEmployees and bindparameters, which are used to bind DropDownList Based on department list and GridView Based on employee list respectively. In addition, the IEmployeeView interface defines a DepartmentSelected event, which is triggered when a single-host query button is selected after the filtering department is selected. The parameter type of the DepartmentSelected event is custom DepartmentSelectedEventAges. The property Department indicates 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;        }    }

The Presenter, as the core of the MVP triangle relationship, is represented by EmployeePresenter. It contains two attributes, one representing the View's read-only attribute IEmployeeView interface, and the other representing the Repository of the Model's read-only attribute EmployeeRepository object. Both attributes are initialized in the constructor.

Public class EmployeePresenter {public IEmployeeView View {get; private set;} public EmployeeRepository Repository {get; private set;} public EmployeePresenter (IEmployeeView view 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 [] orders = new string [] {"", "Sales Department", "purchasing department", "Personnel Department", "IT department"}; this. view. bindadministrative ments (Administrative ments);} protected void OnDepartmentSelected (object sender, DepartmentSelectedEventArgs args) {string department = args. department; var employees = this. repository. getEmployees (department); this. view. bindEmployees (employees );}}

The DepartmentSelected event of the View is registered in the constructor. The OnDepartmentSelected method of the event processor obtains the list of employees under the selected department by calling Repository (Model) and binds them to the View.

Next let's take a look at the View Web page.

<Html xmlns = "http://www.w3.org/1999/xhtml"> 

The following is the background Code definition on the web page, which implements the methods and events defined in the IEmployeeView interface.

public partial class Default : System.Web.UI.Page, IEmployeeView    {        public EmployeePresenter Presenter { get; private 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 = this.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();        }    }








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.