<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'><id>tag:blogger.com,1999:blog-9991642</id><updated>2008-08-25T03:51:43.117-07:00</updated><title type='text'>ILoggable</title><link rel='alternate' type='text/html' href='http://www.claassen.net/geek/blog/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default?start-index=26&amp;max-results=25'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default'/><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='index.xml'/><author><name>ether</name><uri>http://www.blogger.com/profile/16911611449176929088</uri><email>noreply@blogger.com</email></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>150</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-9991642.post-9173023483145492820</id><published>2008-07-13T19:08:00.000-07:00</published><updated>2008-07-13T19:18:35.693-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='braindead'/><category scheme='http://www.blogger.com/atom/ns#' term='appleTV'/><category scheme='http://www.blogger.com/atom/ns#' term='apple'/><title type='text'>OMFG AppleTV, seriously? Wipe yourself clean, why don't you?</title><content type='html'>Ok, here's a feature of AppleTV that can only have it's origin in some devils bargain with Content industry: Once you associate an iTunes instance with AppleTV for sync, you better not ever want to change it, because in the blink of an eye, you will staring a freshly wiped HD.

See, syncing means that whatever is on your PC/Mac is also on your AppleTV. So if you remove it from the PC, it disappears from your AppleTV. Ok, fine, a bit stupid a way to do content management on the AppleTV, but maybe i'll buy that it simplifies things, since all you have to do it keep things organized in AppleTV and all is good.

But here' the kicker, you ever change your AppleTV association to another iTunes, all is wiped of the AppleTV after one quick dialog. And no, oh, it's only hidden in case you re-associate because you realize that was not what you meant to do. No, you now get to re-synch 150GB back to your AppleTV. All for a second of misreading a dialog and clicking the wrong button. Wow! Really? That's great UI simplification. User makes mistake, gets to sit around for a couple of hours while AppleTV re-synch.

Any you cannot tell me that the brilliant minds at Apple came up with this scheme because it was just the most intuitive UI they could think of. No, this is all about the content industry being deathly afraid that someone might take their AppleTV to a friends house, copy all his content and then take it back home. As if this assinine sync behavior prevents something like that.

Grrr.. This is just stupid. 8 out of 450 items synced.</content><link rel='alternate' type='text/html' href='http://www.claassen.net/geek/blog/2008/07/omfg-appletv-seriously-wipe-yourself.html' title='OMFG AppleTV, seriously? Wipe yourself clean, why don&apos;t you?'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9991642&amp;postID=9173023483145492820' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.blogger.com/feedsindex.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/9173023483145492820'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/9173023483145492820'/><author><name>ether</name><uri>http://www.blogger.com/profile/16911611449176929088</uri><email>noreply@blogger.com</email></author></entry><entry><id>tag:blogger.com,1999:blog-9991642.post-8874106704392264964</id><published>2008-07-02T13:56:00.001-07:00</published><updated>2008-07-02T14:03:09.925-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='wpf'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><title type='text'>The riddle of the disappearing WPF databinding</title><content type='html'>I'm currently on a custom control that has a bunch of panels slaved to each other via databinding. And I ran into a bug where moving an element around would suddenly break the sync with the other panels. Nothing in the Output about data-binding failing, so i inspected it with &lt;a href="http://www.blois.us/Snoop/"&gt;Snoop&lt;/a&gt; to see that my data-binding had just up and gone away. So I tracked down the suspect code and monitored the data source with &lt;a href="http://joshsmithonwpf.wordpress.com/mole/"&gt;Mole&lt;/a&gt; as i stepped through and saw that the data-binding went away right after I set the dependency property.

&lt;p&gt;Yeah, not really magic at all. A quick look at the Xaml showed that I had left this particular binding as default (i.e. OneWay). And if you have a OneWay binding and manually set the dependency property, the binding gets overwritten and goes away. Switching the binding to &lt;b&gt;Mode=TwoWay&lt;/b&gt; restored my sync.</content><link rel='alternate' type='text/html' href='http://www.claassen.net/geek/blog/2008/07/riddle-of-disappearing-wpf-databinding.html' title='The riddle of the disappearing WPF databinding'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9991642&amp;postID=8874106704392264964' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.blogger.com/feedsindex.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/8874106704392264964'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/8874106704392264964'/><author><name>ether</name><uri>http://www.blogger.com/profile/16911611449176929088</uri><email>noreply@blogger.com</email></author></entry><entry><id>tag:blogger.com,1999:blog-9991642.post-7357609757117296002</id><published>2008-07-01T22:28:00.001-07:00</published><updated>2008-07-01T22:39:47.977-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='wpf'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><title type='text'>WPF Custom Panel layout and Dependency Properties</title><content type='html'>Just a quick note I learned the hard way when creating custom panels in WPF: &lt;b&gt;If your panel has dependency properties or attached properties on its children that affect measure and/or arrange, calling &lt;i&gt;InvalidateMeasure&lt;/i&gt; or &lt;i&gt;InvalidateArrange&lt;/i&gt; won't necessarily do the trick.&lt;/b&gt; For that matter, calling these methods isn't even necessary. Instead use the &lt;b&gt;FrameworkPropertyMetadata&lt;/b&gt; Metadata class to set appropriate &lt;b&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.windows.frameworkpropertymetadataoptions.aspx"&gt;FrameworkPropertyMetadataOptions&lt;/a&gt;&lt;/b&gt;.

&lt;p&gt;So if you have a Dependency Property on your panel that affects the measure or arrange of its children, make sure it has &lt;b&gt;FrameworkPropertyMetadataOptions.AffectsMeasure&lt;/b&gt; and/or &lt;b&gt;FrameworkPropertyMetadataOptions.AffectsArrange&lt;/b&gt; set.

&lt;p&gt;Similarily, if your panel has an attached property for its children, whose modification affects how that child is laid out, set &lt;b&gt;FrameworkPropertyMetadataOptions.AffectsParentMeasure&lt;/b&gt; and/or &lt;b&gt;FrameworkPropertyMetadataOptions.AffectsParentArrange&lt;/b&gt;.

&lt;p&gt;Now layout and arrange are properly invalidated and called for you.</content><link rel='alternate' type='text/html' href='http://www.claassen.net/geek/blog/2008/07/wpf-custom-panel-layout-and-dependency.html' title='WPF Custom Panel layout and Dependency Properties'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9991642&amp;postID=7357609757117296002' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.blogger.com/feedsindex.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/7357609757117296002'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/7357609757117296002'/><author><name>ether</name><uri>http://www.blogger.com/profile/16911611449176929088</uri><email>noreply@blogger.com</email></author></entry><entry><id>tag:blogger.com,1999:blog-9991642.post-7817596628256634821</id><published>2008-03-19T09:13:00.000-07:00</published><updated>2008-03-19T09:18:15.864-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='asp.net'/><category scheme='http://www.blogger.com/atom/ns#' term='iis6'/><category scheme='http://www.blogger.com/atom/ns#' term='xp64'/><title type='text'>XP 64, IIS 6 and ASP.NET</title><content type='html'>My current dev machine is running XP 64, which is a first for me. In the default setup IIS was not installed, so I went through Add/Remove Programs and installed it, which gave me IIS 6. This in turn has several tabs for ASP.NET, but try as you might none of these are what actually turns on ASP.NET and you just end up with mysterious 404s on a application enabled directory that's configured just like the working ASP.NET on your other machine.

&lt;p&gt;Well, it turns out that ASP.NET (even though it shows up in the Properties tabs) is not installed by default and if you go to Web Service Extensions you won't see it there. So next, track down aspnet_regiis which is in the Framework directory and run

&lt;pre&gt;aspnet_regiis -i&lt;/pre&gt;

Then go back to IIS Manager -&gt; Web Service Extensions where ASP.NET should now be an available extension. Enable it and finally ASP.NET works.</content><link rel='alternate' type='text/html' href='http://www.claassen.net/geek/blog/2008/03/xp-64-iis-6-and-aspnet.html' title='XP 64, IIS 6 and ASP.NET'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9991642&amp;postID=7817596628256634821' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.blogger.com/feedsindex.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/7817596628256634821'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/7817596628256634821'/><author><name>ether</name><uri>http://www.blogger.com/profile/16911611449176929088</uri><email>noreply@blogger.com</email></author></entry><entry><id>tag:blogger.com,1999:blog-9991642.post-1667043524583665771</id><published>2008-03-11T23:12:00.001-07:00</published><updated>2008-03-11T23:57:35.416-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='silverlight'/><category scheme='http://www.blogger.com/atom/ns#' term='.net'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><title type='text'>Sharing code between Silverlight and .NET</title><content type='html'>Currently, a Silverlight Class Library cannot be loaded by the server side project and vice versa. This despite there being very close parity in the BCL on either side. Now, I agree that sharing actual business logic between client and server is a bit of an edge case, but when it comes to data interchange, having a single codebase for you DTOs would be very useful.

&lt;p&gt;Luckily, with a bit of trickery, this can be accomplished, although it would be nice if Visual Studio could just do this for you. I've done this on two different projects, once creating DTOs that were then serialized using the JSON DataContract serializer and other other time using the normal XmlSerializer.

&lt;p&gt;The basic trick is this: Two separate projects can point at the same source code files (and even the integrated source control providers will play along with this game). However, playing by Visual studio rules you can't just create two projects in the same directory because the wizards treats &lt;b&gt;ProjectName == DirectoryName&lt;/b&gt;. Here's how you get around this:

&lt;ol&gt;

