Silverlight has too many flaws to justify its benefits

In this guest post by Nate Irvin, he argues that Silverlight has too many flaws to justify its somewhat dubious benefits. Do you think that .NET developers should discourage the use of Silverlight?

This guest post by Nate Irvin is a reprint from his blog A Very Small Knowledge Base.

One of the bigger paradigm shifts in .NET development that has occurred in the last few years is the introduction of Silverlight and XAML. While often mentioned together, the two are not one and the same: Silverlight is an application framework that's very tightly coupled to Internet solutions, while XAML is a markup language that can be used in many different types of .NET projects.

Silverlight is nice in that it further simplifies web application programming. Some web sites are actually not suited to a model where the user can control page flow, and some user interfaces can only be accomplished in an HTML+JavaScript environment after much trickery and tweaking third-party controls (e.g. jQuery UI). Silverlight allows you to deliver an application in a server-client and/or web-based manner without having to dance around the stateless, static-content-oriented HTTP protocol.

Silverlight's architecture has some good ideas behind it. Applets have been a source of (real and perceived) security concerns for a long time, so the Silverlight designers decided that Silverlight would be a subset, instead of a super-set, of the .NET framework. In other words, Silverlight uses a smaller selection of .NET's functionality.

This deliberate scope restriction takes away the ability of Silverlight applets to do some dangerous things. The code to access the local file system, databases, and other critical resources is not even there. It enforces a safer application ecosystem by design instead of by potentially breakable (and in the end arbitrary) access switches.

It also solves (or at least mitigates) another concern of applets. Applets have to have their own execution sandbox that the client has to download, in addition to downloading the applet itself. A smaller functionality set compiles to smaller binaries, which results in smaller download and install footprints. Even with today's high-speed Internet connections, dual-core processors and hundred-gig hard drives, all resources are still finite, so creating a paradigm that rewards smaller deliverables is a very smart idea.

These benefits do not come without some significant hassles, though.

One of the biggest problems I've had with Silverlight is that various versions are not compatible. You can install and run .NET 1.1, 2.0, and 4.0 all on the same machine without any problem; you can have solutions that have projects in all different versions; you can have multiple IIS app pools running different framework versions all running simultaneously. Not only is this possible, but it's extremely easy - the setup and execution is all seamless.

.NET is certainly not the only application framework or product that can support this kind of parallelism, but the point is that it does it well. Silverlight does not. I know it's possible to get Silverlight 3 and 4 running on the same machine - I've seen co-workers get it done - but it's a very difficult process, and even those co-workers throw up their hands in surrender when I ask them to help me do it. "I just un-installed and re-installed things in an apparently arbitrary order until it started working" was the answer I got from more than one of them.

On the surface, this sounds like a nit-picky concern - "just use the same version of Silverlight for everything," right? But let's be realistic, it's never that simple. Various applications are developed under different constraints and requirements, and sometimes using only one version is simply not a realistic option. Some clients and environments require an older framework, and you can't change that. Plus, even if you do have the option to upgrade, development hours are limited and business users/clients aren't always willing to assume the upgrade risk.

This is true for anything, not just Silverlight - there are still many .NET 2.0 applications and DLLs running in production environments that won't be upgraded for years to come for these very same reasons. Effective multi-version support is a feature I don't think enterprise software development tools can skimp on, and I feel that Silverlight not only skimped, but completely dropped the ball.

This versioning/parallelism flaw is major, but there are also some important minor annoyances. The subset mentality that Silverlight was designed in is a good idea that was clumsily executed. Instead of Silverlight being a "true" subset of .NET, it's actually a parallel, minimized fork of .NET - it looks like .NET, smells like .NET, but it doesn't taste like .NET.

Visual Studio is always cranky when you try to add a reference to a Silverlight project in a non-Silverlight project - it'll do it, and the solution will compile, but VS will always mark it as a broken reference in Solution Explorer. Tools like ReSharper will even give you pre-compile errors in non-Silverlight code that references Silverlight code (as well as in the solution-wide analysis, which is much harder to ignore).

The path of work-arounds this particular flaw sent me down was a real comedy of errors.

