Had A Quick Look at SilverLight. Don't Like It Much.
We've probably all heard of Microsoft's "Flex killer", SilverLight, but how does it stack up against Flex? As a simple comparison exercise I thought I'd try and reproduce the Flex contacts database in SilverLight.
By the time I'd added one XML-consuming DataGrid I gave up in frustration. I don't think Flex has much to worry about for the time being.
Here's the Silverlight app I started which is consuming the same XML as it's Flex counterpart. As you can see, I didn't get far.
Getting Started
To start developing the SiverLight (SL) app I launched Visual Studio Web Express (free!) and created a new SL project.
For the sake of argument let's assume most Flex or SilverLight business applications are going to be centred around some kind of Data Grid / "view", which fetches XML from a web server. Because most apps are like this. Are they not!
So, first thing I did was add a DataGrid and then I did a quick Google to find how to make it consume XML from a URL. I quickly found the code I needed and pasted/modified it to work with my own XML schema.
The C# code-behind for the SL app is, in essence, like this:
namespace SilverlightDominoDemoApp { public class Contact { public string ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string DOB { get; set; } public string Email { get; set; } } public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); //make a new WebClient object WebClient client = new WebClient(); client.DownloadStringCompleted += client_DownloadCompleted; client.DownloadStringAsync(new Uri("http://www.codestore.net/apps/contacts.nsf/vwContactsAsXML?OpenView")); } void client_DownloadCompleted(object sender, DownloadStringCompletedEventArgs e) { if (e.Error == null) { XDocument xml = XDocument.Parse(e.Result); var contacts = from contact in xml.Descendants("documents").Descendants("document") select new Contact { ID = contact.Attribute("id").Value, FirstName = Convert.ToString(contact.Element("first_name").Value), LastName = Convert.ToString(contact.Element("last_name").Value), DOB = Convert.ToString(contact.Element("date_of_birth").Value), Email = Convert.ToString(contact.Element("email").Value) }; myDataGrid.ItemsSource = contacts; } } } }
What this code does is fetch the XML and then, in the "on complete" event listener for the web request it loops all the "document" nodes using LINQ and creates a new object based on the Contact class. It then binds them to the data grid.
At this point alarm bells were ringing. What's this strongly-typed class I need to add? This means that to add or remove columns I need to modify the class and re-compile it all?!
You might remember in part 1 of my Flex App Basics series I talked about building views remotely. The idea being that the grid is designed on the server. The XML defines the columns to display as well as the data for the grid. The SilverLight way looks like it won't adapt itself to working this way. At least not easily. I'm sure there's probably a way to do it, but if it requires an in-depth knowledge of SilverLight in the first place, then it's failed at the first hurdle.
One of the things I have always loved about Flex is how easy it is pick up and how intuitive it is to develop with. Lots of what I've found out about Flex I've found out simply by guessing what might work, trying it and finding out it does.
On the other hand, after spending a couple of hours with SL I've found it to be quite un-intuitive. Almost as though it subscribes to the "it's enterprise class, so it needs to be complicated". You can tell it's a Microsoft product.
As an example, I could find no easy way to set the width of the DataGrid to 100% as percentage values aren't allowed. While I'm sure there's a way to do this, the fact I even need to think about in the first place is kind of off-putting.
On the plus side. The Silverlight app was created at no cost (thanks to the Express version of Visual Studio I used), whereas Flex/Flash Builder Pro cost me about 400 quid! Although I see that as no price considering what I've had out of it.
Summary
While what I did was far from an exhaustive comparison I did see enough to know that I don't think it's worth considering SL as a viable alternative to Flex.
Part of the reason I ever considered its use was that I assumed it would be easier/quicker to integrate it with an ASP.NET/Microsoft background. Whether this is true or not I don't know. Either way I can't imagine there's anything that radical about it that would make it worth re-learning what I know of Flex.
It just re-affirms my belief that Flex is an amazing product without limits as to what can be done with it.
Fortunately, you can use flex with a .NET backend via XML. To be fair, I suppose, Silverlight is a come-lately compared to flex which is built on almost a decade of flash refinement, so rough around the edges ought be expected, although not accepted. If MS is serious about challenging flash/flex, they'll be working mightily to improve silver light as they go. Then again - knowing what I know about .NET and C# so far, I don't expect huge leaps in intuitiveness. :-)
Reply
You may want to look at Google Web Toolkit for this sort of thing too specifically Ext-GWT.
There is example code to do the same sort of thing is here:
http://www.sencha.com/examples/explorer.html#xmlgrid
Reply
Similar to a native Chinese tries to speak German. Because the Chinese doesn't get into German, doesn't mean that German is a 'bad' and 'unproven' language.
Reply
Sorry, all this confirms is that you know very little about Silverlight; and copy & paste coding is rarely successful if you don't really understand what you're doing. I'm sure I'd be equally frustrated if I tried to use Flex to do the same thing without being familiar with it (luckily I'm experienced with both).
Flex and Silverlight both have their strong and weak points, but I've pretty much moved over completely to Silverlight simply because of the flexibility and superior tools.
Reply
I already knew I knew nothing about Silverlight.
using your knowledge of both are you telling me you can build a Silverlight datagrid on the fly using XML from the server to add columns and bind them to the rows of data in the same XML file, while being able to add/remove columns as you please?
Reply
Show the rest of this thread
This is one way to do it but certainly not the only way. You can also just specify the DataGridColumns in the XAML:
<sdk:DataGrid Height="191" Margin="60,41,39,0" VerticalAlignment="Top" DataContext="{Binding}" AutoGenerateColumns="False" ItemsSource="{Binding documents}" >
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Binding="{Binding company}" Header="company"/>
<sdk:DataGridTextColumn Binding="{Binding county}" Header="county"/>
<sdk:DataGridTextColumn Binding="{Binding date_of_birth}" Header="date_of_birth"/>
<sdk:DataGridTextColumn Binding="{Binding detail_icon}" Header="detail_icon"/>
<sdk:DataGridTextColumn Binding="{Binding email}" Header="email"/>
<sdk:DataGridTextColumn Binding="{Binding files}" Header="files"/>
<sdk:DataGridTextColumn Binding="{Binding first_name}" Header="first_name"/>
<sdk:DataGridTextColumn Binding="{Binding id}" Header="id"/>
<sdk:DataGridTextColumn Binding="{Binding last_name}" Header="last_name"/>
<sdk:DataGridTextColumn Binding="{Binding mobile}" Header="mobile"/>
<sdk:DataGridTextColumn Binding="{Binding office}" Header="office"/>
<sdk:DataGridTextColumn Binding="{Binding post_code}" Header="post_code"/>
<sdk:DataGridTextColumn Binding="{Binding starred}" Header="starred"/>
<sdk:DataGridTextColumn Binding="{Binding street}" Header="street"/>
<sdk:DataGridTextColumn Binding="{Binding title}" Header="title"/>
<sdk:DataGridTextColumn Binding="{Binding town}" Header="town"/>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
Then at runtime set the DataContext to your downloaded XML file (like you are already doing).
In a way, this may feel like the Strong Type you didn't like from above, and I see the criticism. I also disagree that most business applications are getting their data from random XML files. True business applications almost always are aware of their data layout, and most business data comes from Databases, which these days means strongly typed ORM classes --- but I digress.
If you really want it to be flexible, then you can parse the XAML and create those DataGridColumns at runtime based on the parsing. And you can still do all this without ever executing that LINQ statement to get a "Strong Type".
I also completely disagree that "most" Silverlight apps are going to be centered around a DataGrid. In my opinion, DataGrid is a bastard control that should have never been implemented. It was added out of band because people used to Web and WinForms were so used to being constrained by it that at first they didn't know what to do without it. Silverlight offers an insane amount of flexibility in how to present data that I refuse to simply stick to the old "row and column" approach. One control does not a technology make.
And this leads to my larger point: you have based your opinion of Silverlight on a single experience, with a single control, and a single task, using code copied off the web, without any real understanding of how the technology works or what options you have. And then, because it is not the same as Flex, you have essentially dismissed it.
"if it requires an in-depth knowledge of SilverLight in the first place, then it's failed at the first hurdle."
I have never used Flex, but I assume it would take me more than a few hours to learn how to use it, and certainly longer to formulate a fair opinion. Maybe I could learn this particular task quicker, who knows, but to do anything meaningful I would need "in-depth" knowledge of Flex. Any technology is going to require more than cursory effort to be competent.
For instance, you said:
"As an example, I could find no easy way to set the width of the DataGrid to 100% as percentage values aren't allowed. While I'm sure there's a way to do this, the fact I even need to think about in the first place is kind of off-putting."
Yes, Silverlight has a different sizing model, one that is infinitely more flexible than simple percentages. It is not any harder to use, just different. Did you drag the DataGrid onto the design surface? If so, it probably created additional XAML you don't want like so:
<sdk:DataGrid Height="191" Margin="60,41,39,0" VerticalAlignment="Top" ... >
This is a fault of the IDE, not Silverlight itself. If you want this to fill its parent space, then remove those unnecessary attributes:
<sdk:DataGrid ... >
These are rudimentary tasks. You say you shouldn't have to think about them, but XAML is a different way of doing things, and so it requires some effort to learn. I say it's a shame you haven't done so.
Reply
Again regarding not being able to set the grid width to 100%. If the grid is a component in an application it will probably be added to some sort of container.
In that case I would prefer the container to determine how the grid fills the container. The reason for this is the container knows about the grid but the grid does not have to know about the container making the application more loosely coupled.
Reply
Percentage sizing isn't directly supported - all you do is create a grid layout control (not the same as the DataGrid) and create relative sized columns or rows.
e.g two columns sized 2* and * would take up 2/3 and 1/3 of the space respectively. You can also have fixed sizes or "auto", which takes up as much space as the child needs.
Reply
Having used Flash from version 2 and Silverlight since it's days as WPF/E. I'd like to suggest that you don't give up hope. Cut and pasting code isn;t always guaranteed to work!
Given the whole picture - Silverlight is very, very good.
Reply
Well done post here. You've got many laughs out of it.
Reply
Jake, what you need to play with Silverlight is Microsoft Expression Blend. It basically takes care of the UI building part and coupled with Visual Studio 2008 or 2010 for back end coding, you will have a much better experience. Grab the evaluation versions if you want to give SL another try!
But I think Flex being more mature than Silverlight, it will have an edge over SL for some time
Reply