New Features in ASP.NET AJAX 4, part 4
The AdoNetServiceProxy Class
The AdoNetServiceProxy class enables read-write interaction with ADO.NET Data Services from JavaScript. The class enables access from JavaScript to a broad range of features of ADO.NET Data Services. It provides direct programmatic access to the basic REST operations provided by ADO.NET Data Services (insert, query, update, and remove), as well as many advanced features such as verb tunneling (custom HTTP methods) and optimistic concurrency.
The AdoNetServiceProxy class is used by the AdoNetDataContext class in read-write scenarios, as explained in the next section.
The DataContext and AdoNetDataContext Classes
For read-write scenarios that use Web services or data services, ASP.NET AJAX 4 provides a DataContext class that provides full support for change tracking in the browser. This enables complete end-to-end AJAX-based data scenarios.
Typically, data is fetched from the server through JSON services such as a WCF AJAX-enabled service or ADO.NET data services. The data is displayed to the user through dynamic data-driven UI, using the DataView control. Declarative live-binding markup in the template provides users with an edit UI, which enables them to modify the data. The ASP.NET AJAX DataContext class tracks all changes to the data automatically. All changes can then be sent at once to the server by calling the SaveChanges method of the DataContext class. A single DataContext instance can manage change tracking for data returned by different operations on the server, even if the operations return different types of objects.
The following example shows how to use the DataContext class.
<script type="text/javascript">
var dataContext = new Sys.Data.DataContext();
dataContext.set_serviceUri("../Services/imagesService.svc");
dataContext.set_saveOperation("SaveImages");
dataContext.initialize();
</script>
<button onclick="dataContext.saveChanges()" class="right">Save Changes</button>
<ul sys:attach="dataview" class="sys-template"
dataview:dataprovider="{{ dataContext }}"
dataview:query="GetImages"
>
<li>
<input type="text" value="{binding Name}"/><br/>
<input type="text" value="{binding Description}"/>
</li>
</ul>
If you are using an ADO.NET data service, you should use the AdoNetDataContext class instead of the more general-purpose DataContext class. (AdoNetDataContext derives from DataContext.) The AdoNetDataContext class provides additional support for features that are specific to ADO.NET, such as identity management, links and associations between entity sets that are returned in different fetch operations, hierarchical data, and optimistic concurrency.
Refactoring the Microsoft AJAX Framework Libraries
ASP.NET AJAX 4 also introduces the ability to use only parts of the ASP.NET AJAX framework for efficiency, as well as the ability to use the ScriptManager control without using the ASP.NET AJAX framework at all. The ScriptManager control provides services such as centralized management of references, support for debug and release modes, support for localization, and script combining. These services can be useful to all client-script developers, even those who use JavaScript libraries other than Microsoft AJAX, such as jQuery. Until now, the ScriptManager control included the Microsoft ASP.NET AJAX library automatically, without providing a simple way to opt out of it. As a result, when developers used the ScriptManager control in their pages, the effect was to include the whole Microsoft ASP.NET AJAX library in their applications.
In ASP.NET 4, the default behavior for the ScriptManager control is to include the complete ASP.NET AJAX library. However, the ScriptManager control supports a new MicrosoftAjaxMode property that lets you choose a subset of the framework by using only portions of the library in the form of split script files. The MicrosoftAjaxMode property can have one of the following values:
· Enabled — All Microsoft AJAX scripts are included (legacy behavior). This is the default value of the property.
· Explicit — Each split script file must be added explicitly; it is up to you to make sure that you include all scripts that have dependencies on one another.
· Disabled — All Microsoft ASP.NET AJAX script features are disabled and the ScriptManager control does not reference any scripts automatically.
When you use Explicit mode, the scripts that are available are as follows:
· MicrosoftAjaxCore.js
· MicrosoftAjaxComponentModel.js
· MicrosoftAjaxSerialization.js
· MicrosoftAjaxGlobalization.js
· MicrosoftAjaxHistory.js
· MicrosoftAjaxNetwork.js
· MicrosoftAjaxWebServices.js
· MicrosoftAjaxApplicationServices.js
· MicrosoftAjaxTemplates.js (New for ASP.NET AJAX 4)
· MicrosoftAjaxAdoNet.js (New for ASP.NET AJAX 4)
The following chart shows the dependencies between split scripts. The violet boxes that are labeled Templates (AdoNetDataContext) and Templates (DataContext) indicate that only these classes in MicrosoftAjaxTemplates.js have the indicated dependency. Thus, MicrosoftAjaxTemplates.js does not require MicrosoftAjaxWebServices.js unless you use the DataContext class, and it does not require MicrosoftAjaxAdoNet.js unless you use the AdoNetDataContext class.
The WebForms box for the MicrosoftAjaxWebForms.js script is green to indicate that it is included automatically when you set EnablePartialRendering to true (which is the default setting for the ScriptManager control).
As an example, to use the ASP.NET AJAX browser history feature with no partial rendering and with MicrosoftAjaxMode set to Explicit, the ScriptManager control must be configured as in the following example:
<asp:ScriptManager ID="ScriptManager1"
EnablePartialRendering="false"
MicrosoftAjaxMode="Explicit"
EnableHistory="true"
runat="server">
<CompositeScript>
<Scripts>
<asp:ScriptReference Name="MicrosoftAjaxCore.js" />
<asp:ScriptReference Name="MicrosoftAjaxComponentModel.js" />
<asp:ScriptReference Name="MicrosoftAjaxSerialization.js" />
<asp:ScriptReference Name="MicrosoftAjaxHistory.js" />
</Scripts>
</CompositeScript>
</asp:ScriptManager>
In the example, EnablePartialRendering has been set to false so that MicrosoftAjaxWebForms.js is not included automatically, and EnableHistory has been set to true so that the ScriptManager control can use the AJAX browser history feature. Order is important when you include split scripts — if a script has dependencies, the ScriptReference elements for those dependencies must be listed before the ScriptReference element for the script itself.
Note that split script files should be used only by developers who are concerned about optimizing for very high performance. When split script files are used, they should be used together with script combining to minimize the numbers of requests that are required in order to download the scripts.
The DOM Ready Event
In Beta 2, changes in ASP.NET make it easier to use Microsoft AJAX when you create an ASP.NET MVC application or pure client Web application. In versions earlier than Beta 2, if you wanted to use the pageLoad method outside a Web Forms application, for performance reasons you needed to call sys.application.initialize at the bottom of the page. Otherwise, the pageLoad method would not be called until the window.onload event was raised.
In Beta 2, the pageLoad method is called immediately after the document DOM content is finished loading and before the window.onload event. You no longer need to call sys.application.initialize to cause the pageLoad method to be called before window.onload occurs.
Using JSONP to Retrieve Cross-Domain Data
Ordinarily, when you make an AJAX request from a page, you are limited to making a request to the domain that hosts the page. In other words, you are not allowed to make cross-domain requests.
In Beta 2, ASP.NET AJAX includes support for JSONP. Using JSONP, you can interact with services that are located in another domain.
No special configuration is required in order to take advantage of JSONP when you create a service reference. You simply configure the service path of the service to include a reference to the target domain, as shown in the following example:
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="http://AnotherDomain/Books.svc" />
</Services>
</asp:ScriptManager>
In order to use JSONP to make cross-domain requests, you must interact with a JSONP-enabled service. For example, if you want to request data from a WCF service, the service must be enabled to support JSONP. For more information about how to enable JSONP for a WCF service, see the article JSON with Padding (AJAX) on the MSDN Web site.
Posted in: | Tags: asp.net asp.net 4.0 asp.net ajax ajax adonetdatacontext datacontext adonetserviceproxy ajax framework json jquery microsoftajaxglobalization microsoftajaxhistory microsoftajaxapplicationservices microsoftajaxadonet