Mar/100
Project Silver Shorts
Last week I decided to install the Visual Studio 2010 RC for the purposes of messing around with C#, Silverlight 3, and all the new WPF goodness that came with it. I only intended to dabble for a day or so, but ended up having my entire week consumed in what seemed like a moment.
If you have Silverlight installed, you can view the end result of my first experiment after the page break. It is a simple Bing image searcher that displays thumbnails of the results on a canvas.
What is Project Silver Shorts?
More than anything Silver Shorts is an idea, a codename for a collection of demos in C#, most of which have yet to be written, targeting Silverlight 3, that are intended to be Short. As I stumble and bumble my way around .NET, finding new and interesting things to do with it, I will make my experiments available with full source code on GitHub (skip to the bottom if all you care about is code.)
For the developers in the audience, let’s take a look at a simple LINQ query I have fabricated to summarize the goals of Silver Shorts while simultaneously showing off how neat LINQ is.
var blogPosts =
from exp in user.Experiences()
where exp.IsAwesome
select new BlogPost()
{
Title = exp.Concept,
Content = exp.Details,
CodeUrl = exp.GitUrl;
};
To be painfully and obnoxiously clear, this code example is not part of the actual demo program, it is only here to look pretty and be a conversation piece.
What is LINQ?
LINQ is a first class language extension that is available to any .NET language, and allows developers to treat their collections of objects and data as parts of an SQL-like query statement. There are some resources that exlain LINQ decently well that I’ve managed to find. I’ll go into more detail in future Silver Shorts about using LINQ to parse the above Bing Image search result XML into custom class data.
For now let us suffice to go over my LINQ query that tells you everything you need to know about Silver Shorts in less than 10 lines of code.
Step 1 – Identify the data sources
var blogPosts =
from exp in user.Experiences()
where exp.IsAwesome
select new BlogPost()
{
Title = exp.Concept,
Content = exp.Details,
CodeUrl = exp.GitUrl;
};
In this example we have a data source user.Experiences() which we’ll say is a collection of Experience objects. Here we’re going to run a query against this data source and filter out only the experiences in which exp.IsAwesome is true.
Step 2 – Projections of the User experience objects into BlogPost objects
var blogPosts =
from exp in user.Experiences()
where exp.IsAwesome
select new BlogPost()
{
Title = exp.Concept,
Content = exp.Details,
CodeUrl = exp.GitUrl;
};
As the query executes, it finds objects that match the criteria we just mentioned, and from there our select statement will take the data from the Experience objects result and reformat it into a BlogPost. For the purposes of our example, we’re assuming that we need to transform this data into a BlogPost object in order to be able to post it to a Wordpress Blog.
Step 3 – Execution and putting it all together
foreach( BlogPost bp in blogPosts )
{
WordPress.post(bp);
}
An important thing to know about LINQ queries is that they are lazily executed. That is to say, even though we have constructed our query statement above, it won’t actually do anything until we reference the data it is supposed to represent. Generally speaking foreach loops are used to execute queries, but it is really whenever you reference your query “data” that it will be executed.
So in this foreach loop, the query is executed and the results are iterated across as BlogPost objects named (bp).
Booty
That just about does it for the inaugural post for Project Silver Shorts. Next time we’ll go over the process of building the Image Search demo at the top of this post, including constructing a LINQ query to parse the XML results from the BING query, Asynchronous network access to perform the Query, and showing the results as thumbnail Images in a Grid. Until then, enjoy your spoils.
Silver Shorts : Get the Code on GitHub.com
No comments yet.
Leave a comment
No trackbacks yet.
