Web Client Software Factory series (4): Data Binding and ObjectContainerDataSource controls

Source: Internet
Author: User
Overview

In the Web Client Software Factory series (3): View-Presenter mode, the provisioner contains the logic for responding to user events and the status of some views, the Web Client Software Factory contains a data source control named ObjectContainerDataSource, which provides a bridge between the View-Presenter mode and data binding, objectContainerDataSource can be simply understood as a container used to package row objects. The processing process is as follows:

The following shows how to use the View-Presenter mode to bind data to the ObjectContainerDataSource control with a complete strength. As mentioned in the previous article, we first add a NewProduct view under the Product business module.

Add the ObjectContainerDataSource control to the view.

Add the ObjectContainerDataSource control in the toolbox, which is located in Microsoft. Practices. Web. UI. WebControls. dll. Drag the ObjectContainerDataSource control to the NewProduct. aspx page.

The next step is to configure ObjectContainerDataSource. In fact, you must configure the DataObjectTypeName attribute, that is, the type of the object to be included in the ObjectContainerDataSource control. Select the Product entity class we wrote before. After configuration, the code in ASPX is as follows:

<asp:Content ID="content1" ContentPlaceHolderID="DefaultContent" Runat="Server">

Note the following When configuring the DataObjectTypeName attribute:

1. If the Insert, Update, and Delete operations are performed, the corresponding type of the configured DataObjectTypeName parameter must have a constructor without parameters;

2. if the Update and Delete operations are performed, the data type corresponding to the configured DataObjectTypeName parameter must have an attribute that uniquely represents an instance of this type, which is actually a primary key corresponding to the database and also supports joint primary keys, this is not hard to understand. Think about SQL statements.

Add DetailsView and GridView to bind to ObjectContainerDataSource

Add the DetailsView and GridView controls on the NewProduct. aspx page, and specify their performanceid as objectcontainerperformance1, as shown in:

The code after setting is as follows:

<asp:Content ID="content" ContentPlaceHolderID="DefaultContent" Runat="Server">Insert, Update, and Delete data

To add, delete, and modify data, we need to modify the Service written in the Composite Web Application Block in the Web Client Software Factory series (2, in order to demonstrate that the data is not read from the database, it is saved in the Session.

IProductDataService interface:

public interface IProductDataService{Product GetProductById(string id);List<Product> Products { get;set;}void InsertProduct(Product product);void UpdateProduct(Product product);void DeleteProduct(Product product);}
Service implementation:
public class ProductDataService : IProductDataService{private List<Product> _products;public List<Product> Products{get{_products = HttpContext.Current.Session["products"] as List<Product>;if (_products == null){_products = new List<Product>();}return _products;}set{HttpContext.Current.Session["products"] = value;}}public void InsertProduct(Product product){product.Id = Guid.NewGuid().ToString();_products = HttpContext.Current.Session["products"] as List<Product>;if (_products == null){_products = new List<Product>();}_products.Add(product);HttpContext.Current.Session["products"] = _products;}public void UpdateProduct(Product product){Product result = FindProduct(Products, product);if (result != null){result.Name = product.Name;result.Brand = product.Brand;}}public void DeleteProduct(Product product){Product result = FindProduct(Products, product);if (result != null){Products.Remove(result);}}private static Product FindProduct(List<Product> products, Product product){return products.Find(delegate(Product match){return product.Id == match.Id;});}}

Open the view INewProduct and write the following code:

public interface INewProduct{List<Product> Products { set;}}
And implement the View Interface in NewProduct. aspx. cs.
public List<Product> Products{set{this.ObjectContainerDataSource1.DataSource = value;}}
What we need to do next is to implement the Controller, which has been discussed in the section registering and using services in the Composite Web Application Block in the Web Client Software Factory series (2). The Code is provided directly:
public class ProductsController{public ProductsController(){}private IProductDataService _productDataService;[ServiceDependency]public IProductDataService ProductDataService{set { _productDataService = value; }}public List<Product> Products{get{return _productDataService.Products;}set{_productDataService.Products = value;}}public void InsertProduct(Product product){_productDataService.InsertProduct(product);}public void UpdateProduct(Product product){_productDataService.UpdateProduct(product);}public void DeleteProduct(Product product){_productDataService.DeleteProduct(product);}}

Implement Our Presenter, which is detailed in the Web Client Software Factory series (3): views, representations, and controllers.
public class NewProductPresenter : Presenter<INewProduct>{private ProductsController _controller;public NewProductPresenter([CreateNew] ProductsController controller){_controller = controller;}public override void OnViewLoaded(){View.Products = _controller.Products;}public override void OnViewInitialized(){}public void OnProductInserted(Product product){_controller.InsertProduct(product);}public void OnProductUpdated(Product product){_controller.UpdateProduct(product);}public void OnProductDeleted(Product product){_controller.DeleteProduct(product);}}

Finally, add related events for the ObjectContainerDataSource control and hand over the specific operations to the Presenter:

protected void ObjectContainerDataSource1_Inserted(object sender, ObjectContainerDataSourceStatusEventArgs e){_presenter.OnProductInserted((Product)e.Instance);}protected void ObjectContainerDataSource1_Updated(object sender, ObjectContainerDataSourceStatusEventArgs e){_presenter.OnProductUpdated((Product)e.Instance);}protected void ObjectContainerDataSource1_Deleted(object sender, ObjectContainerDataSourceStatusEventArgs e){_presenter.OnProductDeleted((Product)e.Instance);}

It should be noted that objectcontainerperformancestatuseventargs has two important attributes: Instance and AffectedRows. The ObjectContainerDataSource control creates a powerful Instance of the type of the object it contains through reflection, and AffectedRows is the number of affected rows.

After running:

Paging and sorting

If you use the default paging and sorting functions of ObjectContainerDataSource, you need to set the following two attributes:

UsingServerPaging="True"UsingServerSorting="True" 

And write the Selecting event:

protected void CustomersDataSource_Selecting(object sender, ObjectContainerDataSourceSelectingEventArgs e){_presenter.OnSelecting(e.Arguments.StartRowIndex, e.Arguments.MaximumRows, e.Arguments.SortExpression);}

The rest goes to the Presenter to process the page :)

Conclusion

For more information about how to bind data to the ObjectContainerDataSource control in View-Presenter mode in Web Client Software Factory, see here.
Download the sample code:/Files/Terrylee/WebClientDemo2.rar

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.