AJAX In Action [91]
Web service
Domain
facade
model
Web client
Web server
Figure 5.5 Comparison of a system in which all objects are fully
exposed as Internet services to an Ajax client and one is using a
Façade to expose only a few carefully chosen pieces of
functionality. By reducing the number of publicly published
methods, we can refactor our domain model without fear of
breaking client code over which we have no control.
By bundling calls across the network into larger documents, service-oriented architectures are making better use of available network resources. Bandwidth is typically less of a problem than latency. They are also causing problems for themselves by standardizing on a bulky XML data format over a verbose transmission protocol (our familiar and well-loved HTTP), but that’s a story for another day. If we look at the options available with Ajax, we can see that we are provided with good native support for HTTP and XML technologies in the browser, and so a document-centric approach to our distributed domain models makes sense. A conventional document, such as this book, is composed of paragraphs, headings, tables, and figures. Likewise, a document in a call to a service may contain a variety of elements, such as queries, updates, and notifications. The Command pattern, discussed in chapter 3, can provide a good foundation for structuring our Licensed to jonathan zheng 174 CHAPTER 5 The role of the server documents as a series of undoable actions to be passed between client and server. We’ll look at an implementation of this later in the chapter. This concludes our discussion of the server-side architectures of the day. None provides a perfect fit for Ajax yet, which is not surprising given that they were designed to serve a considerably different kind of web application. A lot of good work is underway to build Ajax into existing frameworks and the next year or so should prove interesting. Nonetheless, many web developers will be faced with the task of making Ajax work with these legacy systems, and this overview of the strengths and weaknesses for each ought to provide a starting point. Let’s assume for the moment that we have decided upon one architecture or another and begun the work of developing an Ajax application. We have already discussed the architecture of the client application itself in detail in chapter 4, and we provided examples of retrieving XML data from the server in chapter 2. XML is popular but not the only way of exchanging data between client and server. In the following section, we review the full spectrum of options for communicating between client and server. 5.4 The details: exchanging data We’ve looked at the big architectural patterns that describe how our web application might behave and shown that there are many options. We’ve stressed the importance of communication between the client and the server’s domain models, and we might naively assume that once we’ve settled on a framework, our design choices are made for us. In this and the following section, we’ll see that this is far from true. If we focus on a single exchange of data, we have many options. We’ll catalog the options here, with the aim of developing a pattern language for Ajax data exchange. With this in hand, we can make more informed decisions about what techniques to use in particular circumstances. Exchanging pure data has no real analog in the classical web application, and so the pattern language is less well developed in this area. I’ll attempt to fill that void by defining a few phrases of my own. As a first cut, I suggest that we break user interactions into four categories: client-only, content-centric, script-centric, and data-centric. Client-only interactions are simple, so we’ll deal with them quickly in the next section, and then introduce an example that can see us through the other three. Licensed to jonathan zheng The details: exchanging data 175 5.4.1 Client-only interactions A client-only interaction is one in which a user interaction is processed by