Yield to the power of yield

May 26, 2008

I must admit, I had never really become too comfortable with the C# ‘yield’ keyword until recently. I knew that it was introduced in C# 2.0 as a means of simplifying the creation of an enumerator. I also knew that the C# compiler interprets the code in a method that uses the yield keyword. A compiler-generated implementation of IEnumerator exists behind the scenes, which implements the logic to produce the enumerator you declared in C#. Beyond that vague understanding, I was not too familiar with it. It felt “odd” so I rarely used it.

In my ‘Simplifying the WPF TreeView by Using the ViewModel Pattern‘ article I have a demo program that lets the user search through the items in a TreeView. The search logic uses the yield keyword, as seen below:

(Click on the image to view the source code at full-size)

The article also has a demo program that lazy-loads each item’s children. That demo does not provide the ability to search. Shortly after publishing the article, two people asked how to have a lazy-loaded tree with search capabilities. Aside from the fact that performing a search that produces no matching items will force all of the items to be loaded; it is a reasonable question. I decided to implement a solution.

At the time, I misunderstood exactly how my search method used the ‘yield’ keyword. I was under the false assumption that it was executing the search logic to completion when the FindMatches method is called, and storing the results in a collection of some type. As we will see later, this is entirely untrue, but I, too, can be an idiot sometimes. :)

I took the load-on-demand sample and added in the ability to search the items. The UI looks like this:

Based on my misunderstanding of how a yield-based enumerator works, I thought it would have been bad to use it in this situation. Since I (incorrectly) thought that it walked down the entire data tree upon creation, performing a search with it would have forced the entire tree to be loaded into memory at once. So I thought that I had to create a custom search algorithm that would perform the search and only load as many items into memory as necessary to find the first matching item. After finding the first match, the next time I perform the search, it should only load as many items as necessary to find the next matching item. And so on, and so on…

I have plenty of experience building recursive algorithms that walk over trees, but I had never built one like this. Most recursive algorithms store their state on the callstack. Each time the recursive method is called, a new frame is pushed onto the callstack, and it keeps track of the local variable values for you. By ‘local variables’ I mean things like, the current item, the index of a for loop, the parent item to which we return after processing a sub-tree, etc.

That’s all well and good, until you need to create a recursive method that returns a value and, later on, needs to resume processing from where it left off last time it was invoked. This is exactly what I needed to build. I needed to create a recursive algorithm that stores its state in an external data structure, so that I can effectively save and load that state between executions. It was an exciting Sunday morning programming exercise, for sure!

The source code of the demo app is available at the bottom of this post. In it, you’ll find a TreeViewItemViewModelSearch.cs file. It contains all of the code involved with this implementation. That file contains a static class, TreeViewItemViewModelSearch, which contains just one public method:

The SearchResult class implements IEnumerable<TreeViewItemViewModel> and its GetEnumerator returns an instance of SearchEnumerator, which implements IEnumerator<TreeViewItemViewModel>. This search logic is invoked by the enumerator’s MoveNext method:

(Click on the image to view the source code at full size.)

The helper classes seen in that algorithm are listed below:

As it turns out, none of this is necessary at all! If you look in the TreeViewItemViewModel class, you will see how there are two implementations of the search functionality. One of them uses the very elaborate code we just saw, and the other, which works JUST AS WELL as my code, is a simple method with the ‘yield’ keyword. Click on the following image to see how both techniques are used:

As seen in the FindMatches_Yield method, all that I had to do was add in code that lazy-loads the child items before it searches them! The compiler-generated implementation of my search logic will be invoked every time the enumerator’s MoveNext is called, and it only searches for one item at a time. This is perfect for a load-on-demand scenario. If I had known about this earlier, I never would have bothered to write that custom search enumerator. But then again, it was a lot of fun and quite interesting to implement, so it’s all good!

Download the source code here: treeview_with_viewmodel_lazyload_and_search_demo Be sure to change the file extension from .DOC to .ZIP and then decompress the file.


IdentityMine Sweetens the Pot

May 7, 2008

The Podder Skinning Competition is really starting to heat up now!  IdentityMine is now giving away their awesome blendables product as a prize to the winners.  The list of prizes has grown quite a bit since the competition first began, and this latest addition definitely cranks it up a notch.  Thanks a lot, IdentityMine! 8)

