Web Client Software Factory series (3): View-Presenter mode

Source: Internet
Author: User
Overview

Add an ASP. the NET Site is separated into multiple independent modules. The biggest problem is that most of the business logic associated with the page resides in the source code file of the page, we almost cannot divide source code files into multiple independent assemblies. To create an independent function module that is associated with pages on the site, all page logic, event processing logic, and navigation logic must be extracted from the page in some way and stored in an independent set of programs.

The default solution in the Web Composite Application Block is to use the View-Presenter mode to divide the page logic into different classes (proxies) used to respond to any event forwarded by the View (Web page ). The representations class is fully implemented in the business module, so that the application logic is out of the website. interfaces are defined in the business module that defines the methods implemented by the view. In this way, the web page can forward all events to the dashboard at the end, without any actual responsibilities specific to the application. This also makes it easier to test the design dashboard without actually involving the front-end web page.

Add View

In the previous example, we have created relevant business modules and services. Here we add a view:

Here we create a view ProductDetail that displays product details. After the product is created, we can see it in the Resource Manager:

Added the new page ProductDetail. aspx and the corresponding source code file to the/Products directory. For the Products business module project, this solution adds the ProductDetailPresenter class and the corresponding IProductDetail (View) interface, which is already created by ProductDetail. the source code class implementation in the aspx file, and the source code class file also contains the attribute declaration of the ProductDetailPresenter class corresponding to the event to which the page should be forwarded:

public partial class Products_ProductDetail : System.Web.UI.Page, IProductDetail{private ProductDetailPresenter _presenter;protected void Page_Load(object sender, EventArgs e){if (!this.IsPostBack){this._presenter.OnViewInitialized();}this._presenter.OnViewLoaded();}[CreateNew]public ProductDetailPresenter Presenter{set{this._presenter = value;this._presenter.View = this;}}}
To display the Product name and brand, open the IProductDetail. cs file and add the following two attributes:
public interface IProductDetail{public string Name;public string Brand;}

Then we implement these two attributes in the page:

public partial class Products_ProductDetail : System.Web.UI.Page, IProductDetail{private ProductDetailPresenter _presenter;protected void Page_Load(object sender, EventArgs e){if (!this.IsPostBack){this._presenter.OnViewInitialized();}this._presenter.OnViewLoaded();}[CreateNew]public ProductDetailPresenter Presenter{set{this._presenter = value;this._presenter.View = this;}}public string Name{set{this.lbl_Name.Text = value;}}public string Brand{set{this.lbl_Brand.Text = value;}}}
Implement Presenter

From the code above, we can see that the source code file on the page is very clean, and there is nothing related to the business logic. As to where the data comes from, how to display it will be handed over to the Presenter. The next step is to implement Presenter. Open ProductDetailPresenter. cs file, which can implement any event on the Page. The difference between the OnViewLoaded and OnViewInitialized methods lies in the IsPostBack judgment corresponding to the Page. The View attribute is defined in the generic Presenter. The code after writing is as follows:

public class ProductDetailPresenter : Presenter<IProductDetail>{private ProductsController _controller;public ProductDetailPresenter([CreateNew] ProductsController controller){_controller = controller;}public override void OnViewLoaded(){Product product = _controller.GetProductById("1");View.Name = product.Name;View.Brand = product.Brand;}public override void OnViewInitialized(){}}

After viewing in the browser, you can see the following page:

The problem is that the page is displayed, but the link of ProcuctDetail is not displayed in the tree control navigation on the left. This is the module site ing.

Module site ing

The solution master page contains a Tree View control bound to SiteMapDataSource to display the navigation pages on the site, and the SiteMapPath control. The SiteMapDataSource is not bound to the standard Web. sitemap file, but to the custom SiteMapProvider that independently collects navigation information from each module. When the application starts, the Provider explicitly queries the site map information of each module. By default, the SiteMapProvider is registered as the default Provider and is used by SiteMapDataSource for all navigation controls.

To fill in ModuleSiteMapProvider with the site map information of a specific module, override the RegisterSiteMapInformation method in the class derived from ModuleInitializer. We will override this method when creating a business module, and will set the Default value under the business module. the aspx page is inserted into the SiteMapNodes set, but we need to add any other pages to the site and add them to the set.

protected virtual void RegisterSiteMapInformation(ISiteMapBuilderService siteMapBuilderService){SiteMapNodeInfo moduleNode = new SiteMapNodeInfo("Products", "~/Products/Default.aspx", "Products");siteMapBuilderService.AddNode(moduleNode);SiteMapNodeInfo productDetailNode = new SiteMapNodeInfo("ProductDetail","~/Products/ProductDetail.aspx","ProductDetail");siteMapBuilderService.AddNode(productDetailNode,moduleNode);}
SiteMapNodeInfo has three parameters: key value, page address, and display title. However, hard encoding is added here, which can be modified as needed so that it can read site ing information from the configuration file.
Now run the program. You can see that the ProductDetail page has been added to the Products business module and the navigation bar has been added to the page.
 
Conclusion

Using the Web Client Software Factory, a simple Composite Web Application Block and View-Presenter mode are used here. In the next article, we will illustrate how to use the View-Presenter mode to bind data and use the ObjectContainerDataSource control.
Download the sample code:/Files/Terrylee/WebClientDemo1.rar

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.