"Hmm, VS2010 + Resharper doesn't play nice with MSTest projects referencing Silverlight projects. Okay, let's create a Silverlight test project. Hmm, no such project type. Okay, we'll just create a Silverlight class library - the whole 'test project' definition is a somewhat arbitrary distinction anyway. Argh, okay, where can I download the MSTest for Silverlight framework? Geez, that's hard to find. Okay, got it! Compiles, woo-hoo! What? The MS Test runner can't run the MSTest for Silverlight tests (doesn't recognize the attributes)? Crap. Well, maybe there's a port of the test runner for Silverlight. Uh ... well there's a crappy browser-based version that's difficult to use and hard to see test failures in... Okay, screw it, let's just go with NUnit, I've never really liked MSTest anyway. NUnit port for Silverlight? Unofficially done, but existent and stable! Score one for open source! Create Silverlight class library, add tests, reference NUnit for Silverlight DLLs ... compiles! Woo-hoo! RUNS! Woo-hoo!"

I share that partly to inject a little levity, partly to argue that NUnit is better than MSTest, and partly to argue that Silverlight is a second-class citizen, even in the Microsoft world. MSTest doesn't like it, Visual Studio doesn't like it, and it doesn't even like itself. There are just so many little "gotchas" in trying to use Silverlight - functionality that has to be re-created or specialized ports of existing tools that you have to employ. The whole paradigm just seems to work against code re-use.

XAML is the markup language that Silverlight uses to create its user-interface components. As mentioned previously, though, it's not tied to Silveright. The Windows Presentation Foundation (WPF), which is intended for desktop applications, also uses XAML. In fact, Visual Studio 2010 itself is written in WPF, and therefore XAML.

A big leap forward in terms of simplicity and portability of UI design, XAML takes everything that was great about HTML, CSS, and Web Forms, and combines them all into something even better. It further closes the gap between Windows Forms and Web Forms - these two technologies used extremely similar but inherently separate structures, but now everything is united under one roof.

You can design for the desktop or the web (as long as that web is Silverlight) using one approach. XAML makes formatting pages/screens much more intuitive than setting up CSS stylesheets or creating application themes, and it makes the flexibility of HTML layouts available to desktop apps. Making a desktop application look pretty is no small feat, regardless of technology, and WPF gives you a shorter path.

Unfortunately, XAML also takes everything that was bad about ASP.NET Data Grids and makes it the standard. The Model-View Model pattern that XAML is intended to employ encourages injecting property, method, and even class names directly in the XAML markup, or in other words, into uncompiled text. I shudder every time I see this kind of thing being done, whether it's in .config documents, vanilla XML, or in 'magic' strings inside the C#/VB code.

As far as I'm aware, there exists no tool that will extend object refactors into the XAML. Given the fluid nature of the XAML data-binding model, it's a difficult task to hope to accomplish, especially considering the fact that the source object doesn't have to be bound in until run-time. Again, this may seem nit-picky, but I argue that it's not. Code is always changing and needs to be flexible enough to accommodate rapid change. This need becomes more and more pressing each year.

Members in XAML {Binding} or {StaticResources} statements are disconnected from the code in a way that discourages and complicates changes. What's even more concerning to me is that it's very easy for incomplete refactors to go unnoticed. It's very easy to change something, have the {Binding} member no longer match, and then that element no longer shows up on the screen - and no one would even notice, even with the greatest QA department in the world, because no error is thrown when said binding fails.

This kind of thing has bitten us more than a few times even with the more strict binding mechanism of ASP.NET Data Grids, sometimes even in production code. I am pessimistic that such occurrences will only increase in a world that relies more heavily on XAML-based implementations.

Now, the good news is that there are ways to get around this flaw in XAML. The traditional, explicit data-binding model of giving controls names and wiring them up in the code-behind can be employed. There are also code-only ways to create the {Binding} using only C# code - they're not as pretty, but they work, and because they eschew magic-string based reflection they are refactor-friendly.

In the end, I feel Silverlight has too many flaws to justify the somewhat dubious benefits it brings. Traditional ASP.NET web sites with a liberal amount of jQuery can provide all the same functionality without any of Silverlight's limitations or contrivances. I would say .NET developers should discourage the use of Silverlight in order to shorten the time till its end-of-life date.

XAML is better than both Windows Forms and Web Forms; while it has some non-trivial pitfalls, they are worth the risk for the benefits gained. I would urge the use of WPF for desktop development, and if it ever becomes available for non-Silverlight ASP.NET use, I would find it preferable to "pure" HTML+CSS.