I have already received a few e-mails and comments from people who are creating a Podder skin for the competition.  I can’t wait to see what they come up with.  Also, Karl told me that he is locked onto winning the steak dinner at Smith & Wollensky prize like a heat-seeking missile.  :)


Watch my presentation at the WPF Bootcamp 2008

March 29, 2008

A few months ago I flew out to Redmond so that I could speak at Microsoft’s WPF Bootcamp.  I gave a presentation about implementing the Model-View-Whatever pattern in WPF, which was received very well by the attendees.  After giving that presentation I solidified the material into my ‘Using MVC to Unit Test WPF Applications‘ article on CodeProject.  This seminal work is the foundation of the structural skinning architecture seen in Podder.

The entire WPF Bootcamp 2008 was videotaped and recently published online.  Karsten announced it a few days ago in this post.  All of the videos and associated downloads are hosted in a Silverlight application (of course), which you can view hereNOTE: That page comes up empty for me in FireFox, so be sure to open in it Internet Explorer.

You can find my presentation under Day 3.  It is called “Hello, Real World!” and runs for about one hour.  I highly suggest you check out some of the other presentations, too.  There were many interesting things discussed during the bootcamp.

In case you want to download the entire video file,which is quite large, you can grab them individually from this page.  My video is around 330MB so it might take a while to download it, but if you’re just dying to fill up that new external hard drive…  :)


Craig Shoemaker unveils Pixel8

March 4, 2008

Ever since I rejoined Infragistics, I have been in almost daily contact with Craig Shoemaker. We do not work in the same group, or even work on the same projects, but I just really enjoy chatting with him since he’s an all-around great guy. In case you are not already familiar with Polymorphic Podcast, Craig’s popular .NET podcast, I recommend you check it out sometime.

Today Craig announced his new podcast, Pixel8. This new podcast, produced by Infragistics, is all about user experience. I am very excited about this because I think user experience is a very important aspect of any successful user-facing application. You can learn more about Pixel8 via this brief introductory video:


Buying themes for WPF applications

February 19, 2008

The Web is full of comments where people generally voice the same issue with WPF. It seems that everyone and their grandmother thinks WPF is only useful for companies building apps with “differentiated user interfaces.” Ya know, Times Reader, Yahoo Messenger for Vista, etc. A common thread is that if you are building line-of-business (LOB) applications, WPF is not going to give you much over WinForms. While understandable, this opinion is simply wrong. Need proof? Check out the Lawson Smart Client app.

With that said, there is a point to take away from the general consensus. I totally agree that you are missing out on a lot of WPF’s potential if you do not have Visual Designers around to Blend up some fantastic user interfaces for you. Even if you have the budget to hire Visual Designers, it’s not exactly a simple task to find someone who has strong VD skills, as well as a firm background in software development. Those people are in high demand, and are in low supply (’low’ compared to, say, competent WPF developers…oh wait…nevermind). ;)

What I expect to see, in abundance, is third-party and open-source visual themes that can simply be plugged into any application. Development teams will use pre-canned visual designs. There’s gold in them hills. Once a development team can purchase/download a set of styles/templates/resources to turn their drab LOB apps into something like Lawson Smart Client, WPF will be the de facto choice for LOB projects. Of course, I assume by that time design-time support for WPF will be much better and supportive of RAD. Without that, all bets are off.

These types of pre-canned themes are already available to a certain extent. The Reuxables product seems interesting. I have not used it yet, but it seems on the right track and worth trying out. Infragistics offers Theme Packs, which you can use to restyle Infragistics controls. Products like these are the future of WPF and Silverlight development, considering that most dev teams neither have access to or can find Visual Designers.


WPF vs. ASP.NET - An Architect’s Perspective

December 20, 2007

Karl Shifflett recently published a fabulous blog post explaining why he decided to use WPF instead of ASP.NET for his company’s large software product.  It is very down to earth and unbiased.  He obviously put a lot of thought into this, so I highly recommend checking it out.  Here’s the link:

http://karlshifflett.wordpress.com/2007/12/20/reasons-for-choosing-wpf-over-aspnet-for-very-large-project/


