MSDN Magazine - October 2008 - (Page 39) interface. In doing so, you decouple the behavior’s actual implementation (the strategy) from the code that is using it. In ASP.NET, for example, the Strategy pattern is used in the implementation of the provider model for membership, roles, user profiles, and so forth. The ASP.NET runtime knows that there’s a particular interface to deal with, say, membership and users. It also knows how to find and instantiate the concrete classes for these interface types. However, the runtime components depend only on the interface, making the details of the concrete class irrelevant to ASP.NET. Figure 5 QuoteService Checks for Internet Connection public StockInfo GetQuote(string symbol) { // Get the provider of the service IQuoteServiceProvider provider = ResolveProvider(); return provider.GetQuote(symbol); } private IQuoteServiceProvider ResolveProvider() { bool isOnline = IsConnectedToInternet(); if (isOnline) return new FinanceInfoProvider(); return new RandomProvider(); Going through an Example Let’s consider the sample application shown in Figures 1 and 2. In both Silverlight and Windows, the application presents a form where users can type a stock symbol to get a current quote. Figure 3 shows the diagram of the application when the user clicks the button. The user interface employs a Model-View-Presenter (MVP) pattern to convey in a single presenter class all the logic behind the UI. The presenter, in turn, invokes an internal QuoteService class that ultimately responds by providing a StockInfo object with any information to be incorporated in the user interface (see Figure 4). The QuoteService class doesn’t attempt to retrieve quotes itself. It first creates a provider component and then yields to it. The QuoteService class implements a very simple algorithm. If there’s } an Internet connection, it uses a provider class that uses a public Web service to get financial data; otherwise, it switches to a fake provider that just returns random numbers. So at some point, the QuoteService class needs to check for an Internet connection, as shown in Figure 5. So far, there’s no difference between Silverlight and WPF, and all the code is fully reusable. To check for an Internet connection in .NET, you can use some of the static methods on the NetworkInterface object. The object is defined in the System.Net.NetworkInformation namespace. In particular, the GetIsNetworkAvailable method returns a Boolean value that denotes whether any network connection is available. Unfortunately, this doesn’t say much about Advanced Document Imaging Toolkits Zero-footprint Viewing, 3 Months of Free Gold Support, No Sweat. Features include Runtime Royalty-Free OCR, PDF annotations, ISIS support, CAD/CAM and WPF support, AJAX web image viewing with no plug-ins, and more—Take document and photographic imaging to the .NExT step. Visit Atalasoft.com for web demos, video tutorials, and a free trial. Atalasoft, Your .NET Imaging Partner Call us toll free at 866-568-0129 msdnmagazine.com www.atalasoft.com October 2008 39 http://www.atalasoft.com http://www.Atalasoft.com http://www.msdnmagazine.com
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.