MSDN Magazine - October 2008 - (Page 38) Another point to mention is that networking in Silverlight can only happen asynchronously. To make a synchronous call you have to resort to a trick that essentially consists of invoking the browser interoperability layer and reaching the browser’s implementation of XMLHttpRequest, whenever this is possible (for details on this, see go.microsoft.com/fwlink/?LinkId=124048). The bottom line is that WPF and Silverlight codebehind classes leverage significantly different class libraries. This fact, much more than any XAML differences, hinders reusability. Let’s tackle this problem next. When you write code to be shared by Silverlight and Windows runtimes, you should know very well what each platform supports. Ideally, you’ll have a list of requested tasks that require special handling on each platform. Next, you isolate these pieces of code in an object and extract an interface out of them. As a result, you’ll have a fully reusable block of code that calls into separate components for critical functionalities. Porting such an application to Silverlight (or WPF) would then be as easy as replacing critical components with others that are platform specific. Because all these components still expose a common interface, their implementation is transparent to the main block of code. The pattern behind this approach is the “Strategy” pattern. For a formal definition of this, you should see go.microsoft.com/ fwlink/?LinkId=124047. In a nutshell, the Strategy pattern is helpful whenever you need to dynamically change the algorithm used to perform a certain task in an application. Once you have identified a region of your code that has the potential to vary based on runtime conditions, you define an interface that specifies the abstract behavior of your code. Next, you create one or more strategy classes that implement the interface and thereby represent different ways in which the abstract behavior can be accomplished. By then changing the strategy class, you are simply changing your approach for solving the algorithm defined by the Get a Strategy for Critical Code Figure 3 Diagram of the Application’s Behavior is significantly smaller, but it still supports fundamental capabilities such as collections, reflection, regular expressions, string manipulation, threading, and timers. You also have tools to call into a variety of services such as XML Web services, WCF services, and ADO.NET Data Services. In addition, you have a rich networking support to communicate over HTTP—with Plain Old XML (POX) and Representational State Transfer (REST) services—and, in general, reach any public HTTP endpoint. Networking support also includes (cross-domain) sockets and duplex communication. Finally, the Silverlight BCL provides good support for working with XML data, including ad hoc versions of the XmlReader and XmlWriter classes. These classes are quite similar to the analogous classes in the desktop version of the .NET Framework. Building on these core capabilities, in Silverlight 2, you find full support for LINQ to Objects, LINQ to XML, and expression trees. Starting with Beta 2, Microsoft also added a brand new LINQ to JavaScript Object Notation (JSON) provider to run LINQ queries directly on JSON data. Figure 4 The Presenter Class namespace Samples { class SymbolFinderPresenter { // Internal reference to the view to update private ISymbolFinderView _view; public SymbolFinderPresenter(ISymbolFinderView view) { this._view = view; } public void Initialize() { } // Triggered by the user’s clicking on button Find public void FindSymbol() { // Clear the view before operations start ClearView(); // Get the symbol to retrieve string symbol = this._view.SymbolName; if (String.IsNullOrEmpty(symbol)) { _view.QuickInfoErrorMessage = “Symbol not found.”; return; } QuoteService service = new QuoteService(); StockInfo stock = service.GetQuote(symbol); // Update the view UpdateView(stock); private void ClearView() { _view.SymbolDisplayName = String.Empty; _view.SymbolValue = String.Empty; _view.SymbolChange = String.Empty; _view.ServiceProviderName = String.Empty; } } } } private void UpdateView(StockInfo stock) { // Update the view _view.QuickInfoErrorMessage = String.Empty; _view.SymbolDisplayName = stock.Symbol; _view.SymbolValue = stock.Quote; _view.SymbolChange = stock.Change; _view.ServiceProviderName = stock.ProviderName; } 38 msdn magazine Cutting Edge http://go.microsoft.com/fwlink/?LinkId=124048 http://go.microsoft.com/fwlink/?LinkId=124047 http://go.microsoft.com/fwlink/?LinkId=124047
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.