The .NET world is now a better place

December 14, 2007

When I worked in the Infragistics WinForms development lab, I had the privilege of working with Andrew Smith.  If such a thing as a “guru” exists, Andrew is it.  His mastery of .NET programming is the best I have ever seen.  I don’t give compliments of that magnitude too often, but he deserves it.  Heck, as soon as he got involved with the Mole project, some of the really nasty bugs immediately disappeared and the performance improved drastically.  Abra-ca-dabra!

Fortunately, he has decided to share some of his programming thoughts with the rest of the world.  Yahoo!!  He recently started a blog here: http://agsmith.wordpress.com/

Thanks Andrew!


Meet my alter ego

December 13, 2007

This post is for my loyal readers out there.  In case you are looking for some extra blog posts by Yours Truly, you’re in luck.  Now that I work at Infragistics, I have a blog on their blog site as well.  My new blog is not devoted to WPF, in fact, I intend on posting all of my WPF content here.  The new blog will contain the type of posts that I never felt I should write about on a blog called “Josh Smith on WPF”.  

My new blog will be about all of the cool technologies that I research and use for my new job.  It should be a lot of fun, so why not stop on by once in a while to see what the non-WPF Josh is thinking about?  If you want to check it out, here are the links:

Blog: http://blogs.infragistics.com/blogs/joshs/

RSS 2.0: http://blogs.infragistics.com/blogs/joshs/rss.aspx

ATOM: http://blogs.infragistics.com/blogs/joshs/atom.aspx

See you there!


A WPF Contest Worth Entering

December 4, 2007

I am honored to announce that I am a judge in the ”Lab49 WPF in Finance Innovation Contest“.  The premise of the contest is awesome; they provide you with some data and you have to create the best way of visualizing it in WPF.   I almost wish I wasn’t a judge so that I could compete!

In addition to me, the other two judges are Rob Relyea and Charles Petzold.  We have the excellent task of reviewing all of the applications that are submitted and deciding which ones are the best.  I should point out, that if you attempt to contact one of the judges regarding your application then you are automatically disqualified.

The prizes are INCREDIBLE.  It makes the prizes in my WPF Challenge competition look like chicken feed.  You can check out all the goodness here.

What’s even more important than the prizes, however, is the prestige you will receive by winning.  I’m sure that the winners of this competition will be raised a notch or two in the eyes of employers at companies which need WPF devs.  Think about it, what do you have to lose?


I have returned to Infragistics

December 4, 2007

Once upon a time I was hard-core into Windows Forms.  I worked at Infragistics in their WinForms Development Lab, helping build and maintain their awesome controls and components.  After a while the urge to move to NYC became irresistable, so my girlfriend and I packed our bags and headed off to the big city.  Ever since then I never found a job that even came close to being as cool as the one I had at Infragistics.  Nor did I ever find a company where my co-workers were as hard-core into programming and cutting edge technologies as I am.

Fortunately for me, I was able to get a job at Infragistics again!  My girlfriend and I will continue living in NYC, but I’m a full-time employee in the Infragistics User Experience Group.  My official job title is “Guidisan” which I find rather odd, but it is fitting because my job is rather odd.  

Guidisan is supposed to mean the combination of a Guide and an Artisan, which accurately describes what I will be doing there.  I’m thrilled to announce that my job will entail helping developers move into the world of modern UI programming; by creating exemplars (something like a reference app), writing technical articles, creating demonstrations of how the Infragistics controls can be used in powerful and innovative ways, and generally staying in touch with the community by blogging and speaking at tech events.   My manager, Ambrose Little, said that the User Experience Group is like the SWAT team of the company.  So it looks like I should have a pretty diverse and interesting job.

They’re paying me to do what I love!  I knew all this damn blogging would pay off somehow… ;)

I’m glad to say that Infragistics has done nothing but grow since I left.  I was amazed at how many more people the company has now.  I was also pleasantly reminded of how nice and smart the people are there.  I’m thrilled to be back!!

On my first day some of the guys took me out to lunch.  During the ride over to the restaurant I realized that, including myself, I was in a vehicle containing five Microsoft MVPs!  How cool is that?!  Talk about a Geek Squad…