The Illusion of Portability

So, I’ve been looking at a lot of Microsoft technology recently, especially a lot of the technology based on the .NET platform, and for a while I was really excited about some of the possibilities they presented. I thought that, in the future, I’d be able to write a single C# library, and use that library for programs on my phone, through Silverlight, XNA, and WPF. Imagine, being able to write the central logic for something like Sort once, and have it work on 4 different platforms without needing to do too much more work. Even better, imagine being able to write controls that would work on both the Silverlight and WPF platforms, basically out of the box. The whole concept is just very exciting (to me anyway).

So, with Sliverlight 1.1 Alpha now out, I decided to bite the bullet and grab Orcas as well as the SDKs and helpers to write a Silverlight application. The first thing I wanted to try, of course, was attempting to put a Windows Class Library reference into a Silverlight project. No go. I then decided to experiment with the *other* combinations, to no avail. For each platform, you apparently have to have a matching class library project in order to reference it. It was a huge let down. If there’s any way to have a portable class library in this fashion, it’s apparently not something easily accomplished.

Now, I can understand why this is necessary. Although the IL for simple programs for each of the platforms is basically similar, I know that Silverlight and the .NET Compact framework reference different .dlls behind the scenes. I know that they probably have different version and loading information, and I know that I can’t use all of the features of the .NET framework for Silverlight or my phone. But if I want to do simple logic that’s repeatable, why can’t use a single project, compiled to separate platforms when needed? Why can’t Visual Studio take advantage of its own platform drop down to actually make this work? Or, better, why can’t it automatically detect which project is using it and compile the version specifically for that platform?

Now, there are ways around this limitation. Sort-of. You can, if you want, put multiple projects in the same directory, all referencing the same code files, and specifically set their output (and object) directories to different locations (say /bin). In this case, you can compile to all the platforms simultaneously yourself, so long as you don’t mind 5 projects around that all reference the exact same files. As you can probably tell, that tends to be a pain in the ass, but it is what I’m going to try for the next few weeks to see how it works out. I just wish there were a simpler solution.

Now, if I were Microsoft finishing up Orcas (which I’m not, and, let’s face it, they’ve probably thought about this possibility already), here’s how I would play this to get a lot more people on board developing smart and portable applications in the .NET Framework. If possible, provide one more class library type, say “Portable Class Library”, which supports only the features that are available on Silverlight, XNA, Windows, WPF, and .NET Compact. Allow any of those projects to reference the library, and compile a version for each platform when requested. Additionally, I’d also love to see a “Portable Control Library” that would allow development of WPF and Silverlight controls simultaneously, and compile to the correct platform automatically. That would speed the acceptance of Silverlight dramatically I think. At least it would get me thinking about multiple platforms every time I sat down to write a new application.

Leave a Reply »»