Lives: 30

New Grid Feature: WebService Running Mode

Posted Wed Nov 14, 2007 @ 8:30 AM


Version 2007.2 of ComponentArt Grid includes, among other goodies, an exciting new feature: WebService running mode. We think it should be very useful for a lot of people so, in this post, I want to introduce you to this feature and explain how it works.

What is it?

Those who have used Grid will be familiar with the notion of a “running mode”. Until this point, Grid could be used in three different modes: Server, Callback and Client. Setting the RunningMode property to one of these determines whether Grid performs its data-related actions (paging, sorting, filtering, etc) via postbacks, callbacks, or entirely on the client.

The most efficient way of interfacing with large data sets was always the callback mode, but it had one downside: callbacks were made to the same ASPX page which houses the control, so each callback incurred the overhead of the ASP.NET page life cycle and auxiliary init code.

The new WebService running mode allows Grid to perform data operations via calls to an ASP.NET AJAX web service. We feel that this system provides maximal efficiency and design elegance by allowing us to completely separate our data access code from the ASP.NET page which houses the layout.

How do I use it?

To use web service running mode, start by setting Grid’s RunningMode property to WebService.

Then, set the WebService property to the name of the ASP.NET AJAX web service to use. You will also need to set the names of that service’s WebMethods to use for fetching items, or updating and inserting them. For this, use the WebService*Method properties (eg. WebServiceSelectMethod).

The web service should be registered with the ScriptManager in the usual way, and the specified methods should have standard signatures:

GridWebServiceSelectResponse SelectMethod(GridWebServiceSelectRequest req)
bool UpdateMethod (GridWebServiceUpdateRequest req)
bool InsertMethod(GridWebServiceInsertRequest req)

Selection

Most of the usual interaction with a Grid will be done through the select method, whose logic will handle all paging, sorting and filtering. Whenever a new set of records is required on the client this method will be invoked with all the necessary information to fetch the appropriate records. The GridWebServiceSelectRequest will contain the required page index, page size, sort order and any conditional filters. The response should contain all the necessary records (objects with appropriately named properties), and information about the total number of records, for purposes of paging.

Here’s an example of a simple select method which handles paging and sorting:

    [WebMethod]
public GridWebServiceSelectResponse GetRecords(GridWebServiceSelectRequest request)
{
DataSet oDS = FetchAllData(); // implemented elsewhere, returns all records
DataView oView = oDS.Tables[0].DefaultView;

if(request.SortField != "") oView.Sort = request.SortField + " " + request.SortOrder;

List list = new List();

int pageSize = request.PageSize;
int startRec = request.CurrentPageIndex * pageSize;

for (int i = startRec; i < Math.Min(startRec + pageSize, oView.Count); i++)
{
Message msg = new Message();

msg.Subject = (string)oView[i]["Subject"];
msg.LastPostDate = (DateTime)oView[i]["LastPostDate"];
msg.TotalViews = (int)oView[i]["TotalViews"];
msg.StartedBy = (string)oView[i]["StartedBy"];

list.Add(msg);
}

GridWebServiceSelectResponse response = new GridWebServiceSelectResponse();

response.Items = list;
response.RecordCount = oView.Count;

return response;
}

Inserting and Updating

Updating records and inserting new ones is done in very much the same way as selection. However, since these actions only affect one record, the request is a lot simpler and basically contains only the data for the item in question. The relevant classes are GridWebServiceInsertRequest and GridWebServiceUpdateRequest.

We hope this mechanism will allow for totally UI-independent data handling that communicates with the client-side Grid in the most efficient possible way. Because Grid is able to redraw itself on the client, there is no need to ever go back to the original ASP.NET page after it has launched the Grid. Developing this approach should be the way forward in taking client-centric web apps to the next level.

Let us know what you think!

Posted to Milos Glisic by milos

Posted on Wed Nov 14, 2007 @ 8:30 AM

Filed under: , , ,

Comments

Bert
Posted on Wed Nov 14, 2007 @ 8:30 AM
Coolest idea, ever.
Dragos
Posted on Wed Nov 14, 2007 @ 8:30 AM
there is any way you can bind directly to the DataView instead of custom collection ?
milos
Posted on Wed Nov 14, 2007 @ 8:30 AM
Dragos: Since we rely on ASP.NET AJAX to serialize server-side objects into JSON, we can't simply return a DataView or DataSet. These fail to serialize automatically due to their complexity (circular references). For this reason, we need to have control over the objects that are getting returned, so we can ensure successful and efficient JSON serialization. Of course, we'll want to make common scenarios as easy as possible, so we will likely be expanding this functionality with every subsequent release. For instance, we may include a method on the GridWebServiceSelectResponse class which will allow it to load items from a DataView with one line of code. Thanks for your feedback on this.
Gavin
Posted on Wed Nov 14, 2007 @ 8:30 AM
Milos This is really good functionality. Something I have been looking for. I can't see any references or full examples to this in the new 2007.2 documentation/tutorials. Link to tutorial in help file doesn't work. Can you point me to a full plain english documention for this please in the 2007.2 release files ?
Milos
Posted on Wed Nov 14, 2007 @ 8:30 AM
Gavin: You'll find the new properties and classes in the API docs, and examples under Technology Showcase. We'll be adding some tutorial-style documents in there ASAP.
Arch
Posted on Wed Nov 14, 2007 @ 8:30 AM
Milos - Love this feature. It's precisely the model I would like to use. Can you post an example of retrieving a DataTable or similar and converting it into something that can be returned using a web service. I am sure this is a common scenario. I've tried several data structures implementing IList but none seem to work. My problem is that I cannot use a hard-coded structure since the columns returned are dynamic.
Alletha
Posted on Wed Nov 14, 2007 @ 8:30 AM
Milos Is is possible to catch exceptions thrown by the webservice select method before they get down to the client? Getting an undefined error on the client is not very helpful. While I understand that the webservice essentially shouldn't fail, nothing in this world is ever perfect and I would like to show something informative about what to do if this occurs.
keerthi
Posted on Wed Nov 14, 2007 @ 8:30 AM
Nice
Anonymous comments are not allowed. Click here to log in or create an account.

Copyright © 2010 ComponentArt, Inc. All rights reserved.