Browse DevX
Sign up for e-mail newsletters from DevX


Design Patterns for ASP.NET Developers, Part 2: Custom Controller Patterns : Page 2

If any part of your ASP.NET applications display pages based on user interaction, browser type, security permissions, or other factors, you'll find these Controller patterns useful.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Implementing the Controller Patterns in ASP.NET
You can implement both the Page Controller and the Front Controller patterns in ASP.NET. In fact, ASP.NET makes it easy to combine them if required, as shown in Figure 3.

Figure 3. Combining Front Controller and Page Controller: The diagram shows how the Page Controller and Front Controller patterns can work in combination.
In Figure 3, the Front Controller specifies which of three Web pages will load depending on some feature of the request. Each Web page implements the MVP pattern, though one of them also uses the Page Controller pattern to determine which partial View to display within its page. This is, in fact, the overall structure of the downloadable sample application.

All three patterns implemented in this article, the Use Case Controller, Page Controller, and Front Controller, make use of the same set of three partial Views implemented as user controls. Therefore, before looking at the pattern implementations in detail, the next section examines these three user controls.

Implementing the Three Partial View User Controls
To illustrate the various controller patterns, the example application uses three very simple user controls that implement Views for several of the pages. Figure 4 shows the page from the Use Case Controller pattern example that uses the CustomerList user control (CustomerList.ascx). Note that the page heading, buttons, and actual URL are part of the hosting page, and not part of the user control.

Figure 4. The Customer List User Control: In this figure, the Customer List user control doesn't include the page heading, the buttons, or the "Actual URL" label.
The View itself (the ASPX page) contains only a GridView control and a Label control (where any errors messages will appear). The code to populate the GridView is in the Page_Load event handler. It uses the CustomerModel class to get a DataSet containing the list of customers and binds the DataSet to the GridView to show the results:

public partial class CustomerList : System.Web.UI.UserControl { protected void Page_Load(object sender, EventArgs e) { try { // get DataSet of customers from CustomerModel class // and bind to GridView control in the page CustomerModel customerList = new CustomerModel(); GridView1.DataSource = customerList.GetCustomerList(); GridView1.DataBind(); } catch (Exception ex) { Label1.Text += "USER CONTROL ERROR: " + ex.Message; } Label1.Text += "<p />"; } }

Figure 5. The Customer Details User Control: Here's how the control looks in the example application.
The second user control, named "CustomerDetails" (CustomerDetails.ascx), displays details of a specific customer (see Figure 5). To simplify the example, this is hard-coded to show the details of the customer with ID value "ALFKI", but you could of course easily modify the page to allow users to enter a customer ID in the same way as in the Default.aspx page you saw earlier.

The code in the Page_Load event handler of this user control uses the GetCustomerDetails method of the CustomerModel class to get a DataRow containing details of the specified customer, converts this to an Object array, and then iterates through the array displaying the values:

public partial class CustomerDetails : System.Web.UI.UserControl { protected void Page_Load(object sender, EventArgs e) { try { // use CustomerModel to get DataRow // for specified customer CustomerModel customers = new CustomerModel(); DataRow[] details = customers.GetCustomerDetails("ALFKI"); // convert row values into an array Object[] values = details[0].ItemArray; Label1.Text += "Customer Details from " + "CustomerModel class: <br />"; // iterate through the array displaying the values foreach (Object item in values) { Label1.Text += item.ToString() + ", "; } } catch (Exception ex) { Label1.Text += "USER CONTROL ERROR: " + ex.Message; }

Figure 6. The City List User Control: The City List user control displays a list of cities where customers reside, shown here in the sample application.
Label1.Text += "<p />"; } }
The third user control, named "CityList" (CityList.ascx), displays a list of the cities where customers reside, together with a count of the number of customers in each city (see Figure 6).

The code in the Page_Load event handler of the "CityList" user control uses the CustomerModel.GetCityList method to get a System.Collections.SortedList instance containing the list of cities and the count of customers in each one. It then iterates through the list displaying the key (the city name) and the value (the number of customers) for each item:

public partial class CityList : System.Web.UI.UserControl { protected void Page_Load(object sender, EventArgs e) { try { // use CustomerModel to get SortedList of cities CustomerModel customers = new CustomerModel(); SortedList cities = customers.GetCityList(); // iterate through the SortedList displaying // the values Label1.Text += "List of cities from CustomerModel class: <br />"; foreach (String key in cities.Keys) { Label1.Text += key + " (" + cities[key].ToString() + ")<br />"; } } catch (Exception ex) { Label1.Text += "USER CONTROL ERROR: " + ex.Message; } Label1.Text += "<p />"; } }

Thanks for your registration, follow us on our social networks to keep up-to-date