Channels ▼

Web Development

Building Line-of-Business Apps with Silverlight 2

Data Access

Describing a line-of-business application almost always results in mentioning a "grid". Displaying tabular data with the ability to sort, filter or otherwise augment the data is core to just about every LOB-style application. The Silverlght 2 toolbox is full of controls to get you started, including a DataGrid. Sliverlight DataBinding scenarios closely match that of WPF, so if you've already started getting up to speed on WPF you're a step ahead of the game. ASP.NET developers will also be used to the idea of Templates and declarative databinding. Setting up bindings can be done at an individual component level like you're used to with Windows Forms or ASP.NET, or you can set up a DataContext which will be inherited by all controls in a container. Using the DataContext will certainly be valuable in a line-of-business application, where textboxes and checkboxes representing the underlying data are ever present. Listing Five shows an example of a typical binding scenario, where the TextBox is bound to the Description field of the current DataContext. The DataContext in this case is set on the root element, the UserControl. The DataContext can simply be the entity business object retrieved via your WCF web service.

<TextBlock x:Name="labelSettings" Text="{Binding Description}"></TextBlock>

Listing Five

When binding the DataGrid, you'll be working with the ListSource property, which works much like the DataSource property of a classic .NET grid. How to bind the grid is not nearly as interesting as how to get the data in the first place though. Again you can turn to WCF, which provides the most straightforward means to transfer data between the server and the client. Once the communication channel is in place, your attention can turn to the actual data. For new applications which don't already have an underlying data model in place, using LinqToSql to generate dataclasses is worth a strong look. The trick to getting LinqToSql dataclasses to serialize through WCF is to set the DataContext's SerializationMode to Unidirectional, as in Figure 2. Setting the SerializationMode for the DataContext informs the code generator to decorate the classes and properties with the proper DataContract attributes. If you already have your middle tier business objects created you can certainly use those as well, but you'll want to mark those classes with the appropriate WCF DataContract attributes for proper serialization.

Figure 2

Along with the proper DataContract attributes, be sure that your business objects implement the INotifyPropertChanged interface to enable two-way databinding. In this case, two-way databinding means that data changes will be propagated to all consumers. For example, if you were to make changes to an underlying business object, the DataGrid bound to that business object would reflect those changes. Just as if you were to update the DataGrid, the business object would reflect the change as well. Remembering that Silvelright is client-centric, you still have to push data changes from the client to your back end server. The same web service you used to get your data to the client can be used to push changes back to the server. WCF simplifies this task, since pushing business objects over the wire is a trivial task.

A Note on Silverlight's DuplexService

The idea of a DuplexService in Silverlight is extremely attractive, as it enables data to be "pushed" from the server to the client. However, the underlying implementation is still dependant on HTTP connections, which do not enable server initiated communication. To get around this, Silverlight creates a polling system over an HTTP connection left open for a configurable amount of time. Because the entire polling model is neatly wrapped up and abstracted out in the DuplexService, the result is a service that feels like a server-side push. Unfortunately, this functionality is still not quite ready for prime time. The DuplexService is not part of the core Silverlight stack, and is provided in a separate evaluation assembly with its own special license for use. If you absolutely must have server push technology in your Silverlight application, this at least gives you something to work with, but for a typical application, its best to wait for this service to mature a little more before diving in.

Model View ViewModel (MVVM)

With an understanding of the internals of a Silverlight application, it's time to take a broader look at building a line-of-business application in this new environment. Application patterns have exploded in popularity lately. Take a look at what's going on with ASP.NET MVC as a prime example. Developers are looking for ways to make applications easier to work with, and especially easier to test.

You may have heard about Model-View-ViewModel (MVVM) already, as its quickly being adopted in the WPF world. MVVM is an application pattern geared towards improving the applications development and testing processes. The same reasons why MVVM is attractive to a WPF application, most notably the clear separation of design (XAML) from code, make it attractive for a Silverlight application. The goal of MVVM is to separate the design (View) from your underlying page logic (view-model), enabling designers to work completely with the XAML and developers to stay entirely in the code. Aside from being an easy pattern to follow and implement, it will pay dividends in testing and maintenance of your application. Even if you dont have a separate designer working on your XAML for you, this pattern is still worth a look at. It's easy to see why this pattern is becoming so popular so quick.

The MVVM story is a little bit different for Silverlight applications. Being a subset of WPF, silverlight doesnt contain everything that WPF does (as many WPF developers have found when trying to find Triggers in Silverlight...) The commanding framework in particular, is a core piece of the MVVM implementation for WPF, but is missing from the Silverlight subset. Since commands do not exist in Silverlight 2, a bit of custom code is required to glue the view (XAML) to the view-model. Luckily, this problem has already been solved, and the solution along with full source code can be downloaded and used in your project. For details, take a look at Nikhil Kothari's blog.

The Future of LOBs

The Silverlight 2 platform is powerful and familiar, yet different and strange at the same time. Learning to get used to the asynchronous model is something that will only come with experience. Combining rich-client concepts with web-client strategies is something that will take time to get used to. Building some of the richest Internet applications ever seen, is something that doesn't take much time to get used to at all. The topics covered in this article should give you a good idea of what the road ahead looks like. While it may be a new road that youve never traveled before, Silverlight programming is a combination familiar concepts and designs. The limitations fueling dreadful looking line-of-business applications are quickly disappearing. Many Silverlight demos focus on the appeal side of user experience through gratuitous use animations and video, but behind all of that glitz lives the framework for Line-of-Business 2.0.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.