Building Line-of-Business Apps with Silverlight 2.0: Part 1

Posted by Dave Bouwman | Posted in .NET, Silverlight | Posted on 10-06-2009

1

While I’ve been playing around with Silverlight for a while now, the time has come to build something a little bigger with it. Here’s the situation:

  • I’m Converting an Access/SQL Server application over to use a Silverlight 2.0 front end with ADO.NET Data Services on the back.
  • Database already exists, with 83 tables, including users and roles.
  • We need custom Authentication and Authorization services to control who can view/edit data
  • Application will have about ~30 User controls, which will be composed into about ~20 screens

Given this situation, I needed to find a framework that’s proven instead of trying to hack something up myself.

My goals were:

  • the client side Silverlight code needs to be unit testable
  • clean Separation of Concerns where ever possible – both to support unit testing and so we use "fakes" during development
  • Something that’s documented so myself and others on the team can figure it out.

After some Googling, it came down to two options:

Hand-Rolled Model-View-ViewModel (MVVM) or the Composite Application Library (aka Prism v2) from Microsoft Patterns and Practices.

Having used Enterprise Library, I knew that the MS Patterns and Practices stuff would be complex, but it would work. In an effort to keep things simple, I took a peek at "raw" MVVM first.

I’ll also mention that Nikhil Kothari has created Silverlight.FX – “light-weight application framework for building Rich Internet Applications with Silverlight 2”. While I have huge respect for Nikhil, and the project looks promising, it seemed a little too big to take on with just his blog posts and source code as documentation.

"Raw" MVVM in Silverlight 2.0

Conveniently there is a fair bit of information on MVVM floating around, so it was relatively easy to get the gist of things. One of the best resources was this screen cast by Erik Mork at Silver Bay Labs.

silverbay

Erik walks through the basics of the pattern by referencing an article by Shawn Wildermuth in MSDN Magazine, and then shows how to build out a very simple sample application based on the article’s code, and shows how to test the ViewModel using the Silverlight test harness.

As Erik notes here (he uses a media player that sync’s the code display with the video, and it’s bookmarkable!), the down-side of using "raw" MVVM in Silverlight is that you still need to have some "goop" in the code behind that fires events back into the ViewModel (due to the lack of "commanding" in vanilla Silverlight). Ideally we’d like to avoid the code-behind and have the ViewModel linked to the View (XAML) declaratively.

Another downside is that while MVVM provides a pattern for the individual views, it does not provide much infrastructure to create an application that’s composed of many views – I’d have to roll this myself or hack parts from a variety of samples – not idea. Conveniently though, we can use MVVM with Prism…

Composite Application Library aka Prism

Many people have heard of the “Old” Composite Application Block (aka “CAB”), and that alone could be enough to scare some people off. Although it shares a similar name, the Composite Application Library is much different. Prism is a set of components, services and patterns that can help with the construction of  MVVM Silverlight applications. It includes:

  • The Microsoft Unit Inversion of Control Container
  • Commanding for Silverlight (declaratively linking the ViewModel to the View in XAML)
  • Centralized Event Aggregation (makes loosely coupled designs much easier)
  • Support for UI composition from multiple views

Getting Started with Prism

As like most other things from Microsoft Patterns and Practices, this stuff is both complex and well documented. In fact, the Guidance is simply amazing. It has overviews of all the key components, solid descriptions of how/when to use them, as well as “Quick Start” project that show the concept in action. They also have a “reference implementation” which seems to combine all possible aspects and combinations in one project. I’d hold off on looking at this until you’ve got a good handle on the individual components though, as it’s pretty hairy.

But to get a good idea of how Prism works, there are some good videos I recommend checking out:

Erik Mork of SilverBayLabs.org has a couple of great really simple intro videos. These were great for introducing the basic components – the shell, models, viewmodels and how they relate.

Channel9.com has a 4 part series with Bob Brumfield and Erwin van der Valk from patterns and practices showing to build a modular application using Prism.

At this point I’ve managed to get my app’s structure laid out, I’ve got login capabilities which correctly add cookies which allow access to secured services, and I’ve got a few rough modules and a rough means to transition between them. Learning Silveright, XAML, Expression Blend, MVVM, Unity, and Prism all at once made for a pretty harsh learning curve, but I’m past the roughest parts. In some cases I’ve departed from MVVM due to some limitations on databinding (twoway data binding to a PasswordBox is apparently verboten), and I have a little “goop” in my View code-behinds, but I think these are corner cases, and the “main” modules (the ones with the business logic) should be MVVM.

When I get some time, I plan on posting some notes and sample code for things which were difficult to understand, or implement.

Comments (1)

I’ve looked at the first two in this video series and I find them to be helpful so far.

SlickThought.net: http://www.slickthought.net/category/WPF-Composite-App.aspx

In fact I find these to be short, digestible, and at the right level of detail. They also combine good use of code and diagrams/slides.

“Learning Silveright, XAML, Expression Blend, MVVM, Unity, and Prism all at once made for a pretty harsh learning curve…”

I am sort of in the same boat as you on that…and I agree it’s alot all at once.