The background for this article is a discussion thread on the Regional Director discussions list. One of my fellow RDs asked the question weather Silverlight 2 was ready for business applications or not. Based on my reply to the discussion Derek Hatchard asked me to turn it into an article he could publish on the "{You Shape} it" site. The article is now published, but since the {You Shape} it site doesn't support comments, and to make this article available to my subscribers who might not know of the site, I've decided to publish it on here as well. Microsoft Regional Director Rockford Lhotka has published a blog post with the same title as this article. I recommend reading it for his views on Silverlight for business applications. Rocky's post is written as a response to a Ivan Dragoev's post which suggest that the Silverlight 2 platform might not be ready for some serious business/data scenarios yet.
Introduction
With the initial release of Silverlight back in 2007 the focus was on building rich online media experiences with support for high definition video content in the browser. However, there were not many features addressing the needs of developers building data centric line of business applications. With the recent release of Silverlight 2 application developers are asking themselves if Silverlight is ready for business applications. Short answer: definitively yes! With Silverlight 2 you get the power of .NET in the browser, a rich control model, strong data binding support and a solid networking stack with support for WCF, ADO.NET Data Services and REST services. Essentially all the building blocks you need to start developing great Rich Internet Applications that solve business needs.
In this article I will try to highlight some of the strengths of Silverlight 2 as a platform for building data centric business applications, compared to traditional web applications. I will also try to address some of the challenges and concerns developers have around adopting Silverlight 2 today. Some of these concerns are around things like printing, search engine optimization, deep linking and bookmarking. All of these issues can be addressed using various techniques I will touch on in this article.
New Possibilities with Silverlight 2
I figured a good place to start would be by looking at some of the new scenarios enabled by Silverlight 2 that previously would have been impossible or really hard using regular web technologies such as HTML and JavaScript. For most developers, the greatest thing about Silverlight 2 is that you get .NET running in the browser on Windows and OS X (and Linux in the future). You can use your favorite programming language to build your application, supported by the productivity of Visual Studio 2008. You no longer have to use JavaScript to write code on the client (unless you want to). Tooling support for JavaScript was greatly improved in Visual Studio 2008 but still has a long way to go to match C# and VB.NET. Silverlight 2 supports C# and VB.NET out of the box, and you can also write Silverlight 2 applications using dynamic languages such as IronPython or IronRuby.
Having .NET in the browser lets you use the same skill set and programming languages on the client that you use on the server and provides a solid foundation for coding client side validation, business rules, and application logic.
Offline Storage of Data
Silverlight 2 supports offline storage through Isolated Storage, a protected area on the user's hard drive. Each application gets its own isolated space where it can read and write data. This can help application developers improve performance by serializing reference data to XML or JSON and stored it locally for later use. The next time the user loads the application startup time will be improved as reference data is read locally.
Another scenario where offline storage could improve user experience is by saving uncompleted forms. How many times have you accidently closed the browser before completing a web form or had to come back later to complete a form because you where missing some details? In those scenarios the application could save form values locally as the user types. If the user closes the browser and later comes back to complete the form, the application can read the form values from isolated storage.
Access to Local Files and Improved File Uploading
Security is a top priority in Silverlight 2 and there is no way you can read arbitrary files on the hard drive. However the user can choose to give Silverlight 2 access to a local file using the file open dialog. Unlike the standard HTML file input element, you can access the content of the file passed in locally. This can be used to create improved file uploaders that support multiple files at the same time with accurate progress indicators. Or you could use local file access to import and transform CSV or XML files locally before sending the data back to the server.
Multi Threading, Real-Time Networking, and Cross Domain Services
Silverlight 2 supports multi threaded programming, something currently not available in JavaScript. This allows for more complex work on the client without blocking the user interface. Examples of this could be financial calculators, complex validation logic, or other long running background tasks.
The Silverlight 2 networking stack allows real-time network communication over sockets. The client application can open a connection to the server, and the server can push data back to the client. This enables new scenarios, such as constantly up-to-date stock tickers and real-time monitoring of industrial processes or other physical equipment.
One of the limitations developers often run into when working with AJAX is calling web services from other domains than the one hosting the page. In Silverlight 2 cross domain networking is supported if the service explicitly allows this through a policy file. This enables you to call services such as Flickr, YouTube, and Amazon directly from the browser and even access web services hosted on other machines inside the enterprise.
Rich Control Framework for Great User Experiences
So far I have only covered non-visual aspects of Silverlight 2, and in fact all the features I have mentioned so far you could use in a traditional web application by hosting a non-visual Silverlight 2 application and integrate it using the HTML Bridge. But the place Silverlight 2 really shines is the presentation model. You get a control model similar to the one in WPF, and out of the box Silverlight 2 ships with the main controls you would expect like TextBox, Button, ListBox, ComboBox, DataGrid, Slider, DateTimePicker, and many more.
Having a strong set of controls is a key requirement in any data centric business application, and Microsoft has announced that they will continue to improve the Silverlight 2 control story by releasing additional controls as a Silverlight Toolkit hosted on CodePlex. The first batch of new controls will be made available at the PDC next week and will include controls such as TreeView, Expander, Label, Accordion, NumericUpDown, AutoComplete, and a couple more. This is just the first drop of controls and Scott Guthrie has hinted that they expect to ultimately have more than 100 controls available on CodePlex. If the controls from Microsoft do not meet your requirements there is already a large ecosystem of third party controls available.
All Silverlight 2 controls support data binding, making it easy to bind your user interface components against properties on your business objects. This greatly reduces the amount of code you have to write in the UI layer as you no longer have to write the repetitive code of moving values from your textboxes onto your business objects. By applying design patterns like Model-View-ViewModel you can build user interfaces with literally no code behind, making your application easier to unit test.
Strong data binding support combined with the power of .NET makes it easier to write complex validation rules on the client. This enables you to do more validation on the client side without having to do round-trips to the server to validate user input. This isn’t something new to Silverlight 2 and can be achieved using JavaScript. However, writing the validation rules in managed code is easier and more productive compared to JavaScript. In some cases you would also be able to reuse some of your business logic code between server and client. A framework like CSLA.NET can help you architect your application in such a way.
One of the strengths of the Silverlight 2 control model is the separation of visual representation and behavior. Using XAML you can redefine the look and feel of a control without losing its behavior. A slider control to set a visual indicator of air pressure in a tank can be restyled to look like a tank where you can drag content up and down. Checkboxes to set gender can be redesigned to use a male/female icon, but still keep the behavior of a checkbox. Similar things can be achieved using HTML and JavaScript, but Silverlight 2 makes scenarios like this real easy and the styling can be done by a designer without having to change the underlying code using the checkbox or slider control. The clean separation of XAML and interaction logic enables new designer-developer workflows previously hard to implement.
Visualizations and Animations
In addition to powerful controls, the Silverlight 2 presentation model has great support for vector graphics, multimedia, Deep Zoom imagery, and animations. Some might dismiss these features as something not suited for a business application. But who said a business application had to be boring? Using the vector drawing capabilities you can create graphics to visualize your business data in new ways. Already there are multiple charting and gauge controls available. Deep Zoom enables you to view gigapixel images over the Internet, opening up new possibilities for data visualization previously not possible. The Microsoft Health Common User Interface application demonstrates how a radiologist could examine and annotate x-ray imagery remotely using Deep Zoom and Silverlight 2.
Animations, when used appropriately, can greatly enhance the user experience of a traditional business application. Based on selections made by the user, animations can be applied to emphasize certain sections of the page, or to show or hide sections based on the selection. Billy Hollis recently did a DotNetRocksTV episode demonstrating a traditional business application where animations greatly help improve the user experience.
WPF Portability
Silverlight 2 uses a subset of the .NET runtime, and the presentation model is based on WPF. This allows for both code and skill reuse between platforms. Silverlight 2 is a web technology and there might be cases where you want a full desktop version of your application. Access to local resources, support for offline access, or integration with Microsoft Office could be reasons to use WPF. If you decide to make a WPF version of your Silverlight 2 application you will be able to reuse most of your business logic code directly. There are some differences in the XAML and control styling model between WPF and Silverlight 2 that you have to take into consideration when porting your user interface. Silverlight 2 uses a new Visual State Manager component to manage the look and feel of a control in different states. A checkbox, for instance, will have one look for the checked state and one look for the unchecked state. In WPF you manage different states using triggers, something currently not supported in Silverlight 2. Microsoft has announced that they will include the Visual State Model to the next version of WPF to align it with Silverlight 2. While waiting for the Visual State Model to be implemented in WPF you can use Microsoft Expression Studio architect John Gossman's implementation. This would help make portability of Silverlight 2 applications to WPF easier.
Since Silverlight 2 and WPF support the same vector graphics you will be able to reuse any graphic assets created in Expression Design or Adobe Illustrator. This is great for logos, icons, or any other illustration your designers would create for your project.
Arguably the most important piece of reusability between Silverlight 2 and WPF is skills / experience. The two platforms have so much in common that if you start out with Silverlight 2, the transition to WPF will be a lot easier. Silverlight 2 has a smaller feature set than WPF, making it a more approachable starting point.
If you want to learn more about WPF portability, I have a blog post describing the process of porting my Dive Log sample application from Silverlight 2 to WPF.
Deep Linking, Search Engine Optimization, and Printing
One question I get asked a lot is how search engines deal with Silverlight 2 content. From a business application point of view, this might not always be an issue as many applications will be hosted inside the enterprise behind password protected sites. However, there are scenarios where search engine optimization is important for Silverlight 2 business applications. One example could be an online product catalog created in Silverlight 2. I'm sure the marketing department would be rather concerned about the discoverability of their products using search engines. The two key things to think about for search engine optimization are indexability and relevance. Indexability is achieved by addressing what content is visible to the crawler and where the crawler should look. Relevance is achieved by providing deep linking to individual pieces of content so that people can link to your application.
In our product catalog example deep linking could be implemented by adding a parameter to the URL hosting the Silverlight 2 application indicating which product to load. When the Silverlight 2 application loads it could read the URL and load the correct product. This would enable users to share links directly to a specific product or add a bookmark. At the same time the web server rendering the initial page hosting the application could provide a HTML representation of the product page hidden underneath the Silverlight 2 control. The user would only see the Silverlight 2 representation of the product catalog, but the crawler could index the HTML content underneath. To help the crawler find all the products you could supply a sitemap file containing links to each individual product in the catalog. For more information on search engine optimization check out Nikhil Kothari's blog post on Search for Rich Internet Applications.
Printing in Silverlight 2
By default Internet Explorer 7 supports printing of Silverlight 2 content but other browsers such as Safari and Firefox currently don't. For our product catalog this would be a major issue. Rendering HTML content behind the Silverlight 2 control would not only improve indexability, it could also help us improve the printing experience. Using standard CSS print stylesheets, we could hide the Silverlight 2 container when printing the page and instead apply styles to show the HTML content sitting behind it. When the user browses the catalog and selects new products we could use the HTML Bridge in Silverlight 2 to dynamically upgrade the HTML content based on the currently selected product. This way the user always gets a nice looking printout in all browsers. I recently did a blog post describing this technique, as well as covering printing in Silverlight 2 in depth.
For some business applications with more advanced reporting requirements, CSS print stylesheets might not be enough. In those cases you could use the HTML Bridge to invoke JavaScript to open up a new browser window containing a server side generated report. The report could be any document, such as a PDF or XPS file, or simply an HTML page rendered on the server. Since the rendering is happening on the server you are free to choose any technology or reporting framework to create your reports.
Summary
With the recent release of Silverlight 2, now is a great time to start considering Silverlight 2 for your line of business applications. With .NET running in the browser and a powerful presentation model, you now have the building blocks needed to build compelling Rich Internet Applications for use internally or externally on the Internet. Silverlight 2 is a young piece of technology so best practices and patterns are still emerging. Thankfully the similarity with WPF gives us a great starting point, and already application frameworks like Prism and CSLA.NET are being ported over to Silverlight 2. I also expect Microsoft to start adding value on top of Silverlight 2 in terms of application frameworks, reference implementations, and sample applications.