&lt;li&gt;Create a new server side Class Library, say &lt;b&gt;Server.Dto&lt;/b&gt;.
&lt;li&gt;Create a new Silverlight Class Library, say &lt;b&gt;Silverlight.Dto&lt;/b&gt;.
&lt;li&gt;Clean out that default Class.cs and update both to have the same default namespace &lt;b&gt;Foo.Dto&lt;/b&gt;. Your solution should look something like this:
&lt;br&gt;&lt;img src="http://www.claassen.net/images/mutantassembly1.jpg"/&gt;
&lt;li&gt;Now, right-click on &lt;b&gt;Silverlight.Dto&lt;/b&gt; and choose &lt;b&gt;Remove&lt;/b&gt;
&lt;li&gt;Close Visual Studio
&lt;li&gt;Rename the &lt;b&gt;Silverlight.Dto/Properties&lt;/b&gt; directory to &lt;b&gt;Silverlight.Dto/Silverlight.Properties&lt;/b&gt;
&lt;li&gt;Copy &lt;b&gt;Silverlight.Properties&lt;/b&gt; and &lt;b&gt;Silverlight.Dto.csproj&lt;/b&gt; to the &lt;b&gt;Server.Dto&lt;/b&gt; directory
&lt;li&gt;Now, &lt;i&gt;and this is the tricky bit&lt;/i&gt;, open &lt;b&gt;Silverlight.Dto.csproj&lt;/b&gt; in some text-editor (notepad works, but &lt;b&gt;don't&lt;/b&gt; try to use Visual studio for this) and change the line

&lt;pre&gt;&amp;lt;Compile Include="Properties\AssemblyInfo.cs" /&amp;gt;&lt;/pre&gt;

to

&lt;pre&gt;&amp;lt;Compile Include="Silverlight.Properties\AssemblyInfo.cs" /&amp;gt;&lt;/pre&gt;

&lt;li&gt; Re-open your solution, right-click on the Solution in the Solution Explorer and choose &lt;b&gt;Add -&amp;gt; Existing Project...&lt;/b&gt;, browse to &lt;b&gt;Server.Dto&lt;/b&gt; and select &lt;b&gt;Silverlight.Dto.csproj&lt;/b&gt;
&lt;li&gt; Add your first shared class &lt;b&gt;FooDto.cs&lt;/b&gt; to Server.Dto (if you add it on the Silverlight side, you need to clean up a bunch of &lt;i&gt;using&lt;/i&gt; statements).
&lt;li&gt; Select &lt;b&gt;Silverlight.Dto&lt;/b&gt; and click the &lt;b&gt;Show All Files&lt;/b&gt; button at the top of the panel. You should see &lt;b&gt;FooDto.cs&lt;/b&gt; in &lt;b&gt;Silverlight.Dto&lt;/b&gt; now
&lt;li&gt; Right-click &lt;b&gt;FooDto.cs&lt;/b&gt; and choose &lt;b&gt;Include in Project&lt;/b&gt;, like this:
&lt;br&gt;&lt;img src="http://www.claassen.net/images/mutantassembly2.jpg"/&gt;
&lt;li&gt; &lt;i&gt;Voila,&lt;/i&gt; you now have two assemblies referencing the same code so that you can use the same code on both client and server sides. Just repeat the last couple of steps for every new class you want to share.
&lt;/ol&gt;

&lt;p&gt;I know you could use a Web Service from within Silverlight and would automatically generate you the proxy on the client side. And I'd recommend that if your payload is dynamic. However, if your payload is generated occasionally by a server side program or periodic service, this methods lets you create Dto's, serialized in your favorite manner that can be consumed as static files with &lt;b&gt;WebClient&lt;/b&gt;.</content><link rel='alternate' type='text/html' href='http://www.claassen.net/geek/blog/2008/03/sharing-code-between-silverlight-and.html' title='Sharing code between Silverlight and .NET'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9991642&amp;postID=1667043524583665771' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.blogger.com/feedsindex.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/1667043524583665771'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/1667043524583665771'/><author><name>ether</name><uri>http://www.blogger.com/profile/16911611449176929088</uri><email>noreply@blogger.com</email></author></entry><entry><id>tag:blogger.com,1999:blog-9991642.post-5244199178167071559</id><published>2008-03-08T11:42:00.000-08:00</published><updated>2008-03-08T12:12:23.791-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='epoch'/><category scheme='http://www.blogger.com/atom/ns#' term='Extension method'/><category scheme='http://www.blogger.com/atom/ns#' term='unix time'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><title type='text'>Epoch DateTime conversion Extension Methods</title><content type='html'>Interop with unix often requires dealing with Epoch or &lt;a href="http://en.wikipedia.org/wiki/Unix_time"&gt;Unix time&lt;/a&gt;, or the number of seconds since January 1st, 1970 UTC. And if you throw java in the mix, then epoch becomes milliseconds, since they define &lt;b&gt;System.currentTimeMillis()&lt;/b&gt; as the number &lt;i&gt;milli&lt;/i&gt;seconds since the Epoch. Anyway, i figured, this was the perfect use of Extension methods. Well, almost... There's still the issue that getting a DateTime object from seconds requires a static method added to DateTime, which extension methods do not currently support. This means that instead of

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
DateTime utcDateTime = DateTime.FromEpochSeconds(seconds)&lt;/pre&gt;
&lt;/div&gt;

we have to be content with

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
DateTime utcDateTime = DateTimeEx.FromEpochSeconds(seconds)&lt;/pre&gt;
&lt;/div&gt;

Now i also added extensions on long and int but, really, that falls into the realm of stupid extension method tricks. I left them in there, only because they are part of DateTimeEx, therefore will only be available if the appropriate namespace is included and so is at least tangentially relevant in the current scope. Well, that's my rationalization, at least. With this extra extension method you now can do

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
DateTime utcDateTime = 1205003848.DateTimeFromEpochSeconds();&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The one thing to be aware of with all these helpers is that it always deals with UTC time, i.e. the DateTime that you convert to epoch time needs to have a &lt;b&gt;DateTimeKind&lt;/b&gt; that is not &lt;i&gt;Unspecified&lt;/i&gt;. Conversely, the DateTime you get back is UTC and if you want to deal with it with localtime, you need to call &lt;b&gt;ToLocalTime()&lt;/b&gt; on it first.

&lt;p&gt;Anyway, here's the class: 

&lt;p&gt;
&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System;

&lt;span class="kwrd"&gt;namespace&lt;/span&gt; Droog.DateTime
{
  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; DateTimeEx
  {
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; DateTime FromEpochMilliseconds(&lt;span class="kwrd"&gt;long&lt;/span&gt; milliseconds)
    {
      &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; DateTime(1970, 1, 1,0,0,0,DateTimeKind.Utc).AddMilliseconds(milliseconds);
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; DateTime FromEpochSeconds(&lt;span class="kwrd"&gt;int&lt;/span&gt; seconds)
    {
      &lt;span class="kwrd"&gt;return&lt;/span&gt; FromEpochMilliseconds((&lt;span class="kwrd"&gt;long&lt;/span&gt;)seconds * 1000);
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; DateTime DateTimeFromEpochMilliSeconds(&lt;span class="kwrd"&gt;this&lt;/span&gt; &lt;span class="kwrd"&gt;long&lt;/span&gt; milliseconds)
    {
      &lt;span class="kwrd"&gt;return&lt;/span&gt; FromEpochMilliseconds(milliseconds);
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; DateTime DateTimeFromEpochSeconds(&lt;span class="kwrd"&gt;this&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; seconds)
    {
      &lt;span class="kwrd"&gt;return&lt;/span&gt; FromEpochSeconds(seconds);
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; ToEpochSeconds(&lt;span class="kwrd"&gt;this&lt;/span&gt; DateTime dt)
    {
      &lt;span class="kwrd"&gt;return&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt;)(ToEpochMilliseconds(dt)/1000);
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;long&lt;/span&gt; ToEpochMilliseconds(&lt;span class="kwrd"&gt;this&lt;/span&gt; DateTime dt)
    {
      &lt;span class="kwrd"&gt;return&lt;/span&gt; (&lt;span class="kwrd"&gt;long&lt;/span&gt;)(dt.ToUniversalTime() - &lt;span class="kwrd"&gt;new&lt;/span&gt; DateTime(1970, 1, 1)).TotalMilliseconds;
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;
&lt;div id="csharpcode"&gt;
&lt;/div&gt;
&lt;div id="csharpcode"&gt;
&lt;/div&gt;
&lt;div id="csharpcode"&gt;
&lt;/div&gt;
&lt;div id="csharpcode"&gt;
&lt;/div&gt;</content><link rel='alternate' type='text/html' href='http://www.claassen.net/geek/blog/2008/03/epoch-datetime-conversion-extension.html' title='Epoch DateTime conversion Extension Methods'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9991642&amp;postID=5244199178167071559' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.blogger.com/feedsindex.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/5244199178167071559'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/5244199178167071559'/><author><name>ether</name><uri>http://www.blogger.com/profile/16911611449176929088</uri><email>noreply@blogger.com</email></author></entry><entry><id>tag:blogger.com,1999:blog-9991642.post-3376130681961991649</id><published>2008-03-04T22:05:00.000-08:00</published><updated>2008-03-04T22:28:48.052-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='synergy'/><title type='text'>Synergy</title><content type='html'>I used to run x2vnc and win2vnc back in the MP3 days to let me control my Windows and linux boxen. Later I used the same setup with my old MacBook 15" and a Linux box. The other day, my friend n8 &lt;a href="http://www.mybrainhurts.com/blog/2008/02/my-synergy-setup.html"&gt;posted about his Synergy setup&lt;/a&gt;, which came perfectly timed. I just started a new gig at &lt;a href="http://www.bunkspeed.com/"&gt;Bunkspeed&lt;/a&gt; and I'm using a dedicated desktop for dev instead of my MacBook Pro, but i don't want my Mac to be wasted. So i set up &lt;a href="http://synergy2.sourceforge.net/"&gt;Synergy&lt;/a&gt; on both my home and work desktops and have the Mac on a stand running two Synergy clients (only one of which ever finds a server to connect to. This setup rules!

&lt;p&gt;&lt;b&gt;Update:&lt;/b&gt; Don't know if this is something i'll find a way around, but apparently logging on to the VPN killed the connectivity between my desktop and mac :(

&lt;p&gt;&lt;b&gt;Update 2:&lt;/b&gt; Ok, as simple as going into the Advanced config and telling it what local IP to listen for connections on. All good again</content><link rel='alternate' type='text/html' href='http://www.claassen.net/geek/blog/2008/03/synergy.html' title='Synergy'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9991642&amp;postID=3376130681961991649' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.blogger.com/feedsindex.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/3376130681961991649'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/3376130681961991649'/><author><name>ether</name><uri>http://www.blogger.com/profile/16911611449176929088</uri><email>noreply@blogger.com</email></author></entry><entry><id>tag:blogger.com,1999:blog-9991642.post-5639112727615663715</id><published>2008-03-03T13:30:00.000-08:00</published><updated>2008-03-03T13:32:54.609-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='wpf'/><category scheme='http://www.blogger.com/atom/ns#' term='.net'/><title type='text'>VS2k5 &amp; WPF</title><content type='html'>After having tried a bunch of different iterations to get the WPF tools installed for VS2k5, here's what finally worked"

&lt;ul&gt;
&lt;li&gt;Install .NET 3.0
&lt;li&gt;Install Visual Studio Tools for WCF/WPF
&lt;li&gt;And only then install .NET 3.0 SP1
&lt;/ul&gt;

Talk about annoying dependencies.</content><link rel='alternate' type='text/html' href='http://www.claassen.net/geek/blog/2008/03/vs2k5-wpf.html' title='VS2k5 &amp; WPF'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9991642&amp;postID=5639112727615663715' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.blogger.com/feedsindex.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/5639112727615663715'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/5639112727615663715'/><author><name>ether</name><uri>http://www.blogger.com/profile/16911611449176929088</uri><email>noreply@blogger.com</email></author></entry><entry><id>tag:blogger.com,1999:blog-9991642.post-8162955168915056533</id><published>2008-02-26T16:17:00.000-08:00</published><updated>2008-02-26T16:20:14.241-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='mono'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><title type='text'>Use .NET/Mono, it's the environmentally friendly choice</title><content type='html'>I just read what's got to be my favorite &lt;a href="http://tirania.org/blog/archive/2008/Feb-26.html"&gt;Miguel De Icaza quote&lt;/a&gt; to date:

&lt;blockquote&gt;&lt;i&gt;In a world that is increasingly green, it is a waste of perfectly healthy computer cycles to interpret your code when you can use an optimizing JIT compiler to run your code.&lt;/i&gt;&lt;/blockquote&gt;

I'm going to presume that's with tongue firmly implanted in cheek.</content><link rel='alternate' type='text/html' href='http://www.claassen.net/geek/blog/2008/02/use-netmono-its-environmentally.html' title='Use .NET/Mono, it&apos;s the environmentally friendly choice'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9991642&amp;postID=8162955168915056533' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.blogger.com/feedsindex.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/8162955168915056533'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/8162955168915056533'/><author><name>ether</name><uri>http://www.blogger.com/profile/16911611449176929088</uri><email>noreply@blogger.com</email></author></entry><entry><id>tag:blogger.com,1999:blog-9991642.post-4853458234567358959</id><published>2008-02-18T14:43:00.000-08:00</published><updated>2008-02-18T15:21:57.574-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET 3.5'/><title type='text'>Addicted to .Net 3.5</title><content type='html'>Outside of LINQ, i thought that 3.5 was a lot of cool but not vital syntactic sugar. This weekend marks the first time since November that I fired up VS.NET 2k5 to build an app targeting the 2.0 framework and I was amazed how much I'd already come to rely on that sugar. Now this might be seen as an invalidation of my preference of explicit, verbose syntax versus the terse syntax of many scripting languages. I'd like to point out that terseness and expressiveness in sytnax are two separate things. Syntactic sugar that let's me express my action in more concise code that easily conveys the meaning is not the same thing as using a terse vocabulary to keep the typing down and requiring memorization of abbreviated keywords to understand the code. Anyway, here are the parts of 3.5 I've missed more than once this weekend.

&lt;h2&gt;Extension Methods on IEnumerable&lt;/h2&gt;

The plethora of extension methods on all things IEnumerable is largely due to LINQ, but the &lt;i&gt;To*&lt;/i&gt; methods have become just a basic part of my vocubulary. Taking the Values of a Dictionary into an Array of the same type now is seems just painfully verbose without the ToArray() method. Compare

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
FileInfo[] f = &lt;span class="kwrd"&gt;new&lt;/span&gt; FileInfo[files.Count];
files.Values.CopyTo(f,0);
&lt;span class="kwrd"&gt;return&lt;/span&gt; f;
&lt;/pre&gt;
&lt;/div&gt;

with

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
&lt;span class="kwrd"&gt;return&lt;/span&gt; files.Values.ToArray();
&lt;/pre&gt;&lt;/div&gt;

&lt;h2&gt;Object/List initializer syntax&lt;/h2&gt;

Now this I really thought of as frivolous. However, using objects that use DTOs as their initializer/storage, initialization does become rather awkward without loops or  long constructors:

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
      FileInfoData[] remoteFileData = &lt;span class="kwrd"&gt;new&lt;/span&gt; FileInfoData[]
      {
        &lt;span class="kwrd"&gt;new&lt;/span&gt; FileInfoData(),
        &lt;span class="kwrd"&gt;new&lt;/span&gt; FileInfoData(),
        &lt;span class="kwrd"&gt;new&lt;/span&gt; FileInfoData()
      };
      remoteFileData[0].name = &lt;span class="str"&gt;"test1.mpg"&lt;/span&gt;;
      remoteFileData[1].name = &lt;span class="str"&gt;"test2.mpg"&lt;/span&gt;;
      remoteFileData[2].name = &lt;span class="str"&gt;"test10.mpg"&lt;/span&gt;;
      FileInfo[] remoteFiles = &lt;span class="kwrd"&gt;new&lt;/span&gt; FileInfo[]
      {
        &lt;span class="kwrd"&gt;new&lt;/span&gt; FileInfo(remoteFileData[0]),
        &lt;span class="kwrd"&gt;new&lt;/span&gt; FileInfo(remoteFileData[1]),
        &lt;span class="kwrd"&gt;new&lt;/span&gt; FileInfo(remoteFileData[2]),
      };
&lt;/pre&gt;&lt;/div&gt;

versus

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
      FileInfo[] remoteFiles = &lt;span class="kwrd"&gt;new&lt;/span&gt; FileInfo[]
      {
        &lt;span class="kwrd"&gt;new&lt;/span&gt; FileInfo(&lt;span class="kwrd"&gt;new&lt;/span&gt; FileInfoData()
        {
          Name = &lt;span class="str"&gt;"test1.mpg"&lt;/span&gt;
        }),
        &lt;span class="kwrd"&gt;new&lt;/span&gt; FileInfo(&lt;span class="kwrd"&gt;new&lt;/span&gt; FileInfoData()
        {
          Name = &lt;span class="str"&gt;"test2.mpg"&lt;/span&gt;
        }),
        &lt;span class="kwrd"&gt;new&lt;/span&gt; FileInfo(&lt;span class="kwrd"&gt;new&lt;/span&gt; FileInfoData()
        {
          Name = &lt;span class="str"&gt;"test10.mpg"&lt;/span&gt;
        }),
      };
&lt;/pre&gt;&lt;/div&gt;

&lt;h2&gt;Anonymous delegates are a pain and hard to read&lt;/h2&gt;

I needed to pass in a delegate to a function as a factory callback. Perfect scenario for a nice concise lambda. But I was in 2.0, so that meant defining a delegate and anonymous delegate syntax resulting ing
&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;delegate&lt;/span&gt; ILocalFileSystemManager LocalCreateDelegate(&lt;span class="kwrd"&gt;string&lt;/span&gt; localPath, &lt;span class="kwrd"&gt;string&lt;/span&gt; extension);

  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; FileSystemManagerFactory
  {
    &lt;span class="kwrd"&gt;public&lt;/span&gt; FileSystemManagerFactory( LocalCreateDelegate localFactory )
    {
      &lt;span class="kwrd"&gt;this&lt;/span&gt;.localFactory = localFactory;
    }
  }

FileSystemManagerFactory factory = &lt;span class="kwrd"&gt;new&lt;/span&gt; FileSystemManagerFactory(
  &lt;span class="kwrd"&gt;delegate&lt;/span&gt;(&lt;span class="kwrd"&gt;string&lt;/span&gt; localPath, &lt;span class="kwrd"&gt;string&lt;/span&gt; extension)
  {
    &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; MockLocalFileSystemManager(localPath, extension);
  });&lt;/pre&gt;&lt;/div&gt;

instead of 
&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
  &lt;span class="kwrd"&gt;public&lt;/span&gt; FileSystemManagerFactory( Func&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;,&lt;span class="kwrd"&gt;string&lt;/span&gt;,ILocalFileSystemManager&amp;gt; localFactory )
  {
    &lt;span class="kwrd"&gt;this&lt;/span&gt;.localFactory = localFactory;
  }

FileSystemManagerFactory factory = &lt;span class="kwrd"&gt;new&lt;/span&gt; FileSystemManagerFactory(
  (localPath, extension)
  =&amp;gt;
  &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; MockLocalFileSystemManager(localPath, extension);
  );&lt;/pre&gt;&lt;/div&gt;

&lt;h2&gt;Automatic Properties&lt;/h2&gt;
If there is one feature of C# (well and java as well) that is the most code generated, it's getters and setters. I've never liked how code generation tools created those for me, since i liked having my private members in one place and Properties in another. So i've been typing them out for years. But with C# 3.0, we got automatic properties. The two patterns, read/write properties and read-only properties are oft repeated like this
&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
&lt;span class="kwrd"&gt;string&lt;/span&gt; readwriteMember;
&lt;span class="kwrd"&gt;string&lt;/span&gt; readonlyMember;

&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; ReadWrite
{
  get { &lt;span class="kwrd"&gt;return&lt;/span&gt; readwriteMember; }
  set { readwriteMember = &lt;span class="kwrd"&gt;value&lt;/span&gt;; }
}

&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; ReadOnly { get { &lt;span class="kwrd"&gt;return&lt;/span&gt; readonlyMember; }

&lt;/pre&gt;&lt;/div&gt;

Not a ton of code, but certainly takes more time to write than

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; ReadWrite { get; set; }

&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; ReadOnly { get; &lt;span class="kwrd"&gt;private&lt;/span&gt; set; }
&lt;/pre&gt;&lt;/div&gt;

Ho hum.. I'm in 2.0, so i'll have to deal, but I certainly hope that 3.5 has a fast pick-up rate on the client (on the server, I can still control my environment).</content><link rel='alternate' type='text/html' href='http://www.claassen.net/geek/blog/2008/02/addicted-to-net-35.html' title='Addicted to .Net 3.5'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9991642&amp;postID=4853458234567358959' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.blogger.com/feedsindex.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/4853458234567358959'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/4853458234567358959'/><author><name>ether</name><uri>http://www.blogger.com/profile/16911611449176929088</uri><email>noreply@blogger.com</email></author></entry><entry><id>tag:blogger.com,1999:blog-9991642.post-4736683988001109373</id><published>2008-02-01T10:05:00.000-08:00</published><updated>2008-02-02T11:47:47.203-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='lambda'/><category scheme='http://www.blogger.com/atom/ns#' term='linq'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><title type='text'>LINQ: Immutability vs. Deferred execution</title><content type='html'>The last couple of nights I've been playing with some &lt;b&gt;Linq to Sql&lt;/b&gt; and a whole lot of &lt;b&gt;Linq to Objects&lt;/b&gt; and I have to say where coming up with complex Regular Expressions used to be one of my favorite puzzles, coming up with complex projections and transformations through Linq is quickly taking its place. Simple Linq is well documented, but when it comes to aggregation, it's a lot sparser. I expect to write more of that up once I feel more comfortable with the syntax.

&lt;p&gt;In the meantime, I wanted to write up some non-obvious observation about deferred execution with Linq. Considering the gotchas with lambdas, it's easy to extend the lessons learned to linq, since it is after all deferred execution. But what's different with Linq is that, while execution is deferred, the expression tree built via a query is also immutable. I came across this trying to do some simple query re-use.

&lt;p&gt;Let's start with a simple DTO:

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Order
{
  &lt;span class="kwrd"&gt;public&lt;/span&gt; Order(&lt;span class="kwrd"&gt;int&lt;/span&gt; id, &lt;span class="kwrd"&gt;int&lt;/span&gt; val, &lt;span class="kwrd"&gt;bool&lt;/span&gt; buyOrder)
  {
    Id = id;
    Value = val;
    IsBuyOrder = buyOrder;
  }
  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Id { get; set; }
  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Value { get; set; }
  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; IsBuyOrder { get; set; }
}
&lt;/pre&gt;
&lt;/div&gt;

And a set of this data:

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
Order[] orders = &lt;span class="kwrd"&gt;new&lt;/span&gt; Order[]
{
  &lt;span class="kwrd"&gt;new&lt;/span&gt; Order(1,2,&lt;span class="kwrd"&gt;true&lt;/span&gt;),
  &lt;span class="kwrd"&gt;new&lt;/span&gt; Order(2,2,&lt;span class="kwrd"&gt;false&lt;/span&gt;),
  &lt;span class="kwrd"&gt;new&lt;/span&gt; Order(3,4,&lt;span class="kwrd"&gt;true&lt;/span&gt;),
  &lt;span class="kwrd"&gt;new&lt;/span&gt; Order(4,4,&lt;span class="kwrd"&gt;false&lt;/span&gt;),
  &lt;span class="kwrd"&gt;new&lt;/span&gt; Order(5,6,&lt;span class="kwrd"&gt;true&lt;/span&gt;),
  &lt;span class="kwrd"&gt;new&lt;/span&gt; Order(6,6,&lt;span class="kwrd"&gt;false&lt;/span&gt;),
};
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Let's split those into buy and sell orders:

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
var buyOrders = from order &lt;span class="kwrd"&gt;in&lt;/span&gt; orders
          &lt;span class="kwrd"&gt;where&lt;/span&gt; order.IsBuyOrder
          select order;

var sellOrders = from order &lt;span class="kwrd"&gt;in&lt;/span&gt; orders
                 &lt;span class="kwrd"&gt;where&lt;/span&gt; !order.IsBuyOrder
                 select order;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If we want to find the buy and the sell order with a value of 2, you'd think we could write one query and re-use it for both of those queries. Since both queries results in &lt;b&gt;IEnumerable&amp;lt;Order&amp;gt;&lt;/b&gt;, how about we define a query source and assign the value of either above query. 

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
IEnumerable&amp;lt;Order&amp;gt; orders2 = &lt;span class="kwrd"&gt;null&lt;/span&gt;;

var orderAtTwo = from order &lt;span class="kwrd"&gt;in&lt;/span&gt; orders2
                 &lt;span class="kwrd"&gt;where&lt;/span&gt; order.Value == 2
                 select order;

orders2 = buyOrders;
&lt;span class="kwrd"&gt;int&lt;/span&gt; buyOrderId = orderAtTwo.First().Id;

orders2 = sellOrders;
&lt;span class="kwrd"&gt;int&lt;/span&gt; sellOrderId = orderAtTwo.First().Id;

Console.WriteLine(&lt;span class="str"&gt;"buy Id: {0}, sell Id: {1}"&lt;/span&gt;, buyOrderId, sellOrderId);
&lt;/pre&gt;&lt;/div&gt;

Since the query is deferred until we call &lt;b&gt;.First()&lt;/b&gt; on it, that seems like a reasonable syntax. Except this will result in an &lt;b&gt;System.ArgumentNullException&lt;/b&gt; because our query grabbed a reference to &lt;i&gt;orders2&lt;/i&gt; at query definition, even though the query won't be executed until later. Giving &lt;i&gt;orders2&lt;/i&gt; a new value does not change the original reference in the immutable expression tree.

&lt;p&gt;A way around this is to replace the actual contents of &lt;i&gt;orders2&lt;/i&gt;. However, for us to do that, we have to turn it into the query source into a collection first.

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
orders2.Clear();
orders2.AddRange(buyOrders);
&lt;span class="kwrd"&gt;int&lt;/span&gt; buyOrderId = orderAtTwo.First().Id;

orders2.Clear();
orders2.AddRange(sellOrders);
&lt;span class="kwrd"&gt;int&lt;/span&gt; sellOrderId = orderAtTwo.First().Id;

Console.WriteLine(&lt;span class="str"&gt;"buy Id: {0}, sell Id: {1}"&lt;/span&gt;, buyOrderId, sellOrderId);
&lt;/pre&gt;&lt;/div&gt;

This gives us the expected
&lt;div id="csharpcode"&gt;&lt;pre class="csharpcode"&gt;
buy Id: 1, sell Id: 2
&lt;/pre&gt;&lt;/div&gt;

Let's put aside the awkwardness of clearing out a list and stuffing data back in, this code has another unfortunate sideeffect. &lt;b&gt;.AddRange()&lt;/b&gt; actually executes the query passed to it, so we execute our buy and sell queries to populate &lt;i&gt;orders2&lt;/i&gt; and then execute &lt;b&gt;orderAtTwo&lt;/b&gt; twice against those collections.
The beauty of linq is that if you create a query from a query, your not running multiple queries, but building a more complex query to be executed. So, what we really want is query "re-use" that results in single expression trees at execution time.

&lt;p&gt;To achieve this, we need to move the shared query into a separate method such as:

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
&lt;span class="kwrd"&gt;private&lt;/span&gt; IEnumerable&amp;lt;Order&amp;gt; GetTwo(IEnumerable&amp;lt;Order&amp;gt; source)
{
  &lt;span class="kwrd"&gt;return&lt;/span&gt; from order &lt;span class="kwrd"&gt;in&lt;/span&gt; source
         &lt;span class="kwrd"&gt;where&lt;/span&gt; order.Value == 2
         select order;
}
&lt;/pre&gt;&lt;/div&gt;

and the code becomes:

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
&lt;span class="kwrd"&gt;int&lt;/span&gt; buyOrderId = GetTwo(buyOrders).First().Id;
&lt;span class="kwrd"&gt;int&lt;/span&gt; sellOrderId = GetTwo(sellOrders).First().Id;

Console.WriteLine(&lt;span class="str"&gt;"buy Id: {0}, sell Id: {1}"&lt;/span&gt;, buyOrderId, sellOrderId);
&lt;/pre&gt;&lt;/div&gt;

This gives the same output as above, and we're only running two queries, each against the original collection. The method call means that we don't get to re-use an expression tree, since it builds a new one, combining the expression tree passed to it with the one it builds itself.</content><link rel='alternate' type='text/html' href='http://www.claassen.net/geek/blog/2008/02/linq-immutability-vs-deferred-execution.html' title='LINQ: Immutability vs. Deferred execution'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9991642&amp;postID=4736683988001109373' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.blogger.com/feedsindex.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/4736683988001109373'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/4736683988001109373'/><author><name>ether</name><uri>http://www.blogger.com/profile/16911611449176929088</uri><email>noreply@blogger.com</email></author></entry><entry><id>tag:blogger.com,1999:blog-9991642.post-4086050904740585632</id><published>2008-01-15T15:39:00.000-08:00</published><updated>2008-01-15T15:57:05.257-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='automatic property'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><title type='text'>Struct's via Automatic Properties can be tricky</title><content type='html'>Here's a bit of code i just got through debugging...

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; Point Point { get; &lt;span class="kwrd"&gt;private&lt;/span&gt; set; }

&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Offset(Point origin)
{
  Point.Offset(-origin.X, -origin.Y);
}
&lt;/pre&gt;
&lt;/div&gt;

Can you tell what's wrong here? Let's just say that the Offset won't take.

&lt;code&gt;struct&lt;/code&gt;s are value types, which means anytime you pass one around you get a new copy. So far so good. And that means when you expose a struct value via a property,  the accessing party always is looking at a copy. Again, fine, after all when you do a set you change the stored value and if you need to manipulate it, you just manipulate the actual struct.

&lt;p&gt;Enter Automatic properties and you might forget about this last detail and not realize that you never get access to the underlying value, even from within the class. I.e. when I call &lt;b&gt;Point.Offset&lt;/b&gt;, i'm calling it on the copy that was passed to me and the resulting value is immediately thrown away. So i just went back to using the property to facade a private &lt;b&gt;Point&lt;/b&gt;, which i can now manipulate inside of &lt;b&gt;Offset&lt;/b&gt;. Duh.</content><link rel='alternate' type='text/html' href='http://www.claassen.net/geek/blog/2008/01/structs-via-automatic-properties-can-be.html' title='Struct&apos;s via Automatic Properties can be tricky'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9991642&amp;postID=4086050904740585632' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.blogger.com/feedsindex.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/4086050904740585632'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/4086050904740585632'/><author><name>ether</name><uri>http://www.blogger.com/profile/16911611449176929088</uri><email>noreply@blogger.com</email></author></entry><entry><id>tag:blogger.com,1999:blog-9991642.post-9143749385085242818</id><published>2008-01-11T12:50:00.000-08:00</published><updated>2008-01-11T13:56:06.428-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='lambda'/><category scheme='http://www.blogger.com/atom/ns#' term='foreach'/><category scheme='http://www.blogger.com/atom/ns#' term='.net'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='delegate'/><title type='text'>More Deferred Execution Fun: foreach and delegation scope</title><content type='html'>This is closely related to my &lt;a href="http://www.claassen.net/geek/blog/2007/12/dangers-of-deferred-execution.html"&gt;last post on deferred execution&lt;/a&gt; gotchas and its basically more "if you inline delegated code, you may easily overlook scope side-effects". This time it's about dealing with foreach and using the local each item for deferred execution.

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SpawnActions()
{
  &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (ActionContext context &lt;span class="kwrd"&gt;in&lt;/span&gt; contexts)
  {
    &lt;span class="kwrd"&gt;int&lt;/span&gt; id = context.Id;
    Action&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; callback =  
      (workerNumber) =&amp;gt;
      {
        Console.WriteLine(&lt;span class="str"&gt;"{0} Id: {1}/{2}"&lt;/span&gt;, workerNumber, id, context.Id);
      };
    ThreadPool.QueueUserWorkItem(&lt;span class="kwrd"&gt;new&lt;/span&gt; WaitCallback(FutureExecute), callback);
  }
}

&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; FutureExecute(&lt;span class="kwrd"&gt;object&lt;/span&gt; state)
{
  &lt;span class="kwrd"&gt;int&lt;/span&gt; id = worker++;
  Action&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; callback = state &lt;span class="kwrd"&gt;as&lt;/span&gt; Action&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;;
  Thread.Sleep(500);
  callback(id);
}
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The output looks like this:
&lt;div id="csharpcode"&gt;
&lt;pre class="csharpcode"&gt;
0 Id: 0/9
1 Id: 1/9
2 Id: 2/9
3 Id: 3/9
4 Id: 4/9
5 Id: 5/9
6 Id: 6/9
7 Id: 7/9
8 Id: 8/9
9 Id: 9/9
&lt;/pre&gt;
&lt;/div&gt;

So while the foreach scope variable context is kept alive for the deferred execution, it turns out that foreach re-uses the variable on each pass through the loop and therefore when the Action&lt;int&gt; is later executed, each one has a reference to the last context. So what we need to do is create a true local variable for context so that the lambda's scope can hold on to the reference we want.

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
  &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (ActionContext context &lt;span class="kwrd"&gt;in&lt;/span&gt; contexts)
  {
    &lt;span class="kwrd"&gt;int&lt;/span&gt; id = context.Id;
    &lt;span class="rem"&gt;// locally scoped variable&lt;/span&gt;
    &lt;span class="kwrd"&gt;ActionContext&lt;/span&gt; c2 = context;
    Action&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; callback =  
      (workerNumber) =&amp;gt;
      {
        Console.WriteLine(&lt;span class="str"&gt;"{0} Id: {1}/{2}"&lt;/span&gt;, workerNumber, id, c2.Id);
      };
    ThreadPool.QueueUserWorkItem(&lt;span class="kwrd"&gt;new&lt;/span&gt; WaitCallback(FutureExecute), callback);
  }

&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And now our results are a bit more what we expected:

&lt;div id="csharpcode"&gt;
&lt;pre class="csharpcode"&gt;
0 Id: 0/0
1 Id: 1/1
2 Id: 2/2
3 Id: 3/3
4 Id: 4/4
5 Id: 5/5
6 Id: 6/6
7 Id: 7/7
8 Id: 8/8
9 Id: 9/9
&lt;/pre&gt;
&lt;/div&gt;</content><link rel='alternate' type='text/html' href='http://www.claassen.net/geek/blog/2008/01/more-deferred-execution-fun-foreach-and.html' title='More Deferred Execution Fun: foreach and delegation scope'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9991642&amp;postID=9143749385085242818' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.blogger.com/feedsindex.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/9143749385085242818'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/9143749385085242818'/><author><name>ether</name><uri>http://www.blogger.com/profile/16911611449176929088</uri><email>noreply@blogger.com</email></author></entry><entry><id>tag:blogger.com,1999:blog-9991642.post-3731104409054129969</id><published>2008-01-03T14:17:00.000-08:00</published><updated>2008-01-03T14:26:15.662-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='usercontrol'/><category scheme='http://www.blogger.com/atom/ns#' term='silverlight'/><category scheme='http://www.blogger.com/atom/ns#' term='AG_E_INVALID_ARGUMENT'/><title type='text'>Copying a Silverlight User Control from one project to another</title><content type='html'>Here's something I tracked down with no help from error messages:

&lt;p&gt;When you copy a user control in Silverlight 1.1 from one project to another the Xaml that the control loads will have it's &lt;b&gt;Build Action&lt;/b&gt; set to &lt;b&gt;&lt;i&gt;SilverlightPage&lt;/i&gt;&lt;/b&gt;. When you then run your project and try to create an instance of that control you'll get the ever so informative &lt;b&gt;AG_E_INVALID_ARGUMENT&lt;/b&gt;. All you need to do to fix it, is set the &lt;b&gt;Build Action&lt;/b&gt; to &lt;b&gt;&lt;i&gt;Embedded Resource&lt;/i&gt;&lt;/b&gt; again. &lt;i&gt;Tada!&lt;/i&gt;

&lt;p&gt;I love declarative definition of UI with behavior wired to static code. But man, at its current state, the debugging support for it just isn't there. I mean it's bad enough that having strings in the declarative side to link to actions that won't be updated by normal refactoring, nor will they show up as references, but at this state Xaml brings the worst part of scripting languages to compile-time checked coding:

&lt;p&gt;&lt;b&gt;Vague runtime errors without a stacktrace&lt;/b&gt;

&lt;p&gt;Bah.</content><link rel='alternate' type='text/html' href='http://www.claassen.net/geek/blog/2008/01/copying-silverlight-user-control-from.html' title='Copying a Silverlight User Control from one project to another'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9991642&amp;postID=3731104409054129969' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.blogger.com/feedsindex.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/3731104409054129969'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/3731104409054129969'/><author><name>ether</name><uri>http://www.blogger.com/profile/16911611449176929088</uri><email>noreply@blogger.com</email></author></entry><entry><id>tag:blogger.com,1999:blog-9991642.post-4862263197364566727</id><published>2008-01-03T08:45:00.000-08:00</published><updated>2008-01-03T09:23:48.013-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='activation'/><category scheme='http://www.blogger.com/atom/ns#' term='boot camp'/><category scheme='http://www.blogger.com/atom/ns#' term='vmware fusion'/><title type='text'>XP on Bootcamp &amp; VMWare Fusion, take 2</title><content type='html'>&lt;h2&gt;Time to re-install XP&lt;/h2&gt;

&lt;p&gt;My setup until yesterday was Bootcamp partion that was running as a VM using VMWare Fusion. The Bootcamp partition was set up as FAT32, because I NTFS came up as readonly when mounted under Mac OS.

&lt;p&gt;Then I started up another VM (Fedora Core 7, although i have since noticed it's not what OS you run, but just a second VM). Maybe it's not enough memory, maybe it's a VM running from bootcamp plus a VM running from a disk image, but while it had worked previously, this time, it locked up my Mac hard. I finally had to hard boot the Mac. When I got back into th VM, I noticed things were broken. Now, I've had to hard boot XP many times and I've never seen this. An indeterminate number of files were corrupted. I noticed one XML file that halfway through turned into binary garbage, so I assume that the other systems failing were suffering from similar corruption. Basically it was hosed, because there was no way to determine what had been corrupted. Time to re-install.

&lt;h2&gt;Bootcamp &amp; VMware install&lt;/h2&gt;

Looking at my post from last time, it was clearly written with the frazzled recollection of a day of trying to make things work, since i once again ran into problems. This time I'm making sure I write the resolution down.

&lt;ol&gt;
&lt;li&gt;Imaged the old partition, because you always realize that you forgot to back up some vital file.

&lt;li&gt;Removed the old partition and created a new one (using the Bootcamp tool), this time large enough for more than just fallback use (since it's used daily under VMWare). This creates a FAT32 partition.

&lt;li&gt;Inserted my install disk and Bootcamp restarted to fire up the windows install. Important note here, this has to be a real XP install disk. I first tried to use my MSDN DVD with the chooser for picking what OS to install. However, the Mac keyboard doesn't seem to work when you get to the menu. So I used an XP Pro w/ SP2 CD and rebooted. Now the installer ran just fine

&lt;li&gt;Formatted the partition using NTFS. Even if you use FAT32, re-format, don't use the Bootcamp formatted partition. At least for me, using that prepared partition didn't work and created an unbootable image. I know NTFS cannot be written to by MacOS, but it doesn't really matter, since once I boot it as a VM, i can always transfer files via loopback file sharing. I'm going NTFS to get a journaling file system. Theoretically that should prevent the corruption I got last time.

&lt;li&gt;After the install completes, pop in the Mac OS disk and let it install the Bootcamp utilities. This gives you full support for the Macbook Pro hardware.

&lt;li&gt;Activate Windows and reboot into Mac OS X.

&lt;li&gt;Fire up VMWare Fusion. Bootcamp partition should be listed as a VM. Since I previously had a bootcamp partition, I had to go into &lt;b&gt;Library::VMWare Fusion::Application Support::Virtual Machines&lt;/b&gt; and remove the old Bootcamp partition folder. Fusion will then do its magic and prep the Bootcamp partition to run as a VMWare Image.

&lt;li&gt;Activate Windows again. That should be the last time you have to do it.

&lt;/ol&gt;

&lt;p&gt;Let's hope this doesn't turn into a bi-monthly process :)</content><link rel='alternate' type='text/html' href='http://www.claassen.net/geek/blog/2008/01/xp-on-bootcamp-vmware-fusion-take-2.html' title='XP on Bootcamp &amp; VMWare Fusion, take 2'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9991642&amp;postID=4862263197364566727' title='4 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.blogger.com/feedsindex.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/4862263197364566727'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/4862263197364566727'/><author><name>ether</name><uri>http://www.blogger.com/profile/16911611449176929088</uri><email>noreply@blogger.com</email></author></entry><entry><id>tag:blogger.com,1999:blog-9991642.post-1373429173464221732</id><published>2007-12-30T13:39:00.000-08:00</published><updated>2007-12-30T14:03:31.650-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='lambda'/><category scheme='http://www.blogger.com/atom/ns#' term='.net'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='delegate'/><title type='text'>The dangers of deferred execution</title><content type='html'>I recently wrote about &lt;a href="http://www.claassen.net/geek/blog/2007/12/action-func-never-write-another.html"&gt;Action &amp; Func&lt;/a&gt;, which along with Lambda expression let you do easy inline callbacks like this:

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
Utility.ActionDownloader.Download(
  Configuration.GetAssetUri(dto.Url),
  (Downloader d) =&amp;gt;
  {
    FloatContainer c = (FloatContainer)XamlReader.Load(d.ResponseText);
    c.Initialize(dto);
  });
&lt;/pre&gt;
&lt;/div&gt;

i.e. I can call a downloader and inline pass it a bit of code to execute once the download completes. But the catch of course is that looking at the code, and following the usual visual tracing of flow hides the fact that &lt;b&gt;c.Initialize(dto)&lt;/b&gt; doesn't get called until some asynchronous time in the future. Now, that's always been a side-effect of delegates, but until they became anonymous and inline, the visual deception of code &lt;i&gt;that looks like it's in the current flow scope but isn't&lt;/i&gt; wasn't there.

&lt;p&gt;What happened was that I needed my main routine to execute some code after &lt;b&gt;FloatContainer&lt;/b&gt; was initialized, and by habit i created an &lt;b&gt;Initialized&lt;/b&gt; event on &lt;b&gt;FloatContainer&lt;/b&gt;. Of course this was superfluous, since my lambda expression called the synchronous &lt;b&gt;Initialize&lt;/b&gt;, i.e my action could be placed inline after that call to &lt;b&gt;c.Initialize(dto)&lt;/b&gt; and be guaranteed to be called after initialization had completed.

&lt;p&gt;This scenario just meant I created some superfluous code. However, I'm sure as I use lambda expression more, there will be more pitfalls of writing code that doesn't consider that its execution time is unknown, as is the state of the objects tied to the scope of the expression.

&lt;p&gt;This last bit about objects tied to the expression scope is especially tricky and I think we will see some help in terms of Immutable concepts weaving their way into C# 3.x or 4.0, as the whole functional aspect of lambda expressions really work best when dealing with objects that cannot change state. Eric Lippert's been laying the groundwork in a number of posts on the &lt;a href="http://blogs.msdn.com/ericlippert/archive/2007/11/13/immutability-in-c-part-one-kinds-of-immutability.aspx"&gt;subject&lt;/a&gt; and while he constantly disclaims that his ponderings are not a roadmap for C#, I am still going to assume that his interest and recognition of the subject of Immutables will have some impact in a future revision of the language. Well, I at least hope it does.</content><link rel='alternate' type='text/html' href='http://www.claassen.net/geek/blog/2007/12/dangers-of-deferred-execution.html' title='The dangers of deferred execution'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9991642&amp;postID=1373429173464221732' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.blogger.com/feedsindex.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/1373429173464221732'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/1373429173464221732'/><author><name>ether</name><uri>http://www.blogger.com/profile/16911611449176929088</uri><email>noreply@blogger.com</email></author></entry><entry><id>tag:blogger.com,1999:blog-9991642.post-2927176555355174897</id><published>2007-12-14T10:50:00.000-08:00</published><updated>2007-12-14T11:00:43.538-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='activation'/><category scheme='http://www.blogger.com/atom/ns#' term='boot camp'/><category scheme='http://www.blogger.com/atom/ns#' term='vmware fusion'/><title type='text'>Software Activation vs. Virtualization, Part 3</title><content type='html'>Part of an &lt;a href="http://www.claassen.net/geek/blog/2007/12/software-activation-vs-virtualization.html"&gt;ongoing&lt;/a&gt; &lt;a href="http://www.claassen.net/geek/blog/2007/10/my-new-visual-studio-dev-workhorse.html"&gt;saga&lt;/a&gt;.

&lt;p&gt;Rebooted back into VMWare Fusion and yeah, Illustrator Activation was indeed screwed there as well. Office 2007 too, but at least it just let's me reactivate (no doubt noting me as a repeat offender somewhere). So I called Adobe and was told that "it's a sensitive piece of software". No it's not. Illustrator can take any beating you give it.. It's the "anti-piracy" crap that's sensitive. I got an "emergency activation code" to get it going again and was advised to &lt;i&gt;Deactivate&lt;/i&gt; before i switch VM setups and then re-activate after the reboot. &lt;i&gt;OMFG&lt;/i&gt;. Seriously, just give me USB dongle if you are so sensitive about it. That would be inifintely more convenient.

&lt;p&gt;Dug around the net a bit and it seems that if i fake my mac address to be the same between boot camp and the VM boot, it'll not invalidate my activation. Might try that next. Of course, the same board i found that on also noted that if I just got a crack for my legally purchased product, all troubles would be gone as well. Yes, once again, anti-piracy crap is not stopping pirates but legitimate customers. You'd figure someone  might have spotted the pattern here, but may those DRM-colored glasses filter reality a bit too well.</content><link rel='alternate' type='text/html' href='http://www.claassen.net/geek/blog/2007/12/software-activation-vs-virtualization_14.html' title='Software Activation vs. Virtualization, Part 3'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9991642&amp;postID=2927176555355174897' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.blogger.com/feedsindex.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/2927176555355174897'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/2927176555355174897'/><author><name>ether</name><uri>http://www.blogger.com/profile/16911611449176929088</uri><email>noreply@blogger.com</email></author></entry><entry><id>tag:blogger.com,1999:blog-9991642.post-6040327581172856719</id><published>2007-12-13T22:39:00.000-08:00</published><updated>2007-12-13T23:21:18.334-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='lambda'/><category scheme='http://www.blogger.com/atom/ns#' term='.net'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='delegate'/><title type='text'>Action &amp; Func: Never write another delegate</title><content type='html'>With lambda expressions in C#, the Func&lt;TResult&gt; generic delegate and it's variations have been getting a lot of attention. So naturally, you might think that the lambda syntax is just a shortcut for creating anonymous delegates, whether they return values or not.

&lt;p&gt;First let's look at the evolution of delegates from 1.1 to now. Delegates, simply are the method equivalent of function pointers. They let you pass a method call as an argument for later execution. The cool thing (and a garbage collection pitfall) is that a delegate creates a lexical closure, i.e. the delegate carries with it the object that the method gets called on. For garbage collection this means that a delegate prevents an object from being collection. That's why it's important to unsubscribe from those events you subscribed to.

&lt;p&gt;But I digress. Let's define a delegate that returns an Integer and a method that matches that delegate:

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
&lt;span class="kwrd"&gt;delegate&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; IntProducerDelegate();

&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; x = 0;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; IntProducer()
{
  &lt;span class="kwrd"&gt;return&lt;/span&gt; x++;
}
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;With the original .NET 1.0 syntax we'd create the delegate like this:

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
IntProducerDelegate p1 = &lt;span class="kwrd"&gt;new&lt;/span&gt; IntProducerDelegate(IntProducer);
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Now we can call &lt;code&gt;p1()&lt;/code&gt; and get an integer back, and since it's closure, each time we call p1() the originating objects x increases as does our return value.

&lt;p&gt;Then, in .Net 2.0 we got anonymous delegates.

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
IntProducerDelegate p2 = &lt;span class="kwrd"&gt;delegate&lt;/span&gt; { &lt;span class="kwrd"&gt;return&lt;/span&gt; IntProducer(); };

&lt;span class="rem"&gt;// or with IntProducer's action inlined...&lt;/span&gt;
IntProducerDelegate p3 = &lt;span class="kwrd"&gt;delegate&lt;/span&gt; { &lt;span class="kwrd"&gt;return&lt;/span&gt; x++; };
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This got rid of the need to create a method just to pass along a closure that manipulated our object at a later time. The other thing that anonymous delegates re-inforce is that delegates just care about signature. IntProducerDelegate can get assigned any delegate that takes no argument and returns an int. That sounds like a perfect scenario for a delegate and in .NET 3.5, we got just that, a set of generic delegates called &lt;a href="http://msdn2.microsoft.com/en-us/library/bb534960(VS.90).aspx"&gt;Func&lt;/a&gt;. Using Func, we quickly get to our lambda expression replacing the original delegate syntax like this:

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
&lt;span class="rem"&gt;// create a new Func delegate just like the IntProducerDelegate&lt;/span&gt;
IntProducerDelegate p3 = &lt;span class="kwrd"&gt;new&lt;/span&gt; Func&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;(IntProducer);

&lt;span class="rem"&gt;// which means that we don't need IntProducerDelegate at all anymore&lt;/span&gt;
Func&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; p4 = &lt;span class="kwrd"&gt;delegate&lt;/span&gt; { &lt;span class="kwrd"&gt;return&lt;/span&gt; x++; };

&lt;span class="rem"&gt;// and the anonymous delegate can also be shorthanded with a lambda expression&lt;/span&gt;
Func&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; p5 = () =&amp;gt; { &lt;span class="kwrd"&gt;return&lt;/span&gt; x++; };
&lt;span class="rem"&gt;// which says, given that we take no argument "()", execute and return the following "return x++;"&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;However, before there ever was Func&lt;TResult&gt;, .Net 2.0 introduced the generic delegate &lt;a href="http://msdn2.microsoft.com/en-us/library/018hxwa8(VS.90).aspx"&gt;Action&lt;T&gt;&lt;/a&gt;, which is a natural counterpart to Func&lt;TResult&gt;, encapsulating a method that does not return anything. Following through the example of the producer, we'll create a consumer like:

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
&lt;span class="kwrd"&gt;delegate&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; IntConsumerDelegate(&lt;span class="kwrd"&gt;int&lt;/span&gt; i);

&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; IntConsumer(&lt;span class="kwrd"&gt;int&lt;/span&gt; i)
{
  Console.WriteLine(&lt;span class="str"&gt;"The number is {0}"&lt;/span&gt;, i);
}
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now following the same evolution of syntax we get this:

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
IntConsumerDelegate c1 = &lt;span class="kwrd"&gt;new&lt;/span&gt; IntConsumerDelegate(IntConsumer);

IntConsumerDelegate c2 = &lt;span class="kwrd"&gt;new&lt;/span&gt; Action&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;(IntConsumer);

Action&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; c3 = &lt;span class="kwrd"&gt;delegate&lt;/span&gt;(&lt;span class="kwrd"&gt;int&lt;/span&gt; i) { Console.WriteLine(&lt;span class="str"&gt;"The number is {0}"&lt;/span&gt;, i); };

Action&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; c4 = (i) =&amp;gt; { Console.WriteLine(&lt;span class="str"&gt;"The number is {0}"&lt;/span&gt;, i); };
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;So lambda syntax can be used to create either a &lt;span style="font-weight:bold;"&gt;Func&lt;TResult&gt;&lt;/span&gt; or an &lt;span style="font-weight:bold;"&gt;Action&lt;T&gt;&lt;/span&gt;. And that also means that we never have to explicitly need to create another delegate, being able to use a variation of these two generic delegates as our arsenal for storing lambda expressions of all kinds.</content><link rel='alternate' type='text/html' href='http://www.claassen.net/geek/blog/2007/12/action-func-never-write-another.html' title='Action &amp; Func: Never write another delegate'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9991642&amp;postID=6040327581172856719' title='2 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.blogger.com/feedsindex.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/6040327581172856719'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/6040327581172856719'/><author><name>ether</name><uri>http://www.blogger.com/profile/16911611449176929088</uri><email>noreply@blogger.com</email></author></entry><entry><id>tag:blogger.com,1999:blog-9991642.post-4358332521329450308</id><published>2007-12-13T10:12:00.000-08:00</published><updated>2007-12-13T10:22:36.708-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='activation'/><category scheme='http://www.blogger.com/atom/ns#' term='boot camp'/><category scheme='http://www.blogger.com/atom/ns#' term='vmware fusion'/><title type='text'>Software Activation vs. Virtualization (and multiple PC ownership)</title><content type='html'>Just as Virtualization is finally becoming a useful technology, everybody and their uncle has decided that software activation is the new hot way to stop theft. Of course, like all anti-piracy tools, the paying customers get screwed, because the pirates have already patched their copies to not require activation. Bravo! You know i'd prefer friggin USB dongles to this big brother activation business.

&lt;p&gt;I've &lt;a href="http://www.claassen.net/geek/blog/2007/10/my-new-visual-studio-dev-workhorse.html"&gt;talked about these problems before&lt;/a&gt;, but I've got more fun with the VM vs. bootcamp image activation troubles. I just got  Adobe CS3 and for a programmer with occasional Photoshop/Illustrator needs, that's a pretty serious expense. I mean it costs me more than MSDN and gets used a fraction of the time. But I need it. And forget that I have three different computers I use at different times and I really ought to be able to install my purchased software on all of these machines, since I, the owner of the license, will never be using two computers at once. But that's a whole other story.

&lt;p&gt;Back to the re-activation on hardware change business... I've been running Windows under VMware for the last couple of weeks, but for the Illustrator work I need to do right now, it was a bit sluggish. &lt;i&gt;No problem, reboot into Bootcamp!&lt;/i&gt; Mind you, this isn't a differnt install of Windows. This is the same physical disk partition, but booted natively vs. via VMware. What happens? Illustrator bitches about activation, as does office, because it saw the hardware change. Let me guess, when i reboot in the virtual machine it'll bitch yet again. Sooner or later it'll just shut me down as a serial offender. &lt;i&gt;Thanks!&lt;/i&gt; Way to reward my purchase.</content><link rel='alternate' type='text/html' href='http://www.claassen.net/geek/blog/2007/12/software-activation-vs-virtualization.html' title='Software Activation vs. Virtualization (and multiple PC ownership)'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9991642&amp;postID=4358332521329450308' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.blogger.com/feedsindex.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/4358332521329450308'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/4358332521329450308'/><author><name>ether</name><uri>http://www.blogger.com/profile/16911611449176929088</uri><email>noreply@blogger.com</email></author></entry><entry><id>tag:blogger.com,1999:blog-9991642.post-7036394994302748541</id><published>2007-12-09T16:38:00.000-08:00</published><updated>2007-12-10T11:00:09.307-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='xml'/><category scheme='http://www.blogger.com/atom/ns#' term='xaml'/><category scheme='http://www.blogger.com/atom/ns#' term='json'/><category scheme='http://www.blogger.com/atom/ns#' term='xslt'/><title type='text'>A case for XML</title><content type='html'>XML gets maligned a lot. It's &lt;i&gt;enterprisey&lt;/i&gt;, bloated, overly complex, etc. And the abuses visited upon it, like trying to express flow control or whole DSLs in it or being proposed as some sort of panacea for all interop problems only compound this perception. But as long as you treat it as what it is, &lt;i&gt;data storage&lt;/i&gt;, I generally can find little justification to use something else. Not because it's the best, but because it's everywhere.

&lt;p&gt;If you are your own consumer and you want a more efficient data storage, just go binary already. If you're not, then I bet your data consumers are just tickled that they have to add another parser to their repository of data ingestors. Jim Clark probably put it best when he &lt;a href="http://blog.jclark.com/2007/04/do-we-need-new-kind-of-schema-language.html"&gt;said&lt;/a&gt;:

&lt;blockquote&gt;&lt;i&gt;"For the payload format, XML has to be the mainstay, not because it's technically wonderful, but because of the extraordinary breadth of adoption that it has succeeded in achieving. This is where the JSON (or YAML) folks are really missing the point by proudly pointing to the technical advantages of their format: any damn fool could produce a better data format than XML."&lt;/i&gt;&lt;/blockquote&gt;

&lt;p&gt;Ok, I won't get religious on the subject, but mostly wanted to give a couple of examples, where the abilities and the adoption of XML have been a godsend for me. All this does assume you have a mature XML infrastructure. If you're dealing with XML via SAX or even are doing the parsing and writing by hand, then you are in a world of hurt, I admit. But unless it's a memory constraint there really is no reason to do that. Virtually every language has an XML DOM lib at this point.

&lt;h2&gt;I love namespaces&lt;/h2&gt;

&lt;p&gt;One feature a lot of people usually point to when they decry XML to me is namespaces. They can be tricky, i admit, and a lot of consumers of XML don't handle them right, causing problems. Like Blend puking on namespaces that weren't apparently hardcoded into its parser. But very simply, namespaces let you annotate an existing data format without messing with it.

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;somedata&lt;/span&gt; &lt;span class="attr"&gt;droog:meta&lt;/span&gt;&lt;span class="kwrd"&gt;="some info about somedata"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;droog:metablock&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;And a whole block of extra data&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;droog:metablock&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;somedata&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Here's the scenario. I get data in XML and need to reference metadata for processing further down the pipeline. I could have ingested the XML and then written out my own data format. But that would mean I'd have to also do the reverse if I wanted to pass the data along or return it after some modifications and I have to define yet another data format. By creating my own namespace, I am able to annotate the existing data without affecting the source schema and I can simply strip out my namespace when passing the processed data along to someone else. Every data format should be so versatile.

&lt;h2&gt;Transformation, Part 1: Templating&lt;/h2&gt;

When writing webapps, there are literally dozens of templating engines and there's constantly new ones emerging. I chose to learn XSLT some years back because I liked how &lt;a href="http://cocoon.apache.org/"&gt;Cocoon&lt;/a&gt; and &lt;a href="http://axkit.org/"&gt;AxKit&lt;/a&gt; handled web pages. Just create your data in XML and then transform it using XSLT according to the delivery needs. So far, nothing especially unique compared to other templating engines. Except unlike most engines, it didn't rely on some program creating the data and then invoking the templating code. XSLT works with dynamic Apps as easily as with static XML or third party XML without having.

&lt;p&gt;Since those web site roots, I've had need for email templating and data transformation in .NET projects and was able to leverage the same XSLT knowledge. That means I don't have to pick up yet another tool to do a familiar task just a little differently.

&lt;h2&gt;What's the file format?&lt;/h2&gt;
When I first started playing with Xaml, I was taking &lt;a href="http://www.lfs.net/"&gt;Live For Speed&lt;/a&gt; geometry data and wanted to render it in WPF and Silverlight. Sure, I had to learn the syntax of the geometry constructs, but I didn't have to worry about figuring out the data format. I just used the more than familiar XmlDocument and was able to concentrate on geometry, not file formats. 

&lt;h2&gt;Transformation, Part 2: Rewriting&lt;/h2&gt;
Currently I'm working with Xaml again for a Silverlight project. My problem was that I had data visualization in Xaml format (coming out of Illustrator), as well as associated metadata (a database of context data) and I needed to attach the metadata to the geometry, along with behavior. Since the first two are output from other tools I needed a process that could be automated. One way would be to walk the Visual tree once loaded, create a parallel hierarchy of objects containing the metadata and behavior and attach their behavior to the visual tree. But i'd rather have the data do this for itself.

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Canvas&lt;/span&gt; &lt;span class="attr"&gt;x:Name&lt;/span&gt;&lt;span class="kwrd"&gt;="rolloverContainer_1"&lt;/span&gt; &lt;span class="attr"&gt;Width&lt;/span&gt;&lt;span class="kwrd"&gt;="100"&lt;/span&gt; &lt;span class="attr"&gt;Height&lt;/span&gt;&lt;span class="kwrd"&gt;="100"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="rem"&gt;&amp;lt;!-- Some geometry data --&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Canvas&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="rem"&gt;&amp;lt;!-- becomes --&amp;gt;&lt;/span&gt;

&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;droog:RolloverContainer&lt;/span&gt; &lt;span class="attr"&gt;x:Name&lt;/span&gt;&lt;span class="kwrd"&gt;="rolloverContainer_1"&lt;/span&gt; &lt;span class="attr"&gt;Width&lt;/span&gt;&lt;span class="kwrd"&gt;="100"&lt;/span&gt; &lt;span class="attr"&gt;Height&lt;/span&gt;&lt;span class="kwrd"&gt;="100"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="rem"&gt;&amp;lt;!-- Some geometry data --&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;droog:RolloverContainer&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;So I created custom controls that subclassed the geometry content containers. I then created a post-processing script that simply loaded the Xaml into the DOM and rewrote the geometry containers as the appropriate custom controls using object naming as an identifying convention. Now the wiring happens automatically at load, courtesy of Silverlight. Again, no special parser required, just using the same XmlDocument class I've used for years.

&lt;h2&gt;And finally, Serialization&lt;/h2&gt;

I use XML serialization for over the wire transfers as well as data and configuration storage. In all cases, it lets me simply define my DTOs and use them as part of my object hierarchy without ever having to worry about persistence. I just save my object graph by serializing it to XML and rebuild the graph by deserializing the stream again.

&lt;p&gt;I admit that this last bit does depend on some language dependent plumbing that's not all that standard. In .NET, it's built in and let's me mark in my objects with attributes. In Java, I use Simple for the same effect. Without this attribute driven mark up, I'd have to walk the DOM and build m objects by hand, which would be painful.

&lt;p&gt;Sure, for data, binary serialization would be cheaper and more compact, but that misses the other benefits I get for free. The data can be ingested and produced by a wide variety of other platforms, I can manually edit it, or easily build tools for editing and generation, without any specialized coding.

&lt;p&gt;For my Silverlight project, I'm currently using JSON as my serialization layer between client and server, since there currently is no XmlSerializer or even XmlDocument in Silverlight 1.1. It, too, was painless to generate and ingest and, admittedly, much more compact. But I then I added this bit to my DTO:
&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
List&amp;lt;IContentContainer&amp;gt; Containers = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;IContentContainer&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;It serialized just fine, but then on the other end it complained about there not being a no-argument constructor for IContentContainer. &lt;i&gt;Ho Hum&lt;/i&gt;. Easily enough worked around for now, but I will be switching back to XML for this once Silverlight 2.0 fleshes out the framework. Worst case, I'll have to build XmlSerializerLitem, or something like that, myself.

&lt;p&gt;All in all, XML has allowed me to do a lot of data related work without having to constantly worry about yet another file format, or parser. It's really not about being the best format, but about it virtually being everywhere and being supported with a mature toolchain across the vast majority of programming environment and that pays a lot of dividents, imho.</content><link rel='alternate' type='text/html' href='http://www.claassen.net/geek/blog/2007/12/case-for-xml.html' title='A case for XML'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9991642&amp;postID=7036394994302748541' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.blogger.com/feedsindex.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/7036394994302748541'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/7036394994302748541'/><author><name>ether</name><uri>http://www.blogger.com/profile/16911611449176929088</uri><email>noreply@blogger.com</email></author></entry><entry><id>tag:blogger.com,1999:blog-9991642.post-5065203797982961592</id><published>2007-11-28T11:22:00.000-08:00</published><updated>2007-11-28T13:01:36.509-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='lambda'/><category scheme='http://www.blogger.com/atom/ns#' term='yield'/><category scheme='http://www.blogger.com/atom/ns#' term='linq'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET 3.5'/><category scheme='http://www.blogger.com/atom/ns#' term='tree'/><title type='text'>Searching a Tree of Objects with Linq</title><content type='html'>I've finally had legitimate use for LINQ to Objects, not just to make the syntax cleaner, but also to simplify the underlying code and provide me a lot of flexibility without significant work.

&lt;h2&gt;The scenario&lt;/h2&gt;
&lt;p&gt;I have a tree of objects that have both a type and a name. The name is unique, the Type is not. The interface is this:

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; INode
{
  &lt;span class="kwrd"&gt;int&lt;/span&gt; Id { get; }
  &lt;span class="kwrd"&gt;string&lt;/span&gt; Name { get; set; }
  &lt;span class="kwrd"&gt;string&lt;/span&gt; Type { get; set; }
  List&amp;lt;INode&amp;gt; Children { get; }
}
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;I want to be able to find a single named Node in the tree and I want to be able to retrieve a collection of all nodes for a particular type. The searchable interface could be expressed as this:

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; ISearchableNode : INode
{
  INode FindByName(&lt;span class="kwrd"&gt;string&lt;/span&gt; name);
  IEnumerable&amp;lt;INode&amp;gt; FindByType(&lt;span class="kwrd"&gt;string&lt;/span&gt; name);
}
&lt;/pre&gt;
&lt;/div&gt;

Both require me to walk the tree and examine each node, so clearly I just want to have one walk routine and generically evaluate the node in question. In C# 2.0 parlance, that means I could pass an anonymous delegate into my generic find routine and have it recursively iterate through all the children. I also pass along a resultset to be populated.

&lt;p&gt;The signature for the evaluation delegate looks like this:
&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
&lt;span class="kwrd"&gt;delegate&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; FindDelegate(INode node);
&lt;/pre&gt;
&lt;/div&gt;

but since I'm using C# 3.0 (i.e. .NET 3.5) I can use &lt;i&gt;lambda expressions&lt;/i&gt; to avoid creating a delegate and simplify my syntax. Instead of &lt;code&gt;FindDelegate&lt;/code&gt;, I can simply use &lt;code&gt;Func&amp;lt;INode,bool&amp;gt;&lt;/code&gt;:

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
&lt;span class="rem"&gt;// Instead of this:&lt;/span&gt;
&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Find(INode node, List&amp;lt;INode&amp;gt; resultSet, FindDelegate findDelegate);
&lt;span class="rem"&gt;// called like this for a Name search:&lt;/span&gt;
Find(&lt;span class="kwrd"&gt;this&lt;/span&gt;, resultSet, &lt;span class="kwrd"&gt;delegate&lt;/span&gt;(INode node) { &lt;span class="kwrd"&gt;return&lt;/span&gt; node.Name == name; });

&lt;span class="rem"&gt;// I can use this:&lt;/span&gt;
&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Find(INode node, List&amp;lt;INode&amp;gt; resultSet, Func&amp;lt;INode, &lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt; f)
&lt;span class="rem"&gt;// called like this:&lt;/span&gt;
Find(&lt;span class="kwrd"&gt;this&lt;/span&gt;, resultSet, node =&amp;gt; node.Name == name);
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Thus giving me the following implementation for &lt;code&gt;ISearchableNode&lt;/Node&gt;:

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; INode FindByName(&lt;span class="kwrd"&gt;string&lt;/span&gt; name)
{
  List&amp;lt;INode&amp;gt; resultSet = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;INode&amp;gt;();
  Find(&lt;span class="kwrd"&gt;this&lt;/span&gt;, resultSet, x =&amp;gt; x.Name == name);
  &lt;span class="kwrd"&gt;return&lt;/span&gt; resultSet.FirstOrDefault();
}

&lt;span id="kwrd"&gt;public&lt;/span&gt; IEnumerable&amp;lt;INode&amp;gt; FindByType(&lt;span class="kwrd"&gt;string&lt;/span&gt; type)
{
  List&amp;lt;INode&amp;gt; resultSet = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;INode&amp;gt;();
  Find(&lt;span class="kwrd"&gt;this&lt;/span&gt;, resultSet, x =&amp;gt; x.Type == type);
  &lt;span class="kwrd"&gt;return&lt;/span&gt; (IEnumerable&amp;lt;INode&amp;gt;)resultSet;
}

&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Find(INode node, List&amp;lt;INode&amp;gt; resultSet, Func&amp;lt;INode, &lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt; f)
{
  &lt;span class="kwrd"&gt;if&lt;/span&gt; (f(node))
  {
    resultSet.Add(node);
  }
  &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (INode child &lt;span class="kwrd"&gt;in&lt;/span&gt; node.Children)
  {
    Find(child, resultSet, f);
  }
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Problem solved, move on... Well, except there is significant room for improvement. Here are the two main issues that ought to be resolved:

&lt;ol&gt;
&lt;li&gt;Syntax is limited to two types of searches and exposing the generic find makes for an ugly syntax. It would be much nicer if queries to the tree could be expressed in LINQ syntax.
&lt;li&gt;It's also inefficient for the Name search, since I'm walking the entire tree, even if the first node matched the criteria.
&lt;/ol&gt;

&lt;h2&gt;LINQ to Hierarchical Data&lt;/h2&gt;

&lt;p&gt;In order to use LINQ to objects, I need to either create a custom query provider or implement IEnumerable. The latter is significantly simpler and could be expressed using the following interface:

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; IQueryableNode : IEnumerable&amp;lt;INode&amp;gt;
{
}
&lt;/pre&gt;&lt;/div&gt;

Ok, ok, I don't even need an interface, I could just implement IEnumerable&amp;lt;INode&amp;gt;... But what does that actually mean? In the simplest sense, I'm iterating over the node's children, however, I also with do descend into the children's children and so on. So a simple foreach won't do. I could just do the same tree walking with a resultset as I did above and return the Enumerator of the resulting list to implement the interface, but C# 2.0 introduced a much more useful way to implement non-linear Iterators, i.e. the &lt;b&gt;yield&lt;/b&gt; keyword. Instead of building a list to be interated over, &lt;b&gt;yield&lt;/b&gt; let's the iterating code return values as they are found, which means it can be used for recursive iteration. Thus the GetEnumerator is implemented simply as follows

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
&lt;span class="preproc"&gt;#region&lt;/span&gt; IEnumerable&amp;lt;Node&amp;gt; Members
&lt;span class="kwrd"&gt;public&lt;/span&gt; IEnumerator&amp;lt;INode&amp;gt; GetEnumerator()
{
  &lt;span class="kwrd"&gt;yield&lt;/span&gt; &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;this&lt;/span&gt;;
  &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (Node child &lt;span class="kwrd"&gt;in&lt;/span&gt; Children)
  {
    &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (Node subchild &lt;span class="kwrd"&gt;in&lt;/span&gt; child)
    {
      &lt;span class="kwrd"&gt;yield&lt;/span&gt; &lt;span class="kwrd"&gt;return&lt;/span&gt; subchild;
    }
  }
}
&lt;span class="preproc"&gt;#endregion&lt;/span&gt;

&lt;span class="preproc"&gt;#region&lt;/span&gt; IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
  &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;this&lt;/span&gt;.GetEnumerator();
}
&lt;span class="preproc"&gt;#endregion&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

Nice and simple and ready for LINQ.

&lt;p&gt;Searching for all Nodes of a type becomes

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
var allBar = from n &lt;span class="kwrd"&gt;in&lt;/span&gt; x
                &lt;span class="kwrd"&gt;where&lt;/span&gt; n.Type == &lt;span class="str"&gt;"bar"&lt;/span&gt;
                select n;
&lt;span class="kwrd"&gt;foreach&lt;/span&gt; (Node n &lt;span class="kwrd"&gt;in&lt;/span&gt; allBar)
{
  &lt;span class="rem"&gt;// do something with that node&lt;/span&gt;
}
&lt;/pre&gt;&lt;/div&gt;

and the search for a specifically named node becomes

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
INode node = (from n &lt;span class="kwrd"&gt;in&lt;/span&gt; x
              &lt;span class="kwrd"&gt;where&lt;/span&gt; n.Name == &lt;span class="str"&gt;"i"&lt;/span&gt;
              select n).FirstOrDefault();
&lt;/pre&gt;
&lt;/div&gt;

But the real benefit of this approach is that I don't have hard-coded search methods, but can express much more complex queries in a very natural syntax without any extra code on the Node.

&lt;h2&gt;Deferred execution&lt;/h2&gt;

&lt;p&gt;As it turns out, using &lt;b&gt;yield&lt;/b&gt; for the recursive iteration also solved the second issue. As yield returns values as it encounters them during iteration, the search doesn't happen until the query is executed. And one of the side effects of LINQ syntax is that creating a query does not execute it until the result set is iterated over. Therefore, &lt;code&gt;FirstOrDefault()&lt;/code&gt; actually short-circuits the query as soon as the first match (and in case of Name, it's going to be the only match) is hit.</content><link rel='alternate' type='text/html' href='http://www.claassen.net/geek/blog/2007/11/searching-tree-of-objects-with-linq.html' title='Searching a Tree of Objects with Linq'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9991642&amp;postID=5065203797982961592' title='5 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.blogger.com/feedsindex.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/5065203797982961592'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/5065203797982961592'/><author><name>ether</name><uri>http://www.blogger.com/profile/16911611449176929088</uri><email>noreply@blogger.com</email></author></entry><entry><id>tag:blogger.com,1999:blog-9991642.post-842710532044374529</id><published>2007-11-22T09:36:00.001-08:00</published><updated>2007-11-22T09:58:59.477-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Threading'/><category scheme='http://www.blogger.com/atom/ns#' term='Thunderbird'/><category scheme='http://www.blogger.com/atom/ns#' term='Mail.app'/><title type='text'>Threading: Mail.app vs. Thunderbird</title><content type='html'>I generally prefer message forums over mailing lists for community discussions because of the better separation of topics and implicit threading. Not that forums are ideal for threads, since they generally cannot easily spawn sub-threads.

&lt;p&gt;So, when reading discussions on mailing lists, I try to use the threading mode of the mail client to bring some clarity to the discussion and filter out discussions I don't care about. I am a big Imap proponent and read mail on PC with Thunderbird and Mail.app on Mac. Overall i like Mail.app better but sometimes it does exhibit the Mac app tendency of "if you don't like the way we do things, well, sucks for you", vs. Thunderbird's more liberal configurability.

&lt;p&gt;I'll leave the whole subject of how well clients group messages into threads.... Ok, just one stab at that subject. Subject of "Hey" is not too uncommon. So all messages of "Hey", which are almost guaranteed to be unrelated, get lumped into a thread together. Happens in both readers. I know that threading in mail is ad-hoc, so I am not faulting the clients. It's just a silly artifact.

&lt;p&gt;But here's some behavior that I find not only unintuitive but downright tedious:

&lt;p&gt;On both Mail.app and Thunderbird, i can collapse and expand threads by using left and right arrows. So when I decide that a discussion is not of interest to me, i just collapse the thread and hit delete. On Mail.app, the thread is deleted, as I'd expect. On Thunderbird, the current message in the thread is deleted and the next message becomes the head of the thread. So to delete a thread i have to open the thread, select all messages manually and then delete. Bah!

&lt;p&gt;I've dug around the config and even advanced config, but can't find a way to change this behavior.</content><link rel='alternate' type='text/html' href='http://www.claassen.net/geek/blog/2007/11/threading-mailapp-vs-thunderbird.html' title='Threading: Mail.app vs. Thunderbird'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9991642&amp;postID=842710532044374529' title='1 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.blogger.com/feedsindex.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/842710532044374529'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/842710532044374529'/><author><name>ether</name><uri>http://www.blogger.com/profile/16911611449176929088</uri><email>noreply@blogger.com</email></author></entry><entry><id>tag:blogger.com,1999:blog-9991642.post-4834172159579663086</id><published>2007-11-20T09:14:00.000-08:00</published><updated>2007-11-22T10:16:06.819-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Extension method'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='fluent interface'/><title type='text'>Stupid ExtensionMethod tricks</title><content type='html'>I have yet to decide whether &lt;a href="http://weblogs.asp.net/scottgu/archive/2007/03/13/new-orcas-language-feature-extension-methods.aspx"&gt;Extension Methods&lt;/a&gt; in C# are a boon or bane. I've already several times, been frustrated by Intellisense not showing me a method that was legal somewhere else, until I could figure out what using statement brought that extension method into scope. On one hand Extension Methods can be used to simplify code, on the other, I see them as the source of much confusion as they become popular.

&lt;p&gt;Worse yet, they have potential for more about than the blink tag, imho.

&lt;p&gt;The one place I see extension methods being instrumental is in defining &lt;a href="http://martinfowler.com/bliki/FluentInterface.html"&gt;fluent interfaces&lt;/a&gt;, yet another practice I have yet to decide whether I am in favor of or not. Partially, because I don't see them as intrinsically easier to read. Partially because they allow for much syntactic abuse.

&lt;p&gt;So today, I created a fluent interface for an operation that I wish was just support in the language in the first place -- the between operator. It exists in some SQL dialects and is a natural part of so many math equations. I wish I could just write:

&lt;div id="csharpcode"&gt;
&lt;pre class="csharpcode"&gt;
&lt;span class="kwrd"&gt;if&lt;/span&gt;( 0.5 &amp;lt; x &amp;lt; 1.0 )
{
  &lt;span class="rem"&gt;// do something&lt;/span&gt;
}&lt;/pre&gt;
&lt;/div&gt;

Instead, I'll settle for this:

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
&lt;span class="kwrd"&gt;if&lt;/span&gt;( x.Between(0.5).And(1.0) )
{
  &lt;span class="rem"&gt;// do something&lt;/span&gt;
}&lt;/pre&gt;
&lt;/div&gt;

The first part is easy, it's just an Extension Method on double. And if I just had it take the lower and upper bound, then we would have been done. But this is where the fluent interface bug bites me and I want to say &lt;code&gt;And&lt;/code&gt;. This means, that Between can't return a boolean. It needs to return the result of the lower bound test and the value to be tested. That means that &lt;code&gt;Between&lt;/code&gt; returns a helper class, which has one method &lt;code&gt;And&lt;/code&gt;, which finally returns the boolean value.

&lt;div id="csharpcode"&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; DoubleExtensions
{
  
  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; BetweenHelper Between(&lt;span class="kwrd"&gt;this&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; v, &lt;span class="kwrd"&gt;double&lt;/span&gt; lower)
  {
    &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; BetweenHelper(v &amp;gt; lower, v);
  }

  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;struct&lt;/span&gt; BetweenHelper
  {
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; passedLower;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; v;

    &lt;span class="kwrd"&gt;internal&lt;/span&gt; BetweenHelper(&lt;span class="kwrd"&gt;bool&lt;/span&gt; passedLower, &lt;span class="kwrd"&gt;double&lt;/span&gt; v)
    {
      &lt;span class="kwrd"&gt;this&lt;/span&gt;.passedLower = passedLower;
      &lt;span class="kwrd"&gt;this&lt;/span&gt;.v = v;
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; And(&lt;span class="kwrd"&gt;double&lt;/span&gt; upper)
    {
      &lt;span class="kwrd"&gt;if&lt;/span&gt; (passedLower &amp;amp;&amp;amp; v &amp;lt; upper)
      {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;true&lt;/span&gt;;
      }
      &lt;span class="kwrd"&gt;else&lt;/span&gt;
      {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;false&lt;/span&gt;;
      }
    }
  }
}&lt;/pre&gt;&lt;/div&gt;

That's a lot of code for a simple operation and it's still questionable whether it really improves readability. But it is a common enough operation if you have a lot of bounds checking, that it might be worth throwing into a common code dll. I've yet to make up my mind, I mostly did it because i wanted to play with the syntax.</content><link rel='alternate' type='text/html' href='http://www.claassen.net/geek/blog/2007/11/stupid-extensionmethod-tricks.html' title='Stupid ExtensionMethod tricks'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9991642&amp;postID=4834172159579663086' title='1 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.blogger.com/feedsindex.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/4834172159579663086'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/4834172159579663086'/><author><name>ether</name><uri>http://www.blogger.com/profile/16911611449176929088</uri><email>noreply@blogger.com</email></author></entry><entry><id>tag:blogger.com,1999:blog-9991642.post-1366734100281791724</id><published>2007-11-03T11:47:00.001-07:00</published><updated>2007-11-03T11:54:22.544-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='activesync'/><category scheme='http://www.blogger.com/atom/ns#' term='vmware fusion'/><category scheme='http://www.blogger.com/atom/ns#' term='stdtime'/><category scheme='http://www.blogger.com/atom/ns#' term='macbook'/><title type='text'>One week of Windows on Macbook</title><content type='html'>Spent this past week doing development in XP under VMWare Fusion. I hooked up a windows keyboard and mouse when I'm stationary and when in the Windows Space, there was no way to tell that wasn't on a native machine. I actually had a harder time on the Mac side, since I had to remeber to hit the Windows key to get the normal Apple-Command behavior.

The latest thing I tried, that I seriously didn't expect to work was hook up my HTC Apache. As much trouble as I've had with ActiveSync on my desktop machine, I figured that either the USB connection wouldn't even be seen by Windows or that ActiveSync just bombed. But instead, ActiveSync found the phone and synced everything. Now I even have &lt;a href="http://www.stdtime.com/"&gt;Standard Time&lt;/a&gt; with me on a laptop instead of my old setup of Desktop and phone for time tracking.</content><link rel='alternate' type='text/html' href='http://www.claassen.net/geek/blog/2007/11/one-week-of-windows-on-macbook.html' title='One week of Windows on Macbook'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9991642&amp;postID=1366734100281791724' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.blogger.com/feedsindex.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/1366734100281791724'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/1366734100281791724'/><author><name>ether</name><uri>http://www.blogger.com/profile/16911611449176929088</uri><email>noreply@blogger.com</email></author></entry><entry><id>tag:blogger.com,1999:blog-9991642.post-2402417491702500666</id><published>2007-10-29T21:43:00.000-07:00</published><updated>2007-10-29T22:29:22.856-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='VS.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='boot camp'/><category scheme='http://www.blogger.com/atom/ns#' term='vmware fusion'/><category scheme='http://www.blogger.com/atom/ns#' term='macbook'/><title type='text'>My new Visual Studio Dev workhorse: Macbook Pro</title><content type='html'>I'm just starting some new contract work that requires a bit more on-site than usual and instead of syncing up my various dev environments all the time, I decided that it's time for a new laptop. My current laptop is a 15" Powerbook G4. It's been a great machine, but for the  past couple of years it's mostly been an expensive Email/Browser appliance with the occasional Eclipse/Java diversion.

&lt;p&gt;This time I needed something to let me do VS.NET development and all the other MS related things that come across my path. Now, obviously my previous laptop choice shows i'm not impartial, but I certainly wasn't going to settle for less that what I had. I went through this last time as well and the conclusion is not only the same, but decidedly more in favor of a Macbook this time around.

&lt;p&gt;Go ahead, try it.. Find a laptop like this: slim profile, powerful CPU, large widescreen LCD, light and sturdy. By the time you find the PC equivalents, you don't get to be cheaper. And for some reason, PCs still come mostly in two flavors: 1) light and small screen or low power and 2) giant hulking powerhouses that are not that much more convenient than the original PC Portable's. Add to that, that I have yet to find another laptop that comes with a sturdy shell like the Macs -- If you look at some of the dents my old Powerbook has sustained, then you'd realize that I would have cracked open any plastic shelled laptop a long time ago.

&lt;p&gt;Ok, enough with the rationalization already, buy your Mac, be a fanboy. Don't come whining when it can't cut it as your dev machine...

&lt;p&gt;I have to admit that the last 3 days have included more reboots than I'd care to admit, but I finally have everything configured just right and I don't mind saying that this rig is freakin' sweet!

&lt;p&gt;The setup is as follows: 15" 2.4GHz Core Duo Macbook Pro w/ 2GB RAM &amp; 160MB HD. 140GB Leopard partition. 20GB bootcamp partition (short-sighted mistake on sizing) with Win XP Pro, Visual Studio Orcas, etc. Vmware Fusion 1.1 Beta running boot camp partition as VPC image under Leopard.

&lt;p&gt;So what pitfalls have I come across?

&lt;h2&gt;Bootcamp was a pain to get going&lt;/h2&gt;
Vmware fusion was just about the most painless windows install I've ever done. It asked me for the key before it started and took care of everything until it booted into XP. Bravo!

&lt;p&gt;Boot camp on the other hand complained about my disk being corrupt (Brand new mac, mind you). Rebooted from CD, ran disk repair, was told there was nothing to repair. Tried boot camp again. Success! Started XP install on the formatted partition that boot camp set up. Got to the reboot early in the install, Mac rebooted, complained it couldn't boot from CD and the HD had errors, please press any key, but no keys produced results. Hard rebooted, ejected CD, tried to restart install via bootcamp, but same problem. Removed bootcamp partition, started over, this time manually formatting the disk during install to be sure. Some more issues aside, finally boot camp installed. Only then do I find out that VMware Fusion can use the boot camp partition as a virtual image. Now that's useful. Except I had sized it as the emergency fall back windows install. Doh.. Well, I'll just mount the Mac disk on the windows side for storage. Then all my files are inside the FileVault as well.

&lt;h2&gt;XP Activation with bootcamp/vmware fusion&lt;/h2&gt;
After finding out I could use a single install as a full native and a virtual instance, I was thrilled and started up the bootcamp partition. VMware had to tweak it some, but it came up. XP Activation got invalidated because I apparently changed the hardware too much. Same happened when I rebooted into XP directly. Now it seems ok, but Redmond has received about 4 activations from the same XP install in 2 days. No, I'm not frantically installing on all my friend's PCs, I'm just trying to get one install stable.

&lt;h2&gt;Vmware Fusion Unity and Leopard Spaces have some issues&lt;/h2&gt;
Now, I have no right to expect something as funky as Unity to work with an OS feaure that was released 4 days ago, so I'm not really bitching, just warning. For me using Unity and Spaces caused spaces to switch back and forth automatically at about twice a second several times when I had two Win windows in different spaces. And my Mac apps all lost their windows. So for now, XP runs fullscreen in its own Space and it's all wonderfully stable.

&lt;h2&gt;WPF likes hardware acceleration&lt;/h2&gt;
So in the middle of the night I wake up in a panic. Everything was working so wonderfully, but had I missed something? Well, I kept saying &lt;i&gt;"i don't care that virtualization doesn't support the GPU, i'm not planning to play games on this machine"&lt;/i&gt;. Hahaha.. But what about WPF? It uses the GPU to render all that fancy vector goodness! Did I just buy another email appliance? I fired up some WPF samples and it worked fine. As they got fancier, things got a bit choppier and the 3D was a slideshow. But work, it did. So, WPF gracefully falls back to software only mode. I rebooted into native XP and WPF was running in all its glory. Yay, all is good.

&lt;p&gt;Ok, this is day 3 with my new rig and I'm very happy on both the Mac and Windows side. I even have a single dev machine that can test all browsers currently supported by Silverlight. And once Moonlight drops, I'll just fire up a VM of Fedora and cover that use case as well. Let's see if the euphoria lasts.</content><link rel='alternate' type='text/html' href='http://www.claassen.net/geek/blog/2007/10/my-new-visual-studio-dev-workhorse.html' title='My new Visual Studio Dev workhorse: Macbook Pro'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9991642&amp;postID=2402417491702500666' title='2 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.blogger.com/feedsindex.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/2402417491702500666'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9991642/posts/default/2402417491702500666'/><author><name>ether</name><uri>http://www.blogger.com/profile/16911611449176929088</uri><email>noreply@blogger.com</email></author></entry></feed>