Take notes on learning ASP. net mvc Framework-Example demonstration: Application of SC Mode
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
Employees; static EmployeeRepository () {employees = new List
(); 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
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
employees); void BindDepartments(IEnumerable
departments); event EventHandler
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
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.
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
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
employees) { this.GridViewEmployees.DataSource = employees; this.GridViewEmployees.DataBind(); } public void BindDepartments(IEnumerable
departments) { this.DropDownListDepartments.DataSource = departments; this.DropDownListDepartments.DataBind(); } }