<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" gd:etag="W/&quot;C0YBRXk4eSp7ImA9WhVUE08.&quot;"><id>tag:blogger.com,1999:blog-1664217632350055880</id><updated>2012-05-18T00:05:54.731-06:00</updated><category term="SOLID" /><category term="Personal" /><category term="Rx Framework" /><category term="Vista" /><category term="Visual Studio" /><category term="ASP.NET Providers Series" /><category term="Microsoft" /><category term="Mocking" /><category term="SQL" /><category term="MSP" /><category term="Virtual Store" /><category term="GUID" /><category term="Samba" /><category term="Dependency Injection" /><category term="Nothin But .NET" /><category term="BizTalk" /><category term="Prerequisites" /><category term="UI" /><category term="Build" /><category term="Encryption" /><category term="Windows" /><category term="Security" /><category term="MSI" /><category term="ISAPI" /><category term="Webinar/Screencast" /><category term="ASP.NET" /><category term="Web" /><category term="Testing" /><category term="Quote" /><category term="Anagram Code Kata Series" /><category term="Deployment and Installers" /><category term="Winbind" /><category term="Orca" /><category term="Community" /><category term="PowerShell" /><category term="BDD" /><category term="Patching/Updating" /><category term="Macro" /><category term="Isolated Storage" /><category term="Humor" /><category term="Error" /><category term="Bootstrapper" /><category term="Events" /><category term="Virtual Machine" /><category term="Threading" /><category term="LINQ" /><category term="Windows Forms" /><category term="Apprenticeships" /><category term="jQuery" /><category term="VMWare" /><category term="MSpec" /><category term="Office" /><category term="MVC Pattern" /><category term="Monads" /><category term="C/C++" /><category term="XML" /><category term="Recursion" /><category term="PAM" /><category term="F#" /><category term="FubuMVC" /><category term="Kerberos" /><category term="MSBuild" /><category term="VB" /><category term="Java" /><category term="Algorithms" /><category term="Registry" /><category term="IIS" /><category term="Add/Remove Programs" /><category term="Develop With Passion" /><category term="Delegates" /><category term="C#" /><category term="New Technology" /><category term="Conferences" /><category term="Behavior Chains" /><category term="News and Politics" /><category term="Functional Programming" /><category term="Active Directory" /><category term="Database" /><category term="Launch Condition" /><category term="Linux" /><category term="Code Organization" /><category term="Maybe Monad" /><category term="Server 2008" /><category term="JavaScript" /><category term="Entity Framework" /><category term=".NET" /><category term="Silverlight" /><title>Murray on .NET</title><subtitle type="html">If only someone else had posted about this stuff first...</subtitle><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://murrayon.net/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://murrayon.net/" /><link rel="next" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default?start-index=26&amp;max-results=25&amp;redirect=false&amp;v=2" /><author><name>Michael Murray</name><uri>https://profiles.google.com/109073345657761090655</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-Xf7Ccmz4FCQ/AAAAAAAAAAI/AAAAAAAAAGU/jOfxxMc1Lxo/s512-c/photo.jpg" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>72</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/MurrayOnNET" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="murrayonnet" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry gd:etag="W/&quot;CU8AQn48eSp7ImA9WhdSEko.&quot;"><id>tag:blogger.com,1999:blog-1664217632350055880.post-8879570940824310294</id><published>2011-07-21T12:44:00.001-06:00</published><updated>2011-07-21T12:44:03.071-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-07-21T12:44:03.071-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Web" /><category scheme="http://www.blogger.com/atom/ns#" term="New Technology" /><category scheme="http://www.blogger.com/atom/ns#" term="Behavior Chains" /><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET" /><category scheme="http://www.blogger.com/atom/ns#" term="Webinar/Screencast" /><category scheme="http://www.blogger.com/atom/ns#" term="FubuMVC" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="Develop With Passion" /><title>FubuMVC Webinar - Compositional and Convention-based Web Development</title><content type="html">&lt;p&gt;I just gave a webinar yesterday for &lt;a href="http://www.jpboodhoo.com/"&gt;JP Boodhoo’s&lt;/a&gt; &lt;a href="http://webinars.developwithpassion.com/"&gt;Develop With Passion® webinar series&lt;/a&gt; on Compositional and Convention-based Web Development with &lt;a href="http://fubumvc.com/"&gt;FubuMVC&lt;/a&gt;. The recorded session is now posted online with a slew of referenced and related links. I apologize for the sound quality and will make corrections if I get another chance to do another webinar/screencast. Here is the link to the recording:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://webinars.developwithpassion.com/webinars/3"&gt;http://webinars.developwithpassion.com/webinars/3&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;I didn’t mention this in the screencast but before we began switching our company’s production website to FubuMVC, we first devised a way to get the benefit of &lt;a href="http://murrayon.net/2011/06/fubumvc-behavior-chains-bmvc-pattern.html"&gt;FubuMVC Behavior Chains&lt;/a&gt; while still on the ASP.NET MVC framework. Please feel free to read more about that successful venture at &lt;a href="http://paceyourself.net/"&gt;Bob Pace’s&lt;/a&gt; &lt;a href="http://paceyourself.net/2011/06/11/mvctofubu---transition-your-website-from-aspnet-mvc-to-fubumvc-part-1/"&gt;blog post on the MvcToFubu project&lt;/a&gt;.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1664217632350055880-8879570940824310294?l=murrayon.net' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://murrayon.net/feeds/8879570940824310294/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://murrayon.net/2011/07/fubumvc-webinar-compositional-and.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/8879570940824310294?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/8879570940824310294?v=2" /><link rel="alternate" type="text/html" href="http://murrayon.net/2011/07/fubumvc-webinar-compositional-and.html" title="FubuMVC Webinar - Compositional and Convention-based Web Development" /><author><name>Michael Murray</name><uri>https://profiles.google.com/109073345657761090655</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-Xf7Ccmz4FCQ/AAAAAAAAAAI/AAAAAAAAAGU/jOfxxMc1Lxo/s512-c/photo.jpg" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;CEMNQ3g5fSp7ImA9WhZbEE0.&quot;"><id>tag:blogger.com,1999:blog-1664217632350055880.post-5686375064925838226</id><published>2011-06-13T16:01:00.001-06:00</published><updated>2011-06-13T16:01:32.625-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-06-13T16:01:32.625-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Web" /><category scheme="http://www.blogger.com/atom/ns#" term="Behavior Chains" /><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET" /><category scheme="http://www.blogger.com/atom/ns#" term="FubuMVC" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="MVC Pattern" /><category scheme="http://www.blogger.com/atom/ns#" term="Code Organization" /><title>FubuMVC Behavior Chains - The BMVC Pattern</title><content type="html">&lt;p&gt;For the last several months at work, we have been gradually migrating our production website to use the .NET open source &lt;a href="http://fubumvc.com/" target="_blank"&gt;FubuMVC web framework&lt;/a&gt;. Before committing to a whole new infrastructure for our site, some brilliant talent on our team decided to improve our experience on the ASP.NET MVC framework by integrating in one of FubuMVC’s most compelling features, behavior chains. &lt;a href="https://twitter.com/#!/bob_pace" target="_blank"&gt;Bob Pace&lt;/a&gt; originally blogged about the process in a two part blog post series (&lt;a href="http://paceyourself.net/2010/08/06/integrating-fubumvc-with-aspnet-mvc-part-1/" target="_blank"&gt;part 1&lt;/a&gt; and &lt;a href="http://paceyourself.net/2010/08/13/integrating-fubumvc-with-aspnet-mvc-part-2/" target="_blank"&gt;part 2&lt;/a&gt;); since then, he has &lt;a href="https://github.com/bobpace/MvcToFubu" target="_blank"&gt;created a packaged solution called MvcToFubu&lt;/a&gt; which he &lt;a href="http://paceyourself.net/2011/06/11/mvctofubu---transition-your-website-from-aspnet-mvc-to-fubumvc-part-1/" target="_blank"&gt;just recently started another blog post series about&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Behavior chains are by far my favorite feature of FubuMVC. I believe they single-handedly revolutionized our use of the MVC pattern in a web environment. In fact, I like to think of it as a whole new pattern, the &lt;strong&gt;BMVC Pattern&lt;/strong&gt; (I was told by a coworker that the ‘B’ should stand for “Better”, and while that’s true, I’ll stick with “Behaviors”). No, I’m not sincerely trying to coin yet another design or architectural pattern buzzword. However, I am most sincerely not joking when I say behavior chains add a whole new dimension to how I design web architectures. I find myself creating composable pipelines of cohesive, testable components in lieu of monolithic controllers and actions.&lt;/p&gt; &lt;span class="fullpost"&gt;   &lt;h3&gt;What are Behavior Chains?&lt;/h3&gt;    &lt;p&gt;Have you ever looked at your controller actions and thought “Man, there’s a lot going on here!” (validation, authorization, transactions, ORM setup, logging, caching, etc.)? If you haven’t, I urge you to take a conscious look at your controller actions and try to identify code that doesn’t &lt;u&gt;directly&lt;/u&gt; relate to the purpose of the page (or sub-view of the page). Shouldn’t there be a way to move some of this secondary infrastructure code out of our controller actions and have it be applied conventionally to the correct actions/routes? Yes, and this is exactly what Behavior Chains are meant to accomplish, the ability to create composable pipelines of reusable behavior nodes for each route in your site.&lt;/p&gt;    &lt;p&gt;Behavior chains can be built up in any order you want for each route via Conventions and Policies, which are extension points that allow you to inspect and modify the entire Behavior Graph that was built by FubuMVC during startup bootstrapping. The behavior graph is the modifiable collection of all behavior chains in the site, initially built with just a few default behavior nodes for each route. A typical behavior chain by default usually has an ActionCall node (representing the call to a controller action) and an Output node (often a tie-in to a view rendering engine, but can also be JSON or anything else). Other behavior nodes can be sprinkled into the pipeline in any order in front of or in between the ActionCall and the Output nodes. As mentioned before via the use of Conventions and Policies, it is common practice to filter your list of behavior chains using LINQ extension methods and then add a specific behavior node into the matching chains at a specific location in the chain. This truly elevates coding to conventions to a new level; some of the filtering criteria regarding controller actions that you may come up with could be as follows:&lt;/p&gt;    &lt;ul&gt;     &lt;li&gt;Action method names that contain a certain word. &lt;/li&gt;      &lt;li&gt;Actions methods that are located in a particular controller or namespace. &lt;/li&gt;      &lt;li&gt;Action method signatures that contain an input/output model that implements a specific interface. &lt;/li&gt;      &lt;li&gt;Action method signatures that contain an input/output model that contains a property of a certain type or have a particular C# attribute applied to it. &lt;/li&gt;      &lt;li&gt;So on, so forth…whatever you can dream up! &lt;/li&gt;   &lt;/ul&gt;    &lt;p&gt;One more aspect of behavior chains that is important to point out is the logistics in how behavior nodes are chained together. The chains are not merely a linked-list of behavior nodes that are traversed linearly. Instead the makeup of the chains is better conceptualized to be an outer behavior node wrapping around an inner behavior node, which is then itself an outer node wrapped around another inner node, and so on. In fact, the FubuMVC developers like to refer to this chaining architecture as the Onion Layer Model or the Russian Doll Model (conjuring up a mental image of the &lt;a href="http://en.wikipedia.org/wiki/Matryoshka_doll"&gt;nesting Russian doll toy called a matryoshka doll or a babushka doll&lt;/a&gt;). When an outer behavior node has finished executing it’s logic, it can then optionally execute it’s inner behavior node logic (or stop the behavior chain entirely in order to return early with a response, an error, or even transfer to a different route and behavior chain). One of the key advantages that this wrapping architecture can afford is the ability for an outer behavior to run additional logic or clean up code after all inner behavior nodes have completed (i.e., transactions can be committed, connections can be closed, sessions can be disposed, etc.).&lt;/p&gt;    &lt;h3&gt;How does this change the way I code my controllers?&lt;/h3&gt;    &lt;p&gt;I find that my controller actions are much thinner and more focused on the base case and the successful code path. All the exceptional code paths and edge cases have been identified and deal with by the other behavior nodes in the chain, prior to my action call being invoked. My action call can now focus on the essential logic that differentiate this route from others. That essential logic doesn’t get lost anymore in the necessary infrastructure code that should be a secondary concern. And all of this logic can be broken up into cohesive, reusable components within a flexible request/response pipeline that can be built up automatically by coding to whatever conventions you deem beneficial. Behavior chains really do add a whole new first-class citizen to the MVC design pattern. Do yourself a favor and check out the MvcToFubu solution mentioned at the top of this post, and bring some FubuMVC behavior chain goodness to your ASP.NET MVC website. I think you might be as impressed as we were and yearn for &lt;a href="http://lostechies.com/chadmyers/2011/05/30/cool-stuff-in-fubucore-and-fubumvc-series/"&gt;the other killer features of FubuMVC&lt;/a&gt; as well.&lt;/p&gt; &lt;/span&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1664217632350055880-5686375064925838226?l=murrayon.net' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://murrayon.net/feeds/5686375064925838226/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://murrayon.net/2011/06/fubumvc-behavior-chains-bmvc-pattern.html#comment-form" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/5686375064925838226?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/5686375064925838226?v=2" /><link rel="alternate" type="text/html" href="http://murrayon.net/2011/06/fubumvc-behavior-chains-bmvc-pattern.html" title="FubuMVC Behavior Chains - The BMVC Pattern" /><author><name>Michael Murray</name><uri>https://profiles.google.com/109073345657761090655</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-Xf7Ccmz4FCQ/AAAAAAAAAAI/AAAAAAAAAGU/jOfxxMc1Lxo/s512-c/photo.jpg" /></author><thr:total>2</thr:total></entry><entry gd:etag="W/&quot;DU8GQXg5eyp7ImA9WhZUEU4.&quot;"><id>tag:blogger.com,1999:blog-1664217632350055880.post-245020150771598586</id><published>2011-06-03T15:50:00.001-06:00</published><updated>2011-06-03T15:50:20.623-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-06-03T15:50:20.623-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Apprenticeships" /><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="Community" /><title>.NET Needs More Apprenticeships</title><content type="html">&lt;p&gt;It came to my attention recently that there are a few companies in other dev circles with all-star developers who give back to their communities by doing apprenticeship programs or open door programs at their workplaces. Basically the idea is to let people come into your workplace and pair program with them, sharing knowledge and best practices. I believe we need more of this sense of community going on in the .NET space. I’m sure I’ve probably overlooked something (or perhaps I’m about to spout off craziness and heresies), but it just seems like we should be able to work out the logistics to make this a reality.&lt;/p&gt; &lt;span class="fullpost"&gt;   &lt;p&gt;The interesting thing is that this really isn’t a new concept in other professions. For example, the culinary world has had the idea of &lt;a href="http://en.wikipedia.org/wiki/Stage_(cooking)"&gt;staging&lt;/a&gt; for quite a while. Sometimes it’s a final part of an interview process and other times it’s just to swap techniques and recipes. As mentioned above, I was recently informed of a few dev companies that have taken this idea to our profession. In the Rails community, &lt;a href="http://hashrocket.com/people/work-at-hashrocket/apprentice"&gt;Hashrocket has an apprenticeship program&lt;/a&gt; where you can apply to be a part of their team for a week and soak in some of the awesome skills and techniques they possess. It appears they vet their candidates a bit to ensure they get the right kind of people coming in who are motivated and ready to learn, and also because the code being worked on is for real Hashrocket projects for their clients. In the Node.js space, &lt;a href="http://joyeur.com/2011/04/05/events-ssjs-office-hours-and-codeconf/"&gt;Joyent has a program called Office Hours&lt;/a&gt; where devs can register for free in advance to bring in something to work on with a Joyent employee at end of work day and get their feedback &amp;amp; expert advice. They provide snacks, drinks, and a comfortable work environment.&lt;/p&gt;    &lt;p&gt;I think these ideas are brilliant and foster real community among developers. Although this idea alone can improve the interview and hiring process, I think it can go further than that to help our profession mature in the sharing of best practices and proven techniques. For most .NET companies out there, I’m willing to bet there are few legitimate roadblocks that would prevent instituting programs like these. Being completely honest with yourself, how much of what you work on each day is top secret intellectual property that is core to the success of your company and absolutely cannot be seen by outsiders? Even if there are parts of your code that should not be seen by strangers, I’m sure there are projects and tasks to be done on the other parts of code that will still prove instructive for your invited guests. I encourage our dev community to do whatever it takes to make this happen: have the visitors sign non-disclosure agreements, interview and re-interview potential candidates to meet satisfactory requirements, don’t allow them to hook up thumb drives to your machine and steal your secrets, whatever! It doesn’t matter if it’s a week-long thing or a few hours; let’s realize we’re all in this together and build this community up to have a higher standard of software craftsmanship.&lt;/p&gt; &lt;/span&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1664217632350055880-245020150771598586?l=murrayon.net' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://murrayon.net/feeds/245020150771598586/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://murrayon.net/2011/06/net-needs-more-apprenticeships.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/245020150771598586?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/245020150771598586?v=2" /><link rel="alternate" type="text/html" href="http://murrayon.net/2011/06/net-needs-more-apprenticeships.html" title=".NET Needs More Apprenticeships" /><author><name>Michael Murray</name><uri>https://profiles.google.com/109073345657761090655</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-Xf7Ccmz4FCQ/AAAAAAAAAAI/AAAAAAAAAGU/jOfxxMc1Lxo/s512-c/photo.jpg" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;D08ERH05eCp7ImA9WhZVGEg.&quot;"><id>tag:blogger.com,1999:blog-1664217632350055880.post-2771658254176201795</id><published>2011-05-31T09:27:00.000-06:00</published><updated>2011-05-31T09:30:05.320-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-05-31T09:30:05.320-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="BDD" /><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="MSpec" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="Visual Studio" /><category scheme="http://www.blogger.com/atom/ns#" term="Microsoft" /><category scheme="http://www.blogger.com/atom/ns#" term="Code Organization" /><category scheme="http://www.blogger.com/atom/ns#" term="Nothin But .NET" /><category scheme="http://www.blogger.com/atom/ns#" term="Develop With Passion" /><category scheme="http://www.blogger.com/atom/ns#" term="SOLID" /><title>Develop with Passion bootcamp course - Denver 2011</title><content type="html">&lt;p&gt;&lt;a href="http://paceyourself.net/" target="_blank"&gt;A very talented coworker&lt;/a&gt; and I recently went to JP Boodhoo’s &lt;a href="http://www.jpboodhoo.com/training.oo" target="_blank"&gt;Develop with Passion bootcamp training course&lt;/a&gt; (previously known as Nothin but .NET). I had originally heard of the course from &lt;a href="http://www.davesquared.net/" target="_blank"&gt;Dave Tchepak&lt;/a&gt; who &lt;a href="http://www.davesquared.net/2009/08/nothin-but-net-sydney-2009-day-1.html" target="_blank"&gt;attended the course nearly two years ago&lt;/a&gt;. This blog post is a wrap-up of my thoughts and feelings about the course itself, &lt;a href="http://www.jpboodhoo.com/" target="_blank"&gt;JP Boodhoo&lt;/a&gt; as the instructor &amp;amp; experienced craftsman, and the .NET development community in general.&lt;/p&gt; &lt;span class="fullpost"&gt;   &lt;p&gt;My prior impressions of the course from what I had read was that I would be in awe of JP's coding and design skills and very overwhelmed with the feeling that I didn't really know how to code; also that I would get no sleep at all that week, going from early morning to past midnight every day. I had also heard from a previous course member (off the record) that JP’s organization and direction with course material was a bit disorganized back then (luckily this has changed significantly since he had taken the course). I was really excited to see some good BDD in action, get my TDD passion ignited again (I had actually mentioned on twitter recently that my motivation for test was waning as of late), and see JP's mad Vim skills (since I have gotten into that the last few months).&lt;/p&gt;    &lt;p&gt;Overall, I was pleasantly surprised with the fulfillment of most of my positive expectations and initial impressions going into the course. JP's personality and passion are infectious, and his teaching skills are pretty top-notch. The course material was well-structured and there was a clear direction for what we hoped to cover throughout the week (and yet, he was still flexible enough to be able to briefly entertain an off-topic direction the students wanted to take the course). He stayed true to the advertised 9am-9pm every day no matter what. He said that when he did the late nighters with previous courses, people came back the next day with little sleep and trashed as far as coding &amp;amp; learning capabilities go.&lt;/p&gt;    &lt;p&gt;Watching him code so efficiently is quite a treat. He automates anything he does more than once so that a particular task doesn’t ever get in his way and hold him back the next time. His &lt;a href="http://www.autohotkey.com/" target="_blank"&gt;AutoHotkey&lt;/a&gt; scripts and automation tools that he has put together are absolutely incredible. He briefly touched the mouse two times the entire week, and it only for when one of his programs or Windows VM was wigging out. Screens and dialogs flew by so fast, you could tell he wasn't going to let any UI (even Visual Studio) get in the way of his productivity to code. He mentioned he will be open sourcing some of his scripts that he uses to keep his development environment consistent across OS platforms. Definitely cool stuff.&lt;/p&gt;    &lt;p&gt;As far as JP's actual coding/design skills, I am absolutely floored. He certainly understands C# and .NET well. But it's his Object-Oriented design skills that blew us away. I've realized I don't really know OO design. I think too mechanically about the implementation of the solution instead of focusing on breaking down the problem into smaller manageable chunks. His TDD skills (both Top Down Design and Test Driven Development) are amazing. I've never been a fan of the TDD samples and intros I've seen (where the instructors do the absolute dumbest thing to make the test pass; JP admitted that style didn’t jive with him either). I've always thought that Behavior Driven Development and Top-Down Design were the way to go if I were to have tests drive my designs (or have tests at all for that matter; I'm still having trouble finding enthusiasm for tests written after development for modules and units). So it was great to see a master at that particular craft. It made me excited to use tests as a tool to guide my OO design, behavior discovery, and problem solving.&lt;/p&gt;    &lt;p&gt;Some of you may recall that I had &lt;a href="http://murrayon.net/2009/11/anagram-code-kata-bdd-mspec.html" target="_blank"&gt;previously investigated BDD, Machine.Specifications, and Top-Down Design a year and a half ago doing the Anagram Code Kata&lt;/a&gt;. I think I can recognize better design and responsibility break-up and assignment nowadays. I likely will blog on these subjects again this year with new &amp;amp; improved passion and knowledge.&lt;/p&gt;    &lt;p&gt;There was one sad takeaway for me personally from the course, which was a sense of disappointment in the .NET development community in general. Giving myself some time since the course to really think it through, I’m realizing I’m probably being too tough on our community, as I know each Dev community out there suffers from it’s own set of problems. But I don’t want to excuse our shortcomings either; rather, I would like to point them out so that we can focus on them and root those weaknesses out. I think our community in specific is unique in that there is a big shot company that dictates the direction our community should take, sometimes taking credit for all the innovation. Things are improving lately no doubt; it appears Microsoft is listening more and more and trying to let outsiders get involved and contribute.&lt;/p&gt;    &lt;p&gt;But we as .NET devs need to step up and take matters into our own hands, we need to take better control of the code we write and encourage best practices within the realms of our responsibilities and influence. I believe many .NET shops rely on the tools sent down from above so heavily that we don’t really solve problems anymore. JP’s focus on problem solving and code katas from the course attack this deficiency at its core. I do somewhat blame our university Computer Science programs for not teaching real OO (a great discussion on formalism versus hermeneutics in a book I highly recommend, &lt;a href="http://www.amazon.com/Object-Thinking-DV-Microsoft-Professional-David/dp/0735619654" target="_blank"&gt;Object Thinking&lt;/a&gt;), but at some point we have to take what we were given and no matter our current standing, focus on sharpening our skills through practice and continuous learning. It boils down to individual responsibility for our current skill set and progression as a software craftsman. JP’s course does a terrific job of waking you up to that realization and inspiring you to be a better professional at your craft.&lt;/p&gt;    &lt;p&gt;I have a few takeaways from the course that I’ll publicly spell out now. First, I (and hopefully our whole team) need to better understand design patterns and have them become integral to our code discussions. JP was all about efficiency in keystrokes and related dev tasks, and it seems natural that our language and code discussions could benefit from more efficient communication using design pattern terminology to convey a lot about a specific design concept in only a few words. Second, I hope that I (and our team again) can really put our tests to work for us, helping us recognize the problem to be solved or the behavior that needs to be captured, and then guiding us toward a simple solution. The third takeaway is related to that last part, and that is to have simple solutions that are distributed across simple object implementations (focusing on behaviors) that adhere to SOLID principles. It appears the key is to not focus on implementation specifics until the very last responsible moment; if done right, the implementations to create should be so clear cut and trivial, almost spelled right out in the tests themselves. It’s hard to put into words, but it seems crucial to focus first on the collaborators needed while designing code at the higher levels, finding ways to offload as much responsibility as possible (at least that’s what it felt like) onto dependencies that have yet to be designed and implemented. Designing Top-Down in this way gave such simple and elegant solutions that were totally extensible and adhered to the &lt;a href="http://en.wikipedia.org/wiki/Open/closed_principle" target="_blank"&gt;Open/Closed Principle&lt;/a&gt; better than any other code I have ever seen before. Perhaps as I blog more about this, I’ll be able to better put this style of coding into words (especially as I try to better understand it myself; it all seems so magical still!).&lt;/p&gt;    &lt;p&gt;Overall, a great course that I highly recommend to all. JP has mentioned doing a similar course in Ruby, which I personally find especially interesting, given the same design/coding techniques would seem to run against a lot less friction compared to the heavy reliance on interfaces and mocking that C# require.&lt;/p&gt;    &lt;p&gt;Stay tuned as I try to put some of this stuff into practice!&lt;/p&gt; &lt;/span&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1664217632350055880-2771658254176201795?l=murrayon.net' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://murrayon.net/feeds/2771658254176201795/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://murrayon.net/2011/05/develop-with-passion-bootcamp-course.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/2771658254176201795?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/2771658254176201795?v=2" /><link rel="alternate" type="text/html" href="http://murrayon.net/2011/05/develop-with-passion-bootcamp-course.html" title="Develop with Passion bootcamp course - Denver 2011" /><author><name>Michael Murray</name><uri>https://profiles.google.com/109073345657761090655</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-Xf7Ccmz4FCQ/AAAAAAAAAAI/AAAAAAAAAGU/jOfxxMc1Lxo/s512-c/photo.jpg" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;D0AMQXc4eCp7ImA9Wx9SGUs.&quot;"><id>tag:blogger.com,1999:blog-1664217632350055880.post-3118670048214626927</id><published>2010-12-03T15:29:00.004-07:00</published><updated>2010-12-10T00:23:00.930-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-12-10T00:23:00.930-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Error" /><category scheme="http://www.blogger.com/atom/ns#" term="Functional Programming" /><category scheme="http://www.blogger.com/atom/ns#" term="Delegates" /><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="Visual Studio" /><category scheme="http://www.blogger.com/atom/ns#" term="SQL" /><category scheme="http://www.blogger.com/atom/ns#" term="Microsoft" /><category scheme="http://www.blogger.com/atom/ns#" term="LINQ" /><category scheme="http://www.blogger.com/atom/ns#" term="Algorithms" /><title>LINQ Query Syntax and Type Argument Inference</title><content type="html">&lt;p&gt;In LINQ, you can query in-memory data collections using &lt;a href="http://msdn.microsoft.com/en-us/library/bb397947.aspx"&gt;Method Syntax (the &lt;font face="Courier New"&gt;IEnumerable&lt;/font&gt; extension methods) or Query Syntax (the SQL-like, declarative syntactic sugar on top of the underlying method calls)&lt;/a&gt;.&amp;#160; I was using the query syntax to join a few in-memory collections and then project (like a SQL &lt;font face="Courier New"&gt;SELECT&lt;/font&gt;) the denormalized data into a new object that only contained a few pieces of information from each of the separate data collections.&amp;#160; The code could look similar to this (we'll go with a shopping example in honor of the upcoming holiday season):&lt;/p&gt;  &lt;pre class="brush: csharp"&gt;var shoppingLists =
    from person in people
    join wishlist in wishlists on person.Id equals wishlist.personId
    join store in stores on store.Id equals wishlist.storeId
    select new
    {
        PersonName = person.Name,
        StoreName = store.Name,
        NumItems = wishlist.Items.Count
    };&lt;/pre&gt;

&lt;p&gt;However, I ran into the following intellisense red squiggly error in the Visual Studio C# code editor on the second &lt;font face="Courier New"&gt;join&lt;/font&gt; statement:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;font face="Courier New"&gt;The type arguments for method 'IEnumerable&amp;lt;TResult&amp;gt; System.Linq.Enumerable.Join&amp;lt;TOuter,TInner,TKey,TResult&amp;gt;(this IEnumerable&amp;lt;TOuter&amp;gt;, IEnumerable&amp;lt;TInner&amp;gt;, Func&amp;lt;TOuter,TKey&amp;gt;, Func&amp;lt;TInner,TKey&amp;gt;, Func&amp;lt;TOuter,TInner,TResult&amp;gt;)' cannot be inferred from the query.&lt;/font&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The error might also end with a statement that says &amp;quot;&lt;font face="Courier New"&gt;Try specifiying the type arguments explicitly&lt;/font&gt;.&amp;quot;&lt;/p&gt;
&lt;span class="fullpost"&gt;
  &lt;h3&gt;Let’s Take a Closer Look…&lt;/h3&gt;

  &lt;p&gt;The underlying problem is best understood by examining the method signature of the &lt;a href="http://msdn.microsoft.com/en-us/library/bb534675.aspx"&gt;LINQ &lt;font face="Courier New"&gt;Join&lt;/font&gt; extension method&lt;/a&gt; and how the LINQ query syntax for the keyword &lt;font face="Courier New"&gt;join&lt;/font&gt; maps to that method call in the compiler.&amp;#160; The type signature for the extension method is as follows:&lt;/p&gt;

  &lt;pre class="brush: csharp"&gt;public static IEnumerable&amp;lt;TResult&amp;gt; Join&amp;lt;TOuter,TInner,TKey,TResult&amp;gt;(
    this IEnumerable&amp;lt;TOuter&amp;gt; outer,
    IEnumerable&amp;lt;TInner&amp;gt; inner,
    Func&amp;lt;TOuter,TKey&amp;gt; outerKeySelector,
    Func&amp;lt;TInner,TKey&amp;gt; innerKeySelector,
    Func&amp;lt;TOuter,TInner,TResult&amp;gt; resultSelector)&lt;/pre&gt;

  &lt;p&gt;Consistent with all &lt;a href="http://msdn.microsoft.com/en-us/library/bb383977.aspx"&gt;extension methods&lt;/a&gt;, it is &lt;font face="Courier New"&gt;public static&lt;/font&gt; and has a &lt;font face="Courier New"&gt;this&lt;/font&gt; keyword in front of the first method parameter, allowing you to call the &lt;font face="Courier New"&gt;Join&lt;/font&gt; method as if it had been declared in the original definition of the &lt;font face="Courier New"&gt;IEnumerable&amp;lt;&amp;gt;&lt;/font&gt; class or one of its inheriting children classes.&lt;/p&gt;

  &lt;p&gt;The first two parameters are the two data collections being joined, and in the case of my LINQ query above, they would be the joined collection of &lt;font face="Courier New"&gt;people&lt;/font&gt; &amp;amp; &lt;font face="Courier New"&gt;wishlists&lt;/font&gt; and then the &lt;font face="Courier New"&gt;stores&lt;/font&gt; collection, respectively.&lt;/p&gt;

  &lt;p&gt;The next two method parameters take delegates or lambda expressions that select the key value from each collection participating in the join, &lt;font face="Courier New"&gt;outer&lt;/font&gt; collection key selector first and then the &lt;font face="Courier New"&gt;inner&lt;/font&gt; key selector second.&amp;#160; The key values of each item in each collection will compared to each other for equality.&amp;#160; It will use the normal definition of the &lt;font face="Courier New"&gt;Equals&lt;/font&gt; method for your object type (or if you specified a custom one in your class definition), or there is another &lt;font face="Courier New"&gt;Join&lt;/font&gt; extension method signature that takes in another method parameter that an object that conforms to the &lt;font face="Courier New"&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms132151.aspx"&gt;IEqualityComparer&amp;lt;TKey&amp;gt; &lt;font face="Arial"&gt;interface&lt;/font&gt;&lt;/a&gt;&lt;/font&gt;.&amp;#160; The key selectors from our LINQ query sample would be the two member access expressions in the &amp;quot;&lt;font face="Courier New"&gt;on store.Id equals wishlist.storeId&lt;/font&gt;&lt;font face="Arial"&gt;&amp;quot; statement.&lt;/font&gt;&lt;/p&gt;

  &lt;p&gt;The last parameter is a delegate or lambda expression that takes in a item from each participating data collection and projects the items into a new form (like the &lt;font face="Courier New"&gt;Select&lt;/font&gt; LINQ query method and its SQL equivalent).&amp;#160; The two items being passed into the &lt;font face="Courier New"&gt;Func&lt;/font&gt; have passed the equality comparison of each item’s key value, exactly the same as how rows from two tables match up in an &lt;font face="Courier New"&gt;inner join&lt;/font&gt; statement in SQL.&lt;/p&gt;

  &lt;p&gt;As for the &lt;a href="http://msdn.microsoft.com/en-us/library/0zk36dx2.aspx"&gt;generic type parameters&lt;/a&gt; in this extension method definition, &lt;font face="Courier New"&gt;TOuter&lt;/font&gt; and &lt;font face="Courier New"&gt;TInner&lt;/font&gt; are the types from the two collections participating in the join.&amp;#160; &lt;font face="Courier New"&gt;TKey&lt;/font&gt; is the type returned by the key selectors; notice that in order to compare the equality of each key selected, they need to be of the same type.&amp;#160; In our example, &lt;font face="Courier New"&gt;TKey&lt;/font&gt; is likely &lt;font face="Courier New"&gt;int&lt;/font&gt; or &lt;font face="Courier New"&gt;long&lt;/font&gt; (or maybe &lt;font face="Courier New"&gt;Guid&lt;/font&gt;) since we’re dealing with &lt;font face="Courier New"&gt;Id&lt;/font&gt; properties of all of our objects.&amp;#160; &lt;font face="Courier New"&gt;TResult&lt;/font&gt; is the type returned by the projection &lt;font face="Courier New"&gt;Func&amp;lt;TOuter,TInner,TResult&amp;gt; resultSelector&lt;/font&gt;; in our case, we returned an &lt;a href="http://msdn.microsoft.com/en-us/library/bb397696.aspx"&gt;anonymous type&lt;/a&gt; consisting of three properties (&lt;font face="Courier New"&gt;PersonName&lt;/font&gt;, &lt;font face="Courier New"&gt;StoreName&lt;/font&gt;, and &lt;font face="Courier New"&gt;NumItems&lt;/font&gt;).&lt;/p&gt;

  &lt;h3&gt;Ok, So Where Did I Screw Up Then?&lt;/h3&gt;

  &lt;p&gt;To answer this, let’s take a look one more time at my LINQ query syntax, how it would map to LINQ method syntax, and the &lt;font face="Courier New"&gt;Join&lt;/font&gt; method signature again all side-by-side:&lt;/p&gt;

  &lt;pre class="brush: csharp; highlight: [4,19,20]"&gt;var shoppingLists =
    from person in people
    join wishlist in wishlists on person.Id equals wishlist.personId
    join store in stores on store.Id equals wishlist.storeId
    select new
    {
        PersonName = person.Name,
        StoreName = store.Name,
        NumItems = wishlist.Items.Count
    };

// Equivalent LINQ method syntax
var shoppingLists2 = people
    .Join(wishlists,
        person =&amp;gt; person.Id,
        wishlist =&amp;gt; wishlist.personId,
        (person, wishlist) =&amp;gt; new { person, wishlist })
    .Join(stores,
        store =&amp;gt; store.Id,
        something =&amp;gt; something.wishlist.storeId,
        (obj, store) =&amp;gt; new
        {
            PersonName = obj.person.Name,
            StoreName = store.Name,
            NumItems = obj.wishlist.Items.Count
        });

public static IEnumerable&amp;lt;TResult&amp;gt; Join&amp;lt;TOuter,TInner,TKey,TResult&amp;gt;(
    this IEnumerable&amp;lt;TOuter&amp;gt; outer,
    IEnumerable&amp;lt;TInner&amp;gt; inner,
    Func&amp;lt;TOuter,TKey&amp;gt; outerKeySelector,
    Func&amp;lt;TInner,TKey&amp;gt; innerKeySelector,
    Func&amp;lt;TOuter,TInner,TResult&amp;gt; resultSelector)&lt;/pre&gt;

  &lt;p&gt;The issue is lines 19 and 20 above; they need to be switched.&amp;#160; The compiler can’t properly infer the generic type parameters because we have our key selectors out of order in the argument list when invoking the &lt;font face="Courier New"&gt;Join&lt;/font&gt; method.&amp;#160; If you wanted to be explicit about your generic type parameters assignments, then &lt;font face="Courier New"&gt;Join&amp;lt;TOuter, TInner, TKey, TResult&amp;gt;&lt;/font&gt; would be &lt;font face="Courier New"&gt;Join&amp;lt;Temp1, Store, int, Temp2&amp;gt;&lt;/font&gt;, where &lt;font face="Courier New"&gt;Temp1&lt;/font&gt; and &lt;font face="Courier New"&gt;Temp2&lt;/font&gt; represent the magical type definitions that the compiler creates underneath the covers when you use anonymous types in your C# code.&amp;#160; Notice the &lt;font face="Courier New"&gt;outerKeySelector&lt;/font&gt; needs to come before the &lt;font face="Courier New"&gt;innerKeySelector&lt;/font&gt;.&amp;#160; It appears the &amp;quot;&lt;font face="Courier New"&gt;on store.Id equals wishlist.storeId&lt;/font&gt;&lt;font face="Arial"&gt;&amp;quot; syntax is not a &lt;/font&gt;&lt;a href="http://en.wikipedia.org/wiki/Commutativity"&gt;commutative operation&lt;/a&gt;.&lt;/p&gt;

  &lt;p&gt;One last thing, I just realized that the error(s) given in Visual Studio when you actually compile the code are different than the intellisense red squiggly error message.&amp;#160; It appears an actual build of the code is able to be a little more omniscient than the quick compile that intellisense does as you type.&amp;#160; The error (and intelligent suggestion) the compiler offers says something like this:&lt;/p&gt;

  &lt;blockquote&gt;
    &lt;p&gt;&lt;font face="Courier New"&gt;The name 'store' is not in scope on the left side of 'equals'.&amp;#160; Consider swapping the expressions on either side of 'equals'.&lt;/font&gt;&lt;/p&gt;
  &lt;/blockquote&gt;

  &lt;h3&gt;Additional Info&lt;/h3&gt;

  &lt;p&gt;While reading the &lt;a href="http://msdn.microsoft.com/en-us/library/bb534675.aspx"&gt;MSDN article on the LINQ &lt;font face="Courier New"&gt;Join&lt;/font&gt; method&lt;/a&gt;, I thought the &amp;quot;&lt;font face="Courier New"&gt;Remarks&lt;/font&gt;&lt;font face="Arial"&gt;&amp;quot;&lt;/font&gt; section has some really great stuff about deferred execution, default equality comparison, differences from &lt;font face="Courier New"&gt;SelectMany&lt;/font&gt;, sort order perseverance, and so on:&lt;/p&gt;

  &lt;blockquote&gt;
    &lt;p&gt;This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its &lt;strong&gt;GetEnumerator&lt;/strong&gt; method directly or by using foreach in Visual C# or For Each in Visual Basic.&lt;/p&gt;

    &lt;p&gt;The default equality comparer, &lt;a href="http://msdn.microsoft.com/en-us/library/ms224763.aspx"&gt;Default&lt;/a&gt;, is used to hash and compare keys.&lt;/p&gt;

    &lt;p&gt;A join refers to the operation of correlating the elements of two sources of information based on a common key. &lt;a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.join.aspx"&gt;Join&lt;/a&gt; brings the two information sources and the keys by which they are matched together in one method call. This differs from the use of &lt;strong&gt;SelectMany&lt;/strong&gt;, which requires more than one method call to perform the same operation.&lt;/p&gt;

    &lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.join.aspx"&gt;Join&lt;/a&gt; preserves the order of the elements of outer, and for each of these elements, the order of the matching elements of inner.&lt;/p&gt;

    &lt;p&gt;In query expression syntax, a join (Visual C#) or Join (Visual Basic) clause translates to an invocation of &lt;a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.join.aspx"&gt;Join&lt;/a&gt;.&lt;/p&gt;

    &lt;p&gt;In relational database terms, the &lt;a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.join.aspx"&gt;Join&lt;/a&gt; method implements an inner equijoin. 'Inner' means that only elements that have a match in the other sequence are included in the results. An 'equijoin' is a join in which the keys are compared for equality. A left outer join operation has no dedicated standard query operator, but can be performed by using the &lt;a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.groupjoin.aspx"&gt;GroupJoin&lt;/a&gt; method. See &lt;a href="http://msdn.microsoft.com/en-us/library/bb397908.aspx"&gt;Join Operations&lt;/a&gt;.&lt;/p&gt;
  &lt;/blockquote&gt;

  &lt;p&gt;Thanks, I hope this proves helpful to someone, as it was certainly eye-opening to me.&lt;/p&gt;
&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1664217632350055880-3118670048214626927?l=murrayon.net' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://murrayon.net/feeds/3118670048214626927/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://murrayon.net/2010/12/linq-query-syntax-and-type-argument.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/3118670048214626927?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/3118670048214626927?v=2" /><link rel="alternate" type="text/html" href="http://murrayon.net/2010/12/linq-query-syntax-and-type-argument.html" title="LINQ Query Syntax and Type Argument Inference" /><author><name>Michael Murray</name><uri>https://profiles.google.com/109073345657761090655</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-Xf7Ccmz4FCQ/AAAAAAAAAAI/AAAAAAAAAGU/jOfxxMc1Lxo/s512-c/photo.jpg" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;A0AHSXY_cCp7ImA9Wx5WGEU.&quot;"><id>tag:blogger.com,1999:blog-1664217632350055880.post-6101841609243869887</id><published>2010-09-30T17:35:00.001-06:00</published><updated>2010-09-30T17:35:38.848-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-09-30T17:35:38.848-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="New Technology" /><category scheme="http://www.blogger.com/atom/ns#" term="Monads" /><category scheme="http://www.blogger.com/atom/ns#" term="Functional Programming" /><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="Visual Studio" /><category scheme="http://www.blogger.com/atom/ns#" term="Maybe Monad" /><category scheme="http://www.blogger.com/atom/ns#" term="Microsoft" /><category scheme="http://www.blogger.com/atom/ns#" term="Code Organization" /><category scheme="http://www.blogger.com/atom/ns#" term="Algorithms" /><title>Maybe Monad Extensions</title><content type="html">&lt;p&gt;I learned a lot from &lt;a href="http://murrayon.net/2010/09/maybe-from-murray-monads.html" target="_blank"&gt;my last foray into implementing my own Maybe Monad&lt;/a&gt;.&amp;#160; Apparently, it got noticed by and was actually useful to &lt;a href="http://www.lostechies.com/members/derick.bailey/default.aspx" target="_blank"&gt;Derick Bailey&lt;/a&gt;, as it was one of the resources he used in &lt;a href="http://www.lostechies.com/blogs/derickbailey/archive/2010/09/29/monads-in-c-which-part-is-the-monad.aspx" target="_blank"&gt;his own personal exploration into Monads&lt;/a&gt;.&amp;#160; Of course, Derick did the topic a bit more justice, as he has a propensity to do.&lt;/p&gt;  &lt;p&gt;Nonetheless, I have done as I said I would and reworked my Murray Monads into a cleaner, more concise implementation using extension methods, much like the implementation I introduced from Dmitri Nesteruk in the last post.&amp;#160; So without further ado, I’d like to introduce you to the MaybeMonadExtensions.&lt;/p&gt; &lt;span class="fullpost"&gt;   &lt;h4&gt;Monads 2.0 - &lt;a href="http://www.youtube.com/watch?v=ll7rWiY5obI&amp;amp;t=1m19s" target="_blank"&gt;“These Go To Eleven”&lt;/a&gt;&lt;/h4&gt;    &lt;p&gt;If you want to get an introduction to what Monads are and how I got to where I am today with this little library, then you really should check out &lt;a href="http://murrayon.net/2010/09/maybe-from-murray-monads.html" target="_blank"&gt;the previous post on this topic&lt;/a&gt;.&amp;#160; I will however give a few brief code samples to show you what it can do to tidy up all those null checks using the Maybe Monad.&lt;/p&gt;    &lt;p&gt;Here is code we tend to write all the time:&lt;/p&gt;    &lt;pre class="brush: csharp"&gt;string postCode;
if (person != null)
{
  if (HasMedicalRecord(person) &amp;amp;&amp;amp; person.Address != null)
  {
    CheckAddress(person.Address);
    if (person.Address.PostCode != null)
      postCode = person.Address.PostCode.ToString();
    else
      postCode = &amp;quot;UNKNOWN&amp;quot;;
  }
}&lt;/pre&gt;

  &lt;p&gt;It can look like this with some Maybe Monad love:&lt;/p&gt;

  &lt;pre class="brush: csharp"&gt;string postalCode = person.If(HasMedicalRecord)
  .Access(p =&amp;gt; p.Address)
  .Apply(CheckAddress)
  .Access(a =&amp;gt; a.PostCode)
  .Convert(pc =&amp;gt; pc.ToString(), &amp;quot;UNKNOWN&amp;quot;);&lt;/pre&gt;

  &lt;p&gt;I know!&amp;#160; Groovy, huh?!&lt;/p&gt;

  &lt;p&gt;The source can be found here:&amp;#160; &lt;a href="http://bitbucket.org/murrayondotnet/maybemonadextensions/"&gt;http://bitbucket.org/murrayondotnet/maybemonadextensions/&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;Let me know if you put this to good use!&lt;/p&gt;
&lt;/span&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1664217632350055880-6101841609243869887?l=murrayon.net' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://murrayon.net/feeds/6101841609243869887/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://murrayon.net/2010/09/maybe-monad-extensions.html#comment-form" title="3 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/6101841609243869887?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/6101841609243869887?v=2" /><link rel="alternate" type="text/html" href="http://murrayon.net/2010/09/maybe-monad-extensions.html" title="Maybe Monad Extensions" /><author><name>Michael Murray</name><uri>https://profiles.google.com/109073345657761090655</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-Xf7Ccmz4FCQ/AAAAAAAAAAI/AAAAAAAAAGU/jOfxxMc1Lxo/s512-c/photo.jpg" /></author><thr:total>3</thr:total></entry><entry gd:etag="W/&quot;CkcBQHk8cCp7ImA9Wx5WGEQ.&quot;"><id>tag:blogger.com,1999:blog-1664217632350055880.post-5166460154179846176</id><published>2010-09-21T18:33:00.000-06:00</published><updated>2010-09-30T17:40:51.778-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-09-30T17:40:51.778-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Monads" /><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="Maybe Monad" /><category scheme="http://www.blogger.com/atom/ns#" term="Code Organization" /><category scheme="http://www.blogger.com/atom/ns#" term="Microsoft" /><category scheme="http://www.blogger.com/atom/ns#" term="New Technology" /><category scheme="http://www.blogger.com/atom/ns#" term="Functional Programming" /><category scheme="http://www.blogger.com/atom/ns#" term="Rx Framework" /><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET" /><category scheme="http://www.blogger.com/atom/ns#" term="Visual Studio" /><category scheme="http://www.blogger.com/atom/ns#" term="LINQ" /><category scheme="http://www.blogger.com/atom/ns#" term="Algorithms" /><category scheme="http://www.blogger.com/atom/ns#" term="jQuery" /><title>Maybe from Murray Monads</title><content type="html">&lt;p&gt;Off and on for the last six months, I have probably seen and read at least a half dozen introductions and tutorials on &lt;a href="http://en.wikipedia.org/wiki/Monad_(functional_programming)"&gt;monads&lt;/a&gt;, a functional programming construct based on the mathematical monads from category theory.&amp;#160; Monads are known for their headache-inducing explanations, and I’ve had my fair share.&amp;#160; Every tutorial I’ve committed time to mentions the endless possibilities of using such constructs to improve your code, but I’ve had a hard time finding real-world examples of using this stuff out in the wild (&lt;a href="http://stackoverflow.com/questions/866912/what-is-the-compelling-scenario-for-using-monads-in-c"&gt;much like this Stack Overflow poster&lt;/a&gt;, whose link I only found just now).&amp;#160; It has recently become a quest of mine to find some C# code we all write that could be improved by a monad.&lt;/p&gt;  &lt;p&gt;I am now going to attempt to introduce a Maybe monad that I cooked up, but I warn you that I have no idea what I’m doing or if I’m staying within the rules for what makes a monad by definition a monad.&amp;#160; Don’t worry though, you have nothing to lose; I’m the only one taking a risk at looking foolish, and it’s all at the expense of my reputation and not yours.&amp;#160; Should be fun!&lt;/p&gt; &lt;span class="fullpost"&gt;   &lt;h3&gt;“It is not the spoon that bends, it is only yourself”&lt;/h3&gt;    &lt;p&gt;From the &lt;a href="http://en.wikipedia.org/wiki/Monad_(functional_programming)"&gt;Wikipedia article linked to above&lt;/a&gt;:&lt;/p&gt;    &lt;blockquote&gt;     &lt;p&gt;“In functional programming, a monad is a kind of abstract data type constructor used to represent computations (instead of data in the domain model). Monads allow the programmer to chain actions together to build a pipeline, in which each action is decorated with additional processing rules provided by the monad. Programs written in functional style can make use of monads to structure procedures that include sequenced operations, or to define arbitrary control flows (like handling concurrency, continuations, or exceptions).&lt;/p&gt;      &lt;p&gt;…&lt;/p&gt;      &lt;p&gt;“A programmer will compose monadic functions to define a data-processing pipeline. The monad acts as a framework, as it's a reusable behavior that decides the order in which the specific monadic functions in the pipeline are called, and manages all the undercover work required by the computation.”&lt;/p&gt;   &lt;/blockquote&gt;    &lt;p&gt;The promised tenets of monads are increased code readability and algorithm composability, an example of preferring declarative code over imperative, procedural code.&amp;#160; In the case of the Maybe monad, it is a wrapper around types that might have an invalid state like &lt;font face="Courier New"&gt;null&lt;/font&gt; or &lt;font face="Courier New"&gt;nothing&lt;/font&gt;.&amp;#160; The lure of the Maybe monad is the ability to deal with values that might be in this invalid state without having to explicitly check for null before continuing or exceptions being thrown in your algorithms.&lt;/p&gt;    &lt;p&gt;I first found &lt;a href="http://abdullin.com/journal/2009/10/6/zen-development-practices-c-maybe-monad.html"&gt;a post by Rinat Abdullin&lt;/a&gt; where he introduced his version of a C# Maybe monad.&amp;#160; I downloaded the library and attempted to figure out how to use it in code I could possibly write any given day.&amp;#160; I tried mimicking the &lt;font face="Courier New"&gt;HttpContext.Request.Params&lt;/font&gt; collection commonly used in ASP.NET pages and HTTP handlers to retrieve the passed in query string or post values on the &lt;font face="Courier New"&gt;Request&lt;/font&gt; object.&amp;#160; If you ask that collection for a parameter that was not passed in on the request, it returns a null instead of the string value.&amp;#160; Common practice is to then use a bunch of &lt;font face="Courier New"&gt;if&lt;/font&gt; statements and key off the fact that certain query string or post parameters may or may not exist.&lt;/p&gt;    &lt;p&gt;The problem I ran into with Rinat’s implemenation is that I wrote the code using the monad how I thought it should help me, and then ran into compiler errors and runtime exceptions because of the expectation that Maybe objects couldn’t be constructed around null values.&amp;#160; Here is the twitter conversation I had with Rinat in regard to this misconception that I had:&lt;/p&gt;   &lt;span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium &amp;#39;Times New Roman&amp;#39;; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="font-family: verdana, tahoma, arial, &amp;#39;Trebuchet MS&amp;#39;, sans-serif, georgia, courier, &amp;#39;Times New Roman&amp;#39;, serif; color: rgb(0,58,126); font-size: 12px; font-weight: bold" class="Apple-style-span"&gt;       &lt;div style="padding-bottom: 15px; background-color: rgb(234,242,249); margin: 0px; padding-left: 15px; width: 550px; padding-right: 15px; padding-top: 15px; background-origin: initial; background-clip: initial" class="post1"&gt;         &lt;div style="padding-bottom: 0px; margin: 0px 15px 0px 0px; padding-left: 0px; width: 80px; padding-right: 0px; display: block; float: left; height: 80px; padding-top: 0px" class="avatar"&gt;&lt;a style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; color: rgb(0,58,126); text-decoration: none; padding-top: 0px" title="View murrayondotnet&amp;#39;s Twitter Page" href="http://www.twitter.com/murrayondotnet" target="_blank"&gt;&lt;img style="border-bottom: rgb(204,204,204) 1px solid; border-left: rgb(204,204,204) 1px solid; padding-bottom: 3px; margin: 0px; padding-left: 3px; padding-right: 3px; border-top: rgb(204,204,204) 1px solid; border-right: rgb(204,204,204) 1px solid; padding-top: 3px" alt="murrayondotnet&amp;#39;s avatar" src="http://a1.twimg.com/profile_images/547516901/profilepic_bigger.PNG" width="73" height="73" /&gt;&lt;/a&gt;&lt;/div&gt;          &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; float: left; padding-top: 0px" class="post"&gt;           &lt;h5 style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: bold 16px verdana, tahoma, arial, &amp;#39;Trebuchet MS&amp;#39;, sans-serif, georgia, courier, &amp;#39;Times New Roman&amp;#39;, serif; color: rgb(98,98,98); padding-top: 0px"&gt;&lt;a style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; color: rgb(0,58,126); text-decoration: none; padding-top: 0px" title="View murrayondotnet&amp;#39;s Twitter Page" href="http://www.twitter.com/murrayondotnet" target="_blank"&gt;murrayondotnet&lt;/a&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;says:&lt;/h5&gt;            &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; width: 450px; padding-right: 0px; font: 14px/19px tahoma, arial; color: rgb(98,98,98); padding-top: 0px" class="mainpost"&gt;@abdullin Can you make your MaybeMonad in Lokad lib accept null at construct or explicit/implicit cast? Maybe&amp;lt;SomeType&amp;gt; monad = varThatsNull&lt;/div&gt;         &lt;/div&gt;          &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; clear: both; padding-top: 0px" class="clear"&gt;&lt;/div&gt;       &lt;/div&gt;        &lt;div style="padding-bottom: 15px; background-color: rgb(255,255,255); margin: 0px; padding-left: 15px; width: 550px; padding-right: 15px; padding-top: 15px; background-origin: initial; background-clip: initial" class="post2"&gt;         &lt;div style="padding-bottom: 0px; margin: 0px 15px 0px 0px; padding-left: 0px; width: 80px; padding-right: 0px; display: block; float: left; height: 80px; padding-top: 0px" class="avatar"&gt;&lt;a style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; color: rgb(0,58,126); text-decoration: none; padding-top: 0px" title="View abdullin&amp;#39;s Twitter Page" href="http://www.twitter.com/abdullin" target="_blank"&gt;&lt;img style="border-bottom: rgb(204,204,204) 1px solid; border-left: rgb(204,204,204) 1px solid; padding-bottom: 3px; margin: 0px; padding-left: 3px; padding-right: 3px; border-top: rgb(204,204,204) 1px solid; border-right: rgb(204,204,204) 1px solid; padding-top: 3px" alt="abdullin&amp;#39;s avatar" src="http://a3.twimg.com/profile_images/755788123/0_1f297_e07be068_orig_bigger.jpg" width="73" height="73" /&gt;&lt;/a&gt;&lt;/div&gt;          &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; float: left; padding-top: 0px" class="post"&gt;           &lt;h5 style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: bold 16px verdana, tahoma, arial, &amp;#39;Trebuchet MS&amp;#39;, sans-serif, georgia, courier, &amp;#39;Times New Roman&amp;#39;, serif; color: rgb(98,98,98); padding-top: 0px"&gt;&lt;a style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; color: rgb(0,58,126); text-decoration: none; padding-top: 0px" title="View abdullin&amp;#39;s Twitter Page" href="http://www.twitter.com/abdullin" target="_blank"&gt;abdullin&lt;/a&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;says:&lt;/h5&gt;            &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; width: 450px; padding-right: 0px; font: 14px/19px tahoma, arial; color: rgb(98,98,98); padding-top: 0px" class="mainpost"&gt;@murrayondotnet No, this would violate the idea of MaybeMonad (as in Lokad libs). But you can copy code and tweak it to your needs locally.&lt;/div&gt;         &lt;/div&gt;          &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; clear: both; padding-top: 0px" class="clear"&gt;&lt;/div&gt;       &lt;/div&gt;        &lt;div style="padding-bottom: 15px; background-color: rgb(234,242,249); margin: 0px; padding-left: 15px; width: 550px; padding-right: 15px; padding-top: 15px; background-origin: initial; background-clip: initial" class="post1"&gt;         &lt;div style="padding-bottom: 0px; margin: 0px 15px 0px 0px; padding-left: 0px; width: 80px; padding-right: 0px; display: block; float: left; height: 80px; padding-top: 0px" class="avatar"&gt;&lt;a style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; color: rgb(0,58,126); text-decoration: none; padding-top: 0px" title="View murrayondotnet&amp;#39;s Twitter Page" href="http://www.twitter.com/murrayondotnet" target="_blank"&gt;&lt;img style="border-bottom: rgb(204,204,204) 1px solid; border-left: rgb(204,204,204) 1px solid; padding-bottom: 3px; margin: 0px; padding-left: 3px; padding-right: 3px; border-top: rgb(204,204,204) 1px solid; border-right: rgb(204,204,204) 1px solid; padding-top: 3px" alt="murrayondotnet&amp;#39;s avatar" src="http://a1.twimg.com/profile_images/547516901/profilepic_bigger.PNG" width="73" height="73" /&gt;&lt;/a&gt;&lt;/div&gt;          &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; float: left; padding-top: 0px" class="post"&gt;           &lt;h5 style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: bold 16px verdana, tahoma, arial, &amp;#39;Trebuchet MS&amp;#39;, sans-serif, georgia, courier, &amp;#39;Times New Roman&amp;#39;, serif; color: rgb(98,98,98); padding-top: 0px"&gt;&lt;a style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; color: rgb(0,58,126); text-decoration: none; padding-top: 0px" title="View murrayondotnet&amp;#39;s Twitter Page" href="http://www.twitter.com/murrayondotnet" target="_blank"&gt;murrayondotnet&lt;/a&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;says:&lt;/h5&gt;            &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; width: 450px; padding-right: 0px; font: 14px/19px tahoma, arial; color: rgb(98,98,98); padding-top: 0px" class="mainpost"&gt;@abdullin Figured this was case; what does it violate? I'm still new to monads. Thought one would be able construct monad around maybe null&lt;/div&gt;         &lt;/div&gt;          &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; clear: both; padding-top: 0px" class="clear"&gt;&lt;/div&gt;       &lt;/div&gt;        &lt;div style="padding-bottom: 15px; background-color: rgb(255,255,255); margin: 0px; padding-left: 15px; width: 550px; padding-right: 15px; padding-top: 15px; background-origin: initial; background-clip: initial" class="post2"&gt;         &lt;div style="padding-bottom: 0px; margin: 0px 15px 0px 0px; padding-left: 0px; width: 80px; padding-right: 0px; display: block; float: left; height: 80px; padding-top: 0px" class="avatar"&gt;&lt;a style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; color: rgb(0,58,126); text-decoration: none; padding-top: 0px" title="View abdullin&amp;#39;s Twitter Page" href="http://www.twitter.com/abdullin" target="_blank"&gt;&lt;img style="border-bottom: rgb(204,204,204) 1px solid; border-left: rgb(204,204,204) 1px solid; padding-bottom: 3px; margin: 0px; padding-left: 3px; padding-right: 3px; border-top: rgb(204,204,204) 1px solid; border-right: rgb(204,204,204) 1px solid; padding-top: 3px" alt="abdullin&amp;#39;s avatar" src="http://a3.twimg.com/profile_images/755788123/0_1f297_e07be068_orig_bigger.jpg" width="73" height="73" /&gt;&lt;/a&gt;&lt;/div&gt;          &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; float: left; padding-top: 0px" class="post"&gt;           &lt;h5 style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: bold 16px verdana, tahoma, arial, &amp;#39;Trebuchet MS&amp;#39;, sans-serif, georgia, courier, &amp;#39;Times New Roman&amp;#39;, serif; color: rgb(98,98,98); padding-top: 0px"&gt;&lt;a style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; color: rgb(0,58,126); text-decoration: none; padding-top: 0px" title="View abdullin&amp;#39;s Twitter Page" href="http://www.twitter.com/abdullin" target="_blank"&gt;abdullin&lt;/a&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;says:&lt;/h5&gt;            &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; width: 450px; padding-right: 0px; font: 14px/19px tahoma, arial; color: rgb(98,98,98); padding-top: 0px" class="mainpost"&gt;@murrayondotnet check out the article on Maybe monads in Lokad libs: http://bit.ly/1GJlNN&lt;/div&gt;         &lt;/div&gt;          &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; clear: both; padding-top: 0px" class="clear"&gt;&lt;/div&gt;       &lt;/div&gt;        &lt;div style="padding-bottom: 15px; background-color: rgb(234,242,249); margin: 0px; padding-left: 15px; width: 550px; padding-right: 15px; padding-top: 15px; background-origin: initial; background-clip: initial" class="post1"&gt;         &lt;div style="padding-bottom: 0px; margin: 0px 15px 0px 0px; padding-left: 0px; width: 80px; padding-right: 0px; display: block; float: left; height: 80px; padding-top: 0px" class="avatar"&gt;&lt;a style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; color: rgb(0,58,126); text-decoration: none; padding-top: 0px" title="View murrayondotnet&amp;#39;s Twitter Page" href="http://www.twitter.com/murrayondotnet" target="_blank"&gt;&lt;img style="border-bottom: rgb(204,204,204) 1px solid; border-left: rgb(204,204,204) 1px solid; padding-bottom: 3px; margin: 0px; padding-left: 3px; padding-right: 3px; border-top: rgb(204,204,204) 1px solid; border-right: rgb(204,204,204) 1px solid; padding-top: 3px" alt="murrayondotnet&amp;#39;s avatar" src="http://a1.twimg.com/profile_images/547516901/profilepic_bigger.PNG" width="73" height="73" /&gt;&lt;/a&gt;&lt;/div&gt;          &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; float: left; padding-top: 0px" class="post"&gt;           &lt;h5 style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: bold 16px verdana, tahoma, arial, &amp;#39;Trebuchet MS&amp;#39;, sans-serif, georgia, courier, &amp;#39;Times New Roman&amp;#39;, serif; color: rgb(98,98,98); padding-top: 0px"&gt;&lt;a style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; color: rgb(0,58,126); text-decoration: none; padding-top: 0px" title="View murrayondotnet&amp;#39;s Twitter Page" href="http://www.twitter.com/murrayondotnet" target="_blank"&gt;murrayondotnet&lt;/a&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;says:&lt;/h5&gt;            &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; width: 450px; padding-right: 0px; font: 14px/19px tahoma, arial; color: rgb(98,98,98); padding-top: 0px" class="mainpost"&gt;@abdullin Thanks for the link, thats actually how I found your lib. I didnt see where you say why cant construct maybe monad on null. Help?&lt;/div&gt;         &lt;/div&gt;          &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; clear: both; padding-top: 0px" class="clear"&gt;&lt;/div&gt;       &lt;/div&gt;        &lt;div style="padding-bottom: 15px; background-color: rgb(255,255,255); margin: 0px; padding-left: 15px; width: 550px; padding-right: 15px; padding-top: 15px; background-origin: initial; background-clip: initial" class="post2"&gt;         &lt;div style="padding-bottom: 0px; margin: 0px 15px 0px 0px; padding-left: 0px; width: 80px; padding-right: 0px; display: block; float: left; height: 80px; padding-top: 0px" class="avatar"&gt;&lt;a style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; color: rgb(0,58,126); text-decoration: none; padding-top: 0px" title="View abdullin&amp;#39;s Twitter Page" href="http://www.twitter.com/abdullin" target="_blank"&gt;&lt;img style="border-bottom: rgb(204,204,204) 1px solid; border-left: rgb(204,204,204) 1px solid; padding-bottom: 3px; margin: 0px; padding-left: 3px; padding-right: 3px; border-top: rgb(204,204,204) 1px solid; border-right: rgb(204,204,204) 1px solid; padding-top: 3px" alt="abdullin&amp;#39;s avatar" src="http://a3.twimg.com/profile_images/755788123/0_1f297_e07be068_orig_bigger.jpg" width="73" height="73" /&gt;&lt;/a&gt;&lt;/div&gt;          &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; float: left; padding-top: 0px" class="post"&gt;           &lt;h5 style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: bold 16px verdana, tahoma, arial, &amp;#39;Trebuchet MS&amp;#39;, sans-serif, georgia, courier, &amp;#39;Times New Roman&amp;#39;, serif; color: rgb(98,98,98); padding-top: 0px"&gt;&lt;a style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; color: rgb(0,58,126); text-decoration: none; padding-top: 0px" title="View abdullin&amp;#39;s Twitter Page" href="http://www.twitter.com/abdullin" target="_blank"&gt;abdullin&lt;/a&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;says:&lt;/h5&gt;            &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; width: 450px; padding-right: 0px; font: 14px/19px tahoma, arial; color: rgb(98,98,98); padding-top: 0px" class="mainpost"&gt;@murrayondotnet in essence monad in Lokad guarantees: there will be no nulls around (hence no need to check for them), it replaces null.&lt;/div&gt;         &lt;/div&gt;          &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; clear: both; padding-top: 0px" class="clear"&gt;&lt;/div&gt;       &lt;/div&gt;        &lt;div style="padding-bottom: 15px; background-color: rgb(234,242,249); margin: 0px; padding-left: 15px; width: 550px; padding-right: 15px; padding-top: 15px; background-origin: initial; background-clip: initial" class="post1"&gt;         &lt;div style="padding-bottom: 0px; margin: 0px 15px 0px 0px; padding-left: 0px; width: 80px; padding-right: 0px; display: block; float: left; height: 80px; padding-top: 0px" class="avatar"&gt;&lt;a style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; color: rgb(0,58,126); text-decoration: none; padding-top: 0px" title="View murrayondotnet&amp;#39;s Twitter Page" href="http://www.twitter.com/murrayondotnet" target="_blank"&gt;&lt;img style="border-bottom: rgb(204,204,204) 1px solid; border-left: rgb(204,204,204) 1px solid; padding-bottom: 3px; margin: 0px; padding-left: 3px; padding-right: 3px; border-top: rgb(204,204,204) 1px solid; border-right: rgb(204,204,204) 1px solid; padding-top: 3px" alt="murrayondotnet&amp;#39;s avatar" src="http://a1.twimg.com/profile_images/547516901/profilepic_bigger.PNG" width="73" height="73" /&gt;&lt;/a&gt;&lt;/div&gt;          &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; float: left; padding-top: 0px" class="post"&gt;           &lt;h5 style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: bold 16px verdana, tahoma, arial, &amp;#39;Trebuchet MS&amp;#39;, sans-serif, georgia, courier, &amp;#39;Times New Roman&amp;#39;, serif; color: rgb(98,98,98); padding-top: 0px"&gt;&lt;a style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; color: rgb(0,58,126); text-decoration: none; padding-top: 0px" title="View murrayondotnet&amp;#39;s Twitter Page" href="http://www.twitter.com/murrayondotnet" target="_blank"&gt;murrayondotnet&lt;/a&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;says:&lt;/h5&gt;            &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; width: 450px; padding-right: 0px; font: 14px/19px tahoma, arial; color: rgb(98,98,98); padding-top: 0px" class="mainpost"&gt;@abdullin I may try to create my own implementation that hides whether null or not. If so probably base mine off of the nice things in yours&lt;/div&gt;         &lt;/div&gt;          &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; clear: both; padding-top: 0px" class="clear"&gt;&lt;/div&gt;       &lt;/div&gt;     &lt;/span&gt;&lt;/span&gt;    &lt;p&gt;And so I did.&lt;/p&gt;    &lt;h3&gt;Introducing MurrayMonads&lt;/h3&gt;    &lt;p&gt;I don’t even like the name.&amp;#160; I’m also not sure this code will ever be used again…by anyone, let alone me (especially since I like some other guy’s implementation better; read on to watch me embarrass myself).&amp;#160; But whatever, Visual Studio asks for a project/solution name and so there it is.&amp;#160; I need some cool theme like Rhino, as used by &lt;a href="http://ayende.com/blog/default.aspx"&gt;Ayende&lt;/a&gt;.&lt;/p&gt;    &lt;p&gt;Speaking of Oren (I will use his proper name instead so that he doesn’t &lt;a href="http://ayende.com/Blog/archive/2010/08/13/how-to-pay-3-times-for-the-same-flight-ticket.aspx"&gt;get stuck at the airport unable to board his flight&lt;/a&gt;), he posted a link on Twitter to &lt;a href="http://devtalk.net/csharp/chained-null-checks-and-the-maybe-monad/"&gt;an article where Dmitri Nesteruk created his own version of the Maybe monad&lt;/a&gt;, just as I was starting my experiment.&amp;#160; He used extension methods instead of an explicit monad object wrapper in order to enable the pipelining composability that you can also do with Rinat’s implementation.&amp;#160; Really clever, but there was one small thing that bothered me a bit, and that is how the monad chaining begins with Dmitri’s API.&amp;#160; (Actually, I’ll prove I’m wrong by the end of the post; this is the cause of upcoming embarrassment mentioned above, hooray!)&amp;#160; Because of this, I decided to use his example problem as the basis for my sample that I will show, in order to increase comparability.&lt;/p&gt;    &lt;p&gt;So I set off to try and merge the good points of both Rinat’s and Dmitri’s implementations.&amp;#160; I have put my source code up for viewing at Bitbucket.org:&amp;#160; &lt;a href="http://bitbucket.org/murrayondotnet/murraymonads"&gt;http://bitbucket.org/murrayondotnet/murraymonads&lt;/a&gt;&lt;/p&gt;    &lt;h3&gt;Are We There Yet?!&lt;/h3&gt;    &lt;p&gt;Yes, we are.&amp;#160; Let’s see some code finally.&lt;/p&gt;    &lt;p&gt;Here is the code we don’t like writing all the time:&lt;/p&gt;    &lt;pre class="brush: csharp"&gt;string postCode;
if (person != null)
{
  if (HasMedicalRecord(person) &amp;amp;&amp;amp; person.Address != null)
  {
    CheckAddress(person.Address);
    if (person.Address.PostCode != null)
      postCode = person.Address.PostCode.ToString();
    else
      postCode = &amp;quot;UNKNOWN&amp;quot;;
  }
}&lt;/pre&gt;

  &lt;p&gt;This is actually code that you wrote; I took it just yesterday from your current project at work.&amp;#160; I know you’re feeling a bit defensive because of this, but I’m here to help so put down the attitude please.&amp;#160; We’re gonna make your life easier…I hope…&lt;/p&gt;

  &lt;p&gt;All in all, it’s code you see every day and there’s not much really wrong with it per se.&amp;#160; I just think it could be cleaner I suppose.&amp;#160; So using Dmitri’s extension methods, you can get that logic down to this:&lt;/p&gt;

  &lt;pre class="brush: csharp"&gt;string postCode = this.With(x =&amp;gt; person)
  .If(x =&amp;gt; HasMedicalRecord(x))
  .With(x =&amp;gt; x.Address)
  .Do(x =&amp;gt; CheckAddress(x))
  .With(x =&amp;gt; x.PostCode)
  .Return(x =&amp;gt; x.ToString(), &amp;quot;UNKNOWN&amp;quot;);&lt;/pre&gt;

  &lt;p&gt;As long as that last &lt;font face="Courier New"&gt;ToString&lt;/font&gt; method call doesn’t return a null (which one of my use cases does in the &lt;font face="Courier New"&gt;TestConsoleApp&lt;/font&gt; included in my source code), you will either get the value or &lt;font face="Courier New"&gt;“UNKNOWN”&lt;/font&gt;.&amp;#160; Looking good!&lt;/p&gt;

  &lt;p&gt;So my version of the Maybe monad comes out pretty close to this last one:&lt;/p&gt;

  &lt;pre class="brush: csharp"&gt;string postCode = person.If(p =&amp;gt; HasMedicalRecord(p))
  .Access(p =&amp;gt; p.Address)
  .Apply(a =&amp;gt; CheckAddress(a))
  .Access(a =&amp;gt; a.PostCode)
  .Convert(pc =&amp;gt; pc.ToString(), &amp;quot;UNKNOWN&amp;quot;);

// or even
string postalCode = person.If(HasMedicalRecord)
  .Access(p =&amp;gt; p.Address)
  .Apply(CheckAddress)
  .Access(a =&amp;gt; a.PostCode)
  .Convert(pc =&amp;gt; pc.ToString(), &amp;quot;UNKNOWN&amp;quot;);&lt;/pre&gt;

  &lt;p&gt;The difference between Dmitri’s and mine being how the chain is first started, on the Maybe &lt;font face="Courier New"&gt;person&lt;/font&gt; object instead of Dmitri’s use of the &lt;font face="Courier New"&gt;this&lt;/font&gt; keyword.&lt;/p&gt;

  &lt;p&gt;Here’s what you’ve been really waiting for where I eat my own words.&amp;#160; Dmitri’s code can also be written like this:&lt;/p&gt;

  &lt;pre class="brush: csharp"&gt;string postCode = person.If(HasMedicalRecord)
  .With(x =&amp;gt; x.Address)
  .Do(CheckAddress)
  .With(x =&amp;gt; x.PostCode)
  .Return(x =&amp;gt; x.ToString(), &amp;quot;UNKNOWN&amp;quot;);&lt;/pre&gt;

  &lt;p&gt;…which appears to take away any advantage of my library.&amp;#160; I officially like his solution better, I just don’t like some of the method names.&amp;#160; So perhaps &lt;a href="http://murrayon.net/2010/09/maybe-monad-extensions.html" target="_blank"&gt;I will create another blog post&lt;/a&gt; and &lt;a href="http://bitbucket.org/murrayondotnet/maybemonadextensions/" target="_blank"&gt;Bitbucket repo with a new library of extension methods&lt;/a&gt; instead.&amp;#160; I will leave up this mess-of-a-post public (as well as the source code) so that perhaps others can see the process I went through and learn from my mistakes.&lt;/p&gt;

  &lt;h3&gt;Your Turn to Criticize Now&lt;/h3&gt;

  &lt;p&gt;Let me have it.&amp;#160; Let me know how crazy I am or if this is even useful.&amp;#160; Let me know if I’m completely misusing the monad principles.&amp;#160; Or, by some weird miracle, let me know if I’m on to something.&amp;#160; Actually, if Dmitri is on to something.&amp;#160; Either way, I want to hear from you.&amp;#160; Thanks.&lt;/p&gt;

  &lt;h3&gt;Additional Monad Learning Resources&lt;/h3&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;a href="http://blogs.msdn.com/b/wesdyer/archive/2008/01/11/the-marvels-of-monads.aspx"&gt;http://blogs.msdn.com/b/wesdyer/archive/2008/01/11/the-marvels-of-monads.aspx&lt;/a&gt; &lt;/li&gt;

    &lt;li&gt;&lt;a href="http://www.infoq.com/presentations/Demystifying-Monads"&gt;http://www.infoq.com/presentations/Demystifying-Monads&lt;/a&gt; &lt;/li&gt;

    &lt;li&gt;&lt;a href="http://importantshock.wordpress.com/2009/01/18/jquery-is-a-monad/"&gt;http://importantshock.wordpress.com/2009/01/18/jquery-is-a-monad/&lt;/a&gt; &lt;/li&gt;

    &lt;li&gt;Also, LINQ is based on monadic principles. &lt;/li&gt;
  &lt;/ul&gt;
&lt;/span&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1664217632350055880-5166460154179846176?l=murrayon.net' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://murrayon.net/feeds/5166460154179846176/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://murrayon.net/2010/09/maybe-from-murray-monads.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/5166460154179846176?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/5166460154179846176?v=2" /><link rel="alternate" type="text/html" href="http://murrayon.net/2010/09/maybe-from-murray-monads.html" title="Maybe from Murray Monads" /><author><name>Michael Murray</name><uri>https://profiles.google.com/109073345657761090655</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-Xf7Ccmz4FCQ/AAAAAAAAAAI/AAAAAAAAAGU/jOfxxMc1Lxo/s512-c/photo.jpg" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;DE8HRXk8fyp7ImA9Wx5QGE8.&quot;"><id>tag:blogger.com,1999:blog-1664217632350055880.post-5410807974378841491</id><published>2010-09-06T20:22:00.001-06:00</published><updated>2010-09-06T20:33:54.777-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-09-06T20:33:54.777-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="New Technology" /><category scheme="http://www.blogger.com/atom/ns#" term="Functional Programming" /><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="F#" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="Visual Studio" /><category scheme="http://www.blogger.com/atom/ns#" term="Microsoft" /><category scheme="http://www.blogger.com/atom/ns#" term="LINQ" /><category scheme="http://www.blogger.com/atom/ns#" term="Code Organization" /><category scheme="http://www.blogger.com/atom/ns#" term="Algorithms" /><category scheme="http://www.blogger.com/atom/ns#" term="SOLID" /><title>F# Pipeline Operator in C#?</title><content type="html">&lt;p&gt;I am currently reading the &lt;a href="http://www.manning.com/petricek/" target="_blank"&gt;Real-World Functional Programming&lt;/a&gt; book from &lt;a href="http://www.manning.com/" target="_blank"&gt;Manning Publications&lt;/a&gt;. It’s mostly an exercise in learning F#, but also to improve my C# code as well. I finally learned what the F# pipelining operator (&lt;font face="Courier New"&gt;|&amp;gt;&lt;/font&gt;) is all about, and I was so impressed I thought I would try to figure out how to bring it into C# if possible.&lt;/p&gt; &lt;span class="fullpost"&gt;   &lt;h3&gt;Introducing the F# Pipeline Operator&lt;/h3&gt;    &lt;p&gt;Here is some F# code that defines a list of integers and a pair of functions.&amp;#160; They are then used in a few “higher-order” functions (functions that utilize other functions as parameters or return values, according to my book).&amp;#160; &lt;font face="courier new"&gt;List.filter&lt;/font&gt; is equivalent to the LINQ &lt;font face="Courier New"&gt;IEnumerable.Where()&lt;/font&gt; extension method, and &lt;font face="Courier New"&gt;List.map&lt;/font&gt; is equivalent to &lt;font face="Courier New"&gt;IEnumerable.Select()&lt;/font&gt;.&lt;/p&gt;    &lt;pre class="brush: csharp"&gt;let numbers = [ 1 .. 10 ]
let isOdd(n) = n % 2 = 1
let square(n) = n * n

List.filter isOdd numbers
List.map square (List.filter isOdd numbers)&lt;/pre&gt;

  &lt;p&gt;The interesting part is the last line of code, where it is argued that you lose a little bit of code readability because the function that is actually executed first (&lt;font face="Courier New"&gt;List.filter&lt;/font&gt;) is found second in the expression statement.&amp;#160; Enter the F# pipelining operator:&lt;/p&gt;

  &lt;pre class="brush: csharp"&gt;let squared =
  numbers
  |&amp;gt; List.filter isOdd
  |&amp;gt; List.map square&lt;/pre&gt;

  &lt;p&gt;Even though we’re no longer a compact, one-liner expression, I think we’ve gained some code readability.&amp;#160; It says to take the list &lt;font face="Courier New"&gt;numbers&lt;/font&gt; and perform the following manipulations in the exact order listed.&amp;#160; Cool stuff, if I say so myself.&lt;/p&gt;

  &lt;h3&gt;“There is No Charge for Awesomeness…or Attractiveness”&lt;/h3&gt;

  &lt;p&gt;(Sorry, I’ve been watching the movie &lt;a href="http://www.imdb.com/title/tt0441773/" target="_blank"&gt;Kung Fu Panda&lt;/a&gt; a lot with my kids lately.&amp;#160; Funny stuff.)&amp;#160; So can this awesomeness be brought over into C#?&amp;#160; I opened up Visual Studio and tried to see if I could figure out how to define my own operator or reuse one of the existing ones.&lt;/p&gt;

  &lt;p&gt;I realize C# is a pretty tough language to flip keywords and operators around and invent new syntax with (ok, it’s impossible), but I tried anyway.&amp;#160; The type of code readability issue I wanted to remedy is the same as with the F# code snippet above.&amp;#160; I thought it would be nice to make C# code like the following statement more readable and digestible (especially the execution order):&lt;/p&gt;

  &lt;pre class="brush: csharp"&gt;Convert.ToInt32(Context.Request[“requestParam”].Trim());&lt;/pre&gt;

  &lt;p&gt;As I started to (unsuccessfully) beat out some pretty rough code, I ran into problems with trying to make extension methods that are operator definition overloads and errors saying “&lt;font face="Courier New"&gt;One of the parameters of a binary operator must be the containing type.&lt;/font&gt;”&amp;#160; It just wasn’t working and then I began to realize that C# already has a pipelining syntax, even if only in one small subset of the .NET API.&amp;#160; You can find a decent pipelining syntax with the &lt;a href="http://en.wikipedia.org/wiki/Fluent_interface" target="_blank"&gt;Fluent Interface&lt;/a&gt; used for the LINQ extension methods.&amp;#160; Its power is in the fact that the functions return the object type that was passed in and acted upon, so that more functions can be chained on afterward in a composable manner.&amp;#160; The ability to do things like &lt;font face="Courier New"&gt;collection.Where().Select().OrderBy()&lt;/font&gt; is a much more declarative and functional solution to imperative &lt;font face="Courier New"&gt;for&lt;/font&gt; loops (and it makes it more readable and intention revealing in my opinion).&amp;#160; It’s a difference in describing more &lt;u&gt;&lt;em&gt;what&lt;/em&gt;&lt;/u&gt; you want to be accomplished rather than &lt;em&gt;&lt;u&gt;how&lt;/u&gt;&lt;/em&gt; it is to be accomplished (especially when the &lt;u&gt;&lt;em&gt;how&lt;/em&gt;&lt;/u&gt; in programming languages is more readable to a computer than it is a human).&lt;/p&gt;

  &lt;p&gt;So the good news is that you indeed can have a nice pipelining syntax in C#, especially for APIs that are designed with Fluent Interfaces like LINQ.&amp;#160; However, it doesn’t solve code readability issues with code snippets like the C# fragment I showed above.&amp;#160; For those regular, non-fluent method calls in the rest of the .NET API, you must use old-fashioned Object-Oriented Programming practices of breaking code up into meaningful, atomic units of statements and naming and encapsulating functions cohesively in order to get more readable code.&lt;/p&gt;
&lt;/span&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1664217632350055880-5410807974378841491?l=murrayon.net' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://murrayon.net/feeds/5410807974378841491/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://murrayon.net/2010/09/f-pipeline-operator-in-c.html#comment-form" title="6 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/5410807974378841491?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/5410807974378841491?v=2" /><link rel="alternate" type="text/html" href="http://murrayon.net/2010/09/f-pipeline-operator-in-c.html" title="F# Pipeline Operator in C#?" /><author><name>Michael Murray</name><uri>https://profiles.google.com/109073345657761090655</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-Xf7Ccmz4FCQ/AAAAAAAAAAI/AAAAAAAAAGU/jOfxxMc1Lxo/s512-c/photo.jpg" /></author><thr:total>6</thr:total></entry><entry gd:etag="W/&quot;CEUGQnwzfCp7ImA9WxFaEUg.&quot;"><id>tag:blogger.com,1999:blog-1664217632350055880.post-6468495126707346007</id><published>2010-07-14T17:59:00.000-06:00</published><updated>2010-07-14T18:03:43.284-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-07-14T18:03:43.284-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="New Technology" /><category scheme="http://www.blogger.com/atom/ns#" term="JavaScript" /><category scheme="http://www.blogger.com/atom/ns#" term="Delegates" /><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="Rx Framework" /><category scheme="http://www.blogger.com/atom/ns#" term="Events" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="Visual Studio" /><category scheme="http://www.blogger.com/atom/ns#" term="Silverlight" /><category scheme="http://www.blogger.com/atom/ns#" term="Microsoft" /><category scheme="http://www.blogger.com/atom/ns#" term="LINQ" /><category scheme="http://www.blogger.com/atom/ns#" term="Algorithms" /><title>Reactive Framework Finally Explained</title><content type="html">&lt;p&gt;I have been intrigued by the &lt;a href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;Reactive Extensions for .NET (Rx Framework)&lt;/a&gt; for quite a while now, especially the fundamental shift from a “push” data model as opposed to the “pull” model most familiar to us.&amp;#160; I have thought to myself that this must be a game changer in how data and events are consumed in every-day software.&amp;#160; I didn’t quite understand how it all works or how to use it from the few samples I had seen, but I knew if I gave myself a little time to really sit down and look at it, my life would be changed forever.&lt;/p&gt;  &lt;p&gt;However, after reading some articles, watching some recorded presentations, and even &lt;a href="http://live.visitmix.com/MIX10/Sessions/FTL01"&gt;attending a MIX ‘10 session in-person&lt;/a&gt; on the Reactive Extensions, I felt increasingly lost and confused by what this framework could offer me.&amp;#160; I wasn’t understanding some fundamental ideology of the framework and it was jading my learning experience when watching/reading sample code.&amp;#160; Well, I think I have finally figured out what the mental roadblock was and will attempt to explain the framework here as I best as I understand it, in hopes that it can be of help to others not getting what the fuss is all about.&amp;#160; I can sense that I’m not the only one who has been turned off by the unsuccessful attempts by Microsoft to explain this revolutionary framework to the average developer.&amp;#160; Apparently those who had a hand in designing the framework are just so incredibly intelligent that they are having a hard time discerning that the common man is not immediately getting it.&lt;/p&gt; &lt;span class="fullpost"&gt;   &lt;h3&gt;&lt;font face="Courier New"&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/font&gt;, Meet &lt;font face="Courier New"&gt;IObservable&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/h3&gt;    &lt;p&gt;Essentially what Microsoft did is create a &lt;a href="http://en.wikipedia.org/wiki/Duality_(mathematics)"&gt;mathematical dual&lt;/a&gt; of the &lt;font face="Courier New"&gt;IEnumerable&lt;/font&gt; and &lt;font face="Courier New"&gt;IEnumerator&lt;/font&gt; interfaces using &lt;a href="http://en.wikipedia.org/wiki/Category_theory"&gt;Category Theory&lt;/a&gt;, and named them &lt;font face="Courier New"&gt;IObservable&lt;/font&gt; and &lt;font face="Courier New"&gt;IObserver&lt;/font&gt; respectively.&amp;#160; In its bare simplicity, this is done by merely reversing the direction of your arrows in your mathematical functions and compositions.&amp;#160; The following images of a few slides from &lt;a href="http://live.visitmix.com/MIX10/Sessions/FTL01"&gt;Erik Meijer’s MIX ‘10 Session&lt;/a&gt; that were the best visual explanation I have seen:&lt;/p&gt;    &lt;p&gt;Duality Example with De Morgan’s Laws&lt;/p&gt;    &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_rps657FzHZ0/TD5QDKjrqpI/AAAAAAAAADo/_LP_XWkDId4/s1600-h/rxSlide23.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="rxSlide2" border="0" alt="rxSlide2" src="http://lh5.ggpht.com/_rps657FzHZ0/TD5QDjfchiI/AAAAAAAAADs/etW1caiUFHA/rxSlide2_thumb1.png?imgmax=800" width="441" height="334" /&gt;&lt;/a&gt;&lt;/p&gt;    &lt;p&gt;Creating a Dual of &lt;font face="Courier New"&gt;IEnumerable&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_rps657FzHZ0/TD5QD9tyhyI/AAAAAAAAADw/CG9FYEFDB4Y/s1600-h/rxSlide33.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="rxSlide3" border="0" alt="rxSlide3" src="http://lh4.ggpht.com/_rps657FzHZ0/TD5QEm7T9hI/AAAAAAAAAD0/t_pfOknuY3o/rxSlide3_thumb1.png?imgmax=800" width="441" height="334" /&gt;&lt;/a&gt;&lt;/p&gt;    &lt;p&gt;Creating a Dual of &lt;font face="Courier New"&gt;IEnumerator&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_rps657FzHZ0/TD5QE7KuUwI/AAAAAAAAAD4/tpV314oMx5g/s1600-h/rxSlide43.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="rxSlide4" border="0" alt="rxSlide4" src="http://lh3.ggpht.com/_rps657FzHZ0/TD5QFZd3VjI/AAAAAAAAAD8/9jaGTsA76jQ/rxSlide4_thumb1.png?imgmax=800" width="441" height="333" /&gt;&lt;/a&gt;&amp;#160;&lt;/p&gt;    &lt;p&gt;The reason the mathematical proof part is significant is because we can guarantee that LINQ syntax and operators will work on these new collection and data subscriber interfaces, just like they did with &lt;font face="Courier New"&gt;IEnumerable&lt;/font&gt; collections in LINQ-to-Objects.&amp;#160; This new LINQ provider is commonly called “LINQ over Events.”&lt;/p&gt;    &lt;h3&gt;Reacting to Data is Asynchronous&lt;/h3&gt;    &lt;p&gt;This is actually where I think some of the confusion came in for me.&amp;#160; Mostly what I didn’t understand is how you can write LINQ queries into pushed data (already sent and also yet to be sent), when essentially that task is indeterminate by nature.&amp;#160; What I have figured out is that the LINQ syntax is not entirely for the data being pushed, but also used for molding the subscriptions themselves.&amp;#160; What the LINQ syntax provides you is a way to compose, filter, project, aggregate, and throttle data and event subscriptions in a powerful, declarative way.&lt;/p&gt;    &lt;p&gt;A common workflow seems to be to create one or more observable collections (and there are &lt;a title="16 Ways to Create IObservables Without Implementing IObservable" href="http://weblogs.asp.net/sweinstein/archive/2010/01/10/16-ways-to-create-iobservables-without-implementing-iobservable.aspx"&gt;many ways to do so&lt;/a&gt;) and then customize and compose a subscription based on the subscription(s) using LINQ syntax.&amp;#160; All that’s left then is to set the subscription in motion, giving it some sort of callback or lambda expression to be called as data is asynchronously pushed in and processed.&lt;/p&gt;    &lt;p&gt;Here is a very simple code example that illustrates this process quite plainly:&lt;/p&gt;    &lt;pre class="brush: csharp"&gt;using System;
using System.Linq;

namespace TestRX
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var oneNumberPerSecond = Observable.Interval(TimeSpan.FromSeconds(1));

            var numbersTimesTwo = from n in oneNumberPerSecond
                                  select n * 2;

            Console.WriteLine(&amp;quot;Numbers * 2:&amp;quot;);

            numbersTimesTwo.Subscribe(num =&amp;gt;
            {
                Console.WriteLine(num);
            });

            Console.ReadKey();
        }
    }
}&lt;/pre&gt;

  &lt;p&gt;It is interesting to watch the code run and think about what it’s actually doing.&amp;#160; It sits on the &lt;font face="Courier New"&gt;Console.ReadKey()&lt;/font&gt; line while the observable collection is asynchronously pushing data to the lambda expression containing the &lt;font face="Courier New"&gt;Console.WriteLine()&lt;/font&gt; statement.&lt;/p&gt;

  &lt;p&gt;It looks like the Reactive Framework is not just a better way to handle events and pushed data, but it also appears from what I’ve read that it could be used as a nicer API for sending off asynchronous process threads.&lt;/p&gt;

  &lt;h3&gt;Additional Reading&lt;/h3&gt;

  &lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/devlabs/default.aspx"&gt;Microsoft DevLabs&lt;/a&gt; has created the &lt;a href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;Reactive Extensions for .NET&lt;/a&gt; (and it’s associated languages), but it has also came up with the Reactive Extensions for Silverlight and JavaScript (&lt;a href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;can be found on same webpage&lt;/a&gt;).&amp;#160; This proves very interesting as JavaScript (and Silverlight) tends to be quite event driven because of its close interactions to UI elements in HTML (or XAML).&amp;#160; In fact, one of the most encouraging example use cases that I’ve heard was that of having some HTML text field that you wanted to auto-search as the user types, but not until the user pauses for a second; they can also hit Enter.&amp;#160; What the Reactive Framework could provide you is a way to aggregate more than one event source (alphabetic and Enter key strokes and the timer) into a single event handler using clear, declarative framework statements.&lt;/p&gt;

  &lt;p&gt;Here are other links I would recommend for additional examples and explanation:&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;a href="http://rxwiki.wikidot.com/101samples"&gt;(not yet) 101 Rx Samples – Reactive Framework (Rx) Wiki&lt;/a&gt; &lt;/li&gt;

    &lt;li&gt;&lt;a href="http://www.codeproject.com/KB/Parallel_Programming/RxByExample.aspx"&gt;The Reactive Framework by Example – The Code Project&lt;/a&gt; &lt;/li&gt;

    &lt;li&gt;&lt;a href="http://live.visitmix.com/MIX10/Sessions/FTL01"&gt;Reactive Extensions for JavaScript – Erik Meijer’s MIX ‘10 Session&lt;/a&gt; &lt;/li&gt;

    &lt;li&gt;&lt;a href="http://www.virtualaltnet.com/Recordings/Show/99"&gt;Making the Most of the Reactive Extensions for .NET – Virtual Alt.NET recording with Scott Weinstein&lt;/a&gt;&lt;/li&gt;

    &lt;li&gt;&lt;a href="http://channel9.msdn.com/shows/Going+Deep/Expert-to-Expert-Brian-Beckman-and-Erik-Meijer-Inside-the-NET-Reactive-Framework-Rx/"&gt;Channel 9 Video with Brian Beckman and Erik Meijer&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;

  &lt;p&gt;Enjoy!&amp;#160; Let me know via comments if you start using it for something real cool and useful.&lt;/p&gt;
&lt;/span&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1664217632350055880-6468495126707346007?l=murrayon.net' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://murrayon.net/feeds/6468495126707346007/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://murrayon.net/2010/07/reactive-framework-finally-explained.html#comment-form" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/6468495126707346007?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/6468495126707346007?v=2" /><link rel="alternate" type="text/html" href="http://murrayon.net/2010/07/reactive-framework-finally-explained.html" title="Reactive Framework Finally Explained" /><author><name>Michael Murray</name><uri>https://profiles.google.com/109073345657761090655</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-Xf7Ccmz4FCQ/AAAAAAAAAAI/AAAAAAAAAGU/jOfxxMc1Lxo/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh5.ggpht.com/_rps657FzHZ0/TD5QDjfchiI/AAAAAAAAADs/etW1caiUFHA/s72-c/rxSlide2_thumb1.png?imgmax=800" height="72" width="72" /><thr:total>2</thr:total></entry><entry gd:etag="W/&quot;DUYDQnk7eip7ImA9WxFVE0s.&quot;"><id>tag:blogger.com,1999:blog-1664217632350055880.post-1982434384212077640</id><published>2010-06-12T12:19:00.004-06:00</published><updated>2010-06-12T12:26:13.702-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-06-12T12:26:13.702-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="New Technology" /><category scheme="http://www.blogger.com/atom/ns#" term="Threading" /><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="Visual Studio" /><category scheme="http://www.blogger.com/atom/ns#" term="Microsoft" /><category scheme="http://www.blogger.com/atom/ns#" term="LINQ" /><category scheme="http://www.blogger.com/atom/ns#" term="Algorithms" /><title>Parallel.For Loops in .NET 4</title><content type="html">&lt;p&gt;I was having fun working on the &lt;a href="http://toughestdeveloperpuzzleever.com/tdpe2/"&gt;Toughest Developer Puzzle Ever 2&lt;/a&gt; when I came across a problem that asked for the coordinates into a grid of numbers of the upper left corner for a 4x4 sub-grid with a magic sum of 34, meaning all rows, columns, and the 2 diagonals all sum up to 34.&amp;#160; Instead of trying to figure it out manually, I probably did what many programmers would do and wrote a quick and dirty program to accomplish the task for me.&lt;/p&gt;  &lt;p&gt;I didn’t try to use any optimized search algorithm or anything, as I knew that the problem space was very small and specific and that computers are really fast.&amp;#160; So I figured I would just write a brute-force iterate through every row and column type of an algorithm (in Big O notation being &lt;font face="Courier New"&gt;O(mn)&lt;/font&gt; or just &lt;font face="Courier New"&gt;O(n&lt;span style="font-size:xx-small; vertical-align:top;"&gt;2&lt;/span&gt;)&lt;/font&gt; if the original grid is square), knowing it would return in milliseconds.&lt;/p&gt;  &lt;p&gt;I quickly realized that each iteration was not dependent any previous iteration for its calculations, so I decided it might be a good time to try out the new &lt;font face="Courier New"&gt;Parallel.For&lt;/font&gt; loops in the .NET 4 Framework that would multi-thread the iterations of the loops.&amp;#160; Here’s the implementation I threw together:&lt;/p&gt; &lt;span class="fullpost"&gt;   &lt;pre class="brush: csharp"&gt;using System;
using System.Linq;
using System.Threading.Tasks;

namespace Find34Grid
{
    public class Program
    {
        private static readonly int[][] Grid = new []
        {
            new [] {16,3,2,13,15,10,3,6,41,15,14,4,12,8,7,1,12},
            new [] {5,10,22,8,4,5,16,7,9,7,6,12,5,11,10,8,5},
            new [] {9,6,7,12,14,11,2,13,16,3,2,13,15,10,3,6,5},
            new [] {41,15,14,4,12,8,7,2,5,10,11,8,4,5,16,9,15},
            new [] {16,3,2,13,15,10,3,6,15,10,16,2,3,13,16,2,3},
            new [] {5,10,11,8,4,5,16,9,4,5,5,11,10,8,5,11,10},
            new [] {9,6,7,12,14,11,2,13,14,11,9,7,6,12,9,7,6},
            new [] {41,15,14,4,12,8,7,1,12,8,4,14,15,1,14,15,1},
            new [] {9,7,6,12,5,11,10,8,5,11,10,3,6,41,15,14,4},
            new [] {4,14,15,1,9,7,6,12,9,7,5,16,9,9,7,6,12},
            new [] {12,8,13,13,4,14,15,1,4,14,11,2,13,16,3,2,13}
        };

        private const int MagicSum = 34;

        public static void Main(string[] args)
        {
            Parallel.For(0, Grid.Length - 3, i =&amp;gt;
            {
                Parallel.For(0, Grid[i].Length - 3, j =&amp;gt;
                {
                    int[] sums = new []
                    {
                        Grid[i].Skip(j).Take(4).Sum(), // Row 1
                        Grid[i+1].Skip(j).Take(4).Sum(), // Row 2
                        Grid[i+2].Skip(j).Take(4).Sum(), // Row 3
                        Grid[i+3].Skip(j).Take(4).Sum(), // Row 4
                        new [] { Grid[i][j], Grid[i+1][j], Grid[i+2][j], Grid[i+3][j]}.Sum(), // Column 1
                        new [] { Grid[i][j+1], Grid[i+1][j+1], Grid[i+2][j+1], Grid[i+3][j+1]}.Sum(), // Column 2
                        new [] { Grid[i][j+2], Grid[i+1][j+2], Grid[i+2][j+2], Grid[i+3][j+2]}.Sum(), // Column 3
                        new [] { Grid[i][j+3], Grid[i+1][j+3], Grid[i+2][j+3], Grid[i+3][j+3]}.Sum(), // Column 4
                        new [] { Grid[i][j], Grid[i+1][j+1], Grid[i+2][j+2], Grid[i+3][j+3]}.Sum(), // Diagonal 1
                        new [] { Grid[i][j+3], Grid[i+1][j+2], Grid[i+2][j+1], Grid[i+3][j]}.Sum() // Diagonal 2
                    };

                    if (sums.All(x =&amp;gt; x == MagicSum))
                    {
                        Console.WriteLine(&amp;quot;y (row): {0}\nx (column): {1}&amp;quot;, i, j);
                    }
                });
            });

            Console.ReadLine();
        }
    }
}&lt;/pre&gt;
&lt;p&gt;Nothing special and certainly did the job.&amp;#160; I didn’t really notice a performance improvement in anyway, but that was expected for such a small problem.&amp;#160; I don’t really expect a whole lot of feedback on such straightforward code, but if there is a cleaner or more efficient way to utilize &lt;font face="Courier New"&gt;Parallel.For&lt;/font&gt; or &lt;font face="Courier New"&gt;Parallel.ForEach&lt;/font&gt;, please don’t hesitate to let me know via commenting to this post.&amp;#160; Thanks!&lt;/p&gt;
&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1664217632350055880-1982434384212077640?l=murrayon.net' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://murrayon.net/feeds/1982434384212077640/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://murrayon.net/2010/06/parallelfor-loops-in-net-4.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/1982434384212077640?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/1982434384212077640?v=2" /><link rel="alternate" type="text/html" href="http://murrayon.net/2010/06/parallelfor-loops-in-net-4.html" title="Parallel.For Loops in .NET 4" /><author><name>Michael Murray</name><uri>https://profiles.google.com/109073345657761090655</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-Xf7Ccmz4FCQ/AAAAAAAAAAI/AAAAAAAAAGU/jOfxxMc1Lxo/s512-c/photo.jpg" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;CUAHSH8yfip7ImA9WxBVF0w.&quot;"><id>tag:blogger.com,1999:blog-1664217632350055880.post-2654738557567584288</id><published>2010-02-20T17:55:00.001-07:00</published><updated>2010-02-20T17:55:39.196-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-20T17:55:39.196-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="New Technology" /><category scheme="http://www.blogger.com/atom/ns#" term="BDD" /><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="Testing" /><category scheme="http://www.blogger.com/atom/ns#" term="Anagram Code Kata Series" /><category scheme="http://www.blogger.com/atom/ns#" term="MSpec" /><category scheme="http://www.blogger.com/atom/ns#" term="Mocking" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="Visual Studio" /><category scheme="http://www.blogger.com/atom/ns#" term="Microsoft" /><category scheme="http://www.blogger.com/atom/ns#" term="Code Organization" /><category scheme="http://www.blogger.com/atom/ns#" term="SOLID" /><title>Anagram Code Kata Part 5 – Domain Objects Over Language Primitives</title><content type="html">&lt;p&gt;This post is part of a &lt;a href="http://murrayon.net/2009/11/anagram-code-kata-bdd-mspec.html"&gt;series on coding Kata, BDD, MSpec, and SOLID principles&lt;/a&gt;.&amp;#160; Feel free to visit the above link which points to the introductory post, also containing an index of all posts in the series.&lt;/p&gt;  &lt;p&gt;In this post, we will discuss some reasons why you might want to avoid using language primitives directly in place of domain objects.&amp;#160; Specifically, I have been using &lt;font face="Courier New"&gt;String&lt;/font&gt; variables and objects to represent words up to this point.&amp;#160; &lt;a href="http://adotnetdude.blogspot.com/"&gt;Esteban&lt;/a&gt; suggested I create a &lt;font face="Courier New"&gt;Word&lt;/font&gt; class for a few good reasons which I’ll layout for you.&amp;#160; Out of sheer luck and good timing, &lt;a href="http://codebetter.com/blogs/dru.sellers/default.aspx"&gt;Dru Sellers&lt;/a&gt; of &lt;a href="http://codebetter.com/"&gt;CodeBetter.com&lt;/a&gt; also wrote on this subject shortly afterward and confirmed Esteban’s reasoning.&lt;/p&gt; &lt;span class="fullpost"&gt;   &lt;h3&gt;Use &lt;font face="Courier New"&gt;String&lt;/font&gt; Class or Create &lt;font face="Courier New"&gt;Word&lt;/font&gt; Domain Object?&lt;/h3&gt;    &lt;p&gt;I mentioned in the previous post that Esteban had suggested creating a &lt;font face="Courier New"&gt;Word&lt;/font&gt; class instead of just passing around strings everywhere through my application.&amp;#160; One good reason is that we don’t own the &lt;font face="Courier New"&gt;String&lt;/font&gt; class and so further modification to our string-based implementation is difficult because the logic is scattered throughout the application, instead of centralized under the responsibility and definition of one domain class.&amp;#160; It is much more difficult to refactor the word-specific logic with its behaviors and attributes spread throughout the code.&amp;#160; Even if your &lt;font face="Courier New"&gt;Word&lt;/font&gt; class doesn’t grow any more beyond a seemingly unnecessary wrapper of the &lt;font face="Courier New"&gt;String&lt;/font&gt; class, the code is more cohesive and ready for change should the need arise.&lt;/p&gt;    &lt;p&gt;But more importantly, you designed the code thinking in a true object-oriented mindset.&amp;#160; You have to keep in mind that the &lt;font face="Courier New"&gt;String&lt;/font&gt; class is someone else’s implementation, and is hardly ever sufficient in and of itself as a domain object within your solution.&amp;#160; Think about it, the behaviors that a string object performs are so generic and multipurpose that you likely don’t need two-thirds of the class as defined (and there’s likely a few behaviors you really need that aren’t there).&amp;#160; Of course nearly every application on earth makes use of the &lt;font face="Courier New"&gt;String&lt;/font&gt; class; but because of this fact, it has no meaning in and of itself within any given application.&amp;#160; You would have to look at how the stings are actually used within the code in order to understand its unique application within the app’s context.&amp;#160; Of course that task of research is much easier for everyone (including the original author of the code) if it’s all encapsulated within a dedicated domain object class.&amp;#160; True object-orientation means describing in code form the properties, behaviors, and interactions/relationships of real world objects within your problem domain.&lt;/p&gt;    &lt;p&gt;Esteban gave me a great example to illustrate these points.&amp;#160; He said that you can always represent money as a decimal (and even when you use a domain object, it’s got to have a decimal language primitive underneath the covers).&amp;#160; However, what happens when you need to attach metadata to the amount (like currency denomination), or if you need to change decimal precision?&amp;#160; You would have to go through all of the code and make sure your use of decimal language primitives is modified uniformly in order to retain consistency.&amp;#160; Also, mathematic operations involving money are hardly ever the same as their counterparts involving standard decimals, because currency deals with discrete values to a certain decimal precision.&amp;#160; Typically when the behaviors and properties within our system begin to get complex, we are cognizant enough to create domain objects in order to bring it all under one class.&amp;#160; We definitely don’t want to over-architect features and interactions before we need them, but I think there is power in this principle of abstracting away language primitives and instead encapsulating their use within domain objects located in just one place in your codebase.&amp;#160; I believe it is one thing we can keep in mind to help guide us to better object-oriented thinking, and avoid language-oriented coding.&lt;/p&gt;    &lt;p&gt;As mentioned above, Esteban’s thoughts were confirmed nearly verbatim by &lt;a href="http://codebetter.com/blogs/dru.sellers/default.aspx"&gt;Dru Sellers&lt;/a&gt; of &lt;a href="http://codebetter.com/"&gt;CodeBetter.com&lt;/a&gt; in his blog post that he wrote just a few days after I had the conversation with Esteban.&amp;#160; A great coincidence no doubt, and worth a read; here’s the link:&lt;/p&gt;    &lt;p&gt;&lt;a href="http://codebetter.com/blogs/dru.sellers/archive/2010/01/27/business-primitives-1-2.aspx"&gt;Business Primitives (1/2)&lt;/a&gt;&amp;#160;&lt;/p&gt;    &lt;h3&gt;&lt;font face="Courier New"&gt;Word&lt;/font&gt; Class Implementation&lt;/h3&gt;    &lt;p&gt;So basically I created the following class implementation and then replaced string with a reference to this new class, &lt;font face="Courier new"&gt;Word&lt;/font&gt;:&lt;/p&gt;    &lt;pre class="brush: csharp"&gt;public class Word
{
    private string wordStr = string.Empty;

    public Word(string wordStr)
    {
        this.wordStr = wordStr;
    }

    public override string ToString()
    {
        return wordStr;
    }

    public override bool Equals(object obj)
    {
        return wordStr == ((Word)obj).ToString();
    }

    public override int GetHashCode()
    {
        return base.GetHashCode();
    }

    public int GetCanonicalHashCode()
    {
        char[] letters = wordStr.ToCharArray();
        Array.Sort&amp;lt;char&amp;gt;(letters);
        return new string(letters).GetHashCode();
    }
}&lt;/pre&gt;

  &lt;p&gt;I have defined an overridden implementation for &lt;font face="Courier New"&gt;Equals(object)&lt;/font&gt; (so that the test assertions and other &lt;font face="Courier New"&gt;IEnumerable.Contains()&lt;/font&gt; queries work) and &lt;font face="Courier New"&gt;GetHashCode()&lt;/font&gt; (solely to satisfy a compiler warning).&amp;#160; I also moved the &lt;font face="Courier New"&gt;GetCanoncialHashCode()&lt;/font&gt; method from &lt;font face="Courier New"&gt;AnagramGrouper&lt;/font&gt;, in order to better encapsulate it as a behavior a Word knows how to do innately.&lt;/p&gt;

  &lt;p&gt;One other change I had to make was to convert strings into &lt;font face="Courier New"&gt;Word&lt;/font&gt; objects in our &lt;font face="Courier New"&gt;NewlineFileParser&lt;/font&gt;, which I accomplished by using an &lt;font face="Courier New"&gt;IEnumberable.Select()&lt;/font&gt; call as shown below:&lt;/p&gt;

  &lt;pre class="brush: csharp"&gt;return File.ReadAllLines(filePath).Select&amp;lt;string, Word&amp;gt;(x =&amp;gt; new Word(x));&lt;/pre&gt;

  &lt;p&gt;&lt;/p&gt;

  &lt;h3&gt;Let’s Revisit &lt;font face="Courier New"&gt;AssertWasCalled&lt;/font&gt; One More Time&lt;/h3&gt;

  &lt;p&gt;Trust me, I am groaning with you, even as I wrote that heading text.&amp;#160; The last thing we need is for me to rehash the topic again and flip flop my stance yet another time.&amp;#160; Yes, that’s right I’ve changed my mind again.&amp;#160; First I couldn’t understand what utility asserting methods were called would have under normal test scenarios.&amp;#160; Then I changed my mind that perhaps using it would help my test specifications have clearer intent of what I am asserting.&amp;#160; After an email conversation with &lt;a href="http://www.davesquared.net/"&gt;David Tchepak&lt;/a&gt;, I think I’m now back to my original stance.&amp;#160; Here is what Dave said that had me reconsidering, and I think it’s pretty sound reasoning (emphasis added by me):&lt;/p&gt;

  &lt;blockquote&gt;
    &lt;p&gt;…&lt;/p&gt;

    &lt;p&gt;“But seeing as you asked for it, here goes. :)&lt;/p&gt;

    &lt;p&gt;“I try and avoid &lt;font face="Courier New"&gt;AssertWasCalled&lt;/font&gt; like the plague. Generally I don't care that some method was called, I care that my class does the work it needs to. If that involves calling a dependency then great, but that is not my class' reason for existence.&lt;/p&gt;

    &lt;p&gt;“I prefer your original approach of stubbing out everything in the setup and having that tested indirectly. One reason I prefer this is I find it makes it easier to refactor: the assertions in my test don't change, the class still does the same thing. However I can add or change dependencies by changing some wiring in the setup, and then make sure I haven't stuffed anything up as my assertions still pass. &lt;strong&gt;I prefer that my tests specify what I want, not how it does it.&lt;/strong&gt; To me, &lt;font face="Courier New"&gt;AssertWasCalled&lt;/font&gt; reeks of over-specification. The one exception is where I hit the external boundaries of my code, so where I want to send an email or something without side effects that I can test. Then the core of the behaviour is the call itself, so I'm happy to assert on that then.”&lt;/p&gt;
  &lt;/blockquote&gt;

  &lt;p&gt;And with that I’ll promise to never bring this up again…unless of course I get swayed by someone else. :)&amp;#160; In all seriousness, I think this is an interesting discussion and so if you have insight, please share via comment below.&lt;/p&gt;

  &lt;h3&gt;Summary&lt;/h3&gt;

  &lt;p&gt;Please leave your thoughts in the comments below in regard to creating domain objects over using language primitives (or heaven forbid, the &lt;font face="Courier New"&gt;AssertWasCalled&lt;/font&gt; debate).&amp;#160; As far as this coding kata exercise, I need to take a high-level look at where this should go next.&amp;#160; Perhaps the next post will tie up loose ends and see how our code performs on large text files as input.&amp;#160; It may be that we will need to refactor our architecture to achieve better runtimes.&amp;#160; If not, maybe we can still discuss where would could have headed if it had been necessary.&lt;/p&gt;
&lt;/span&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1664217632350055880-2654738557567584288?l=murrayon.net' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://murrayon.net/feeds/2654738557567584288/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://murrayon.net/2010/02/anagram-code-kata-part-5-domain-objects.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/2654738557567584288?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/2654738557567584288?v=2" /><link rel="alternate" type="text/html" href="http://murrayon.net/2010/02/anagram-code-kata-part-5-domain-objects.html" title="Anagram Code Kata Part 5 – Domain Objects Over Language Primitives" /><author><name>Michael Murray</name><uri>https://profiles.google.com/109073345657761090655</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-Xf7Ccmz4FCQ/AAAAAAAAAAI/AAAAAAAAAGU/jOfxxMc1Lxo/s512-c/photo.jpg" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;CkcARngzcSp7ImA9WxBXFUo.&quot;"><id>tag:blogger.com,1999:blog-1664217632350055880.post-8196351134851458941</id><published>2010-01-26T22:27:00.001-07:00</published><updated>2010-01-26T22:27:27.689-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-01-26T22:27:27.689-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="BDD" /><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="MSpec" /><category scheme="http://www.blogger.com/atom/ns#" term="Code Organization" /><category scheme="http://www.blogger.com/atom/ns#" term="Microsoft" /><category scheme="http://www.blogger.com/atom/ns#" term="New Technology" /><category scheme="http://www.blogger.com/atom/ns#" term="Testing" /><category scheme="http://www.blogger.com/atom/ns#" term="Anagram Code Kata Series" /><category scheme="http://www.blogger.com/atom/ns#" term="Mocking" /><category scheme="http://www.blogger.com/atom/ns#" term="Visual Studio" /><category scheme="http://www.blogger.com/atom/ns#" term="Algorithms" /><category scheme="http://www.blogger.com/atom/ns#" term="SOLID" /><title>Anagram Code Kata Part 4 – Will it Write My Code for Me?</title><content type="html">&lt;p&gt;This post is part of a &lt;a href="http://murrayon.net/2009/11/anagram-code-kata-bdd-mspec.html"&gt;series on coding Kata, BDD, MSpec, and SOLID principles&lt;/a&gt;.&amp;#160; Feel free to visit the above link which points to the introductory post, also containing an index of all posts in the series.&lt;/p&gt;  &lt;p&gt;In this post, we work on implementing the other main dependency, &lt;font face="Courier New"&gt;IAnagramsFinder&lt;/font&gt;.&amp;#160; In doing so, you will discover that I have been a little naïve with this BDD methodology. Apparently I expected a different experience than what I ran into doing the exercise.&amp;#160; In gathering feedback, I had a very interesting discussion about Object-Oriented Programming with &lt;a href="http://adotnetdude.blogspot.com/"&gt;Esteban&lt;/a&gt; that I will summarize and ask for further discussion from all of you.&amp;#160; I’ll also show you one way to debug your code when running the MSpec tests via the console test runner.&lt;/p&gt; &lt;span class="fullpost"&gt;   &lt;h3&gt;New Thoughts on Mocks vs. Stubs&lt;/h3&gt;    &lt;p&gt;During &lt;a href="http://murrayon.net/2009/11/anagram-code-kata-part-2-mocking-and.html"&gt;Part 2 of this series&lt;/a&gt;, &lt;a href="http://papamufflon.blogspot.com/"&gt;Tobias&lt;/a&gt; left &lt;a href="http://murrayon.net/2009/11/anagram-code-kata-part-2-mocking-and.html#comments"&gt;a comment about using mocks and &lt;font face="Courier New"&gt;AssertWasCalled&lt;/font&gt;&lt;/a&gt;, rather than stubs and defining fake responses.&amp;#160; I didn’t understand how it would help, as it appeared they would accomplish roughly the same thing.&amp;#160; With stubs and fake responses, if you don’t get back the return you are looking for, then you can infer that the dependencies weren’t called since you had previously hard-coded their return values when called.&amp;#160; With mocks and asserting methods were called, you directly assert that the manager class did in fact call it’s dependencies.&lt;/p&gt;    &lt;p&gt;When I looked more closely into using the &lt;font face="Courier New"&gt;AssertWasCalled&lt;/font&gt; method in Rhino.Mocks, I noticed that my strategy of using stubs and faked return values wasn’t very explicit in signifying that the ultimate responsibility of the manager class was to delegate to its dependencies.&amp;#160; I mean the test context does rigorously stub out the dependencies, but the actual assertions weren’t clear what we were testing.&amp;#160; Therefore, I decided to pop in a pair of &lt;font face="Courier New"&gt;AssertWasCalled&lt;/font&gt; statements (as found below), leaving &lt;a href="http://murrayon.net/2009/11/anagram-code-kata-part-2-mocking-and.html#firstSpec"&gt;the rest of the test the same as it was back in Part 2&lt;/a&gt;.&lt;/p&gt;    &lt;pre class="brush: csharp"&gt;It should_result_in_list_of_anagram_sets_and_both_dependencies_should_have_been_called = () =&amp;gt;
{
    fileParser.AssertWasCalled(x =&amp;gt; x.ExtractWordListFromFile(filePath));
    anagramGrouper.AssertWasCalled(x =&amp;gt; x.FindAnagramSets(wordListFromFile));

    result.ShouldEqual(expected);
};&lt;/pre&gt;

  &lt;p&gt;However, when I thought to simplify the test code by removing most of stubbing setup, it appeared I had to leave most of the stub logic in tact in order to test if &lt;font face="Courier New"&gt;anagramGrouper.FindAnagramSets()&lt;/font&gt; was called with parameter &lt;font face="Courier New"&gt;wordListFromFile&lt;/font&gt;, which was one of the stubbed fake responses.&amp;#160; The one dependency (the file parser) needed to pass it’s return value to the next dependency (the anagrams finder).&amp;#160; I could not think of a way to accomplish testing that the manager class facilitated that without the stubbing logic (fake responses) that I created.&lt;/p&gt;

  &lt;p&gt;It would be great if someone could enlighten me since I’m such a newbie to mocking and stubbing.&amp;#160; Otherwise, I feel the intent of the test is now more clear with the &lt;font face="Courier New"&gt;AssertWasCalled&lt;/font&gt; checks that I added, even if that I means I’m needlessly mixing stubbing and mocking.&amp;#160; But one last question:&amp;#160; should I try to stick more closely to the rule of thumb guiding me to stick to one all-encompassing assertion per test if possible?&amp;#160; Let me know your thoughts.&lt;/p&gt;

  &lt;h3&gt;Implementing the &lt;font face="Courier New"&gt;AnagramGrouper&lt;/font&gt; Dependency&lt;/h3&gt;

  &lt;p&gt;So let’s continue onward with some more actual design and code.&amp;#160; We are tackling the &lt;font face="Courier New"&gt;AnagramGrouper&lt;/font&gt; dependency, whose sole responsibility is to take in the parsed collection of words and find anagram sets.&amp;#160; Here is the test spec (one thing to note is that in some of my earlier tests for the &lt;font face="Courier New"&gt;AnagramsFinder&lt;/font&gt; manager class and &lt;font face="Courier New"&gt;IFileParser&lt;/font&gt;, I had used string arrays liberally; I decided to go with &lt;font face="Courier New"&gt;IList&amp;lt;string&amp;gt;&lt;/font&gt; collections instead):&lt;/p&gt;

  &lt;pre class="brush: csharp"&gt;public class AnagramGrouperSpecs
{
    [Subject(typeof(AnagramGrouperSpecs))]
    public class when_given_word_list_collection
    {
        static AnagramGrouper sut;
        static IEnumerable&amp;lt;IList&amp;lt;string&amp;gt;&amp;gt; result;
        static IEnumerable&amp;lt;string&amp;gt; wordList = new[] { &amp;quot;wordA&amp;quot;, &amp;quot;wordB&amp;quot;, &amp;quot;Aword&amp;quot;, &amp;quot;wordC&amp;quot; };

        Establish context = () =&amp;gt;
        {
            sut = new AnagramGrouper();
        };

        Because of = () =&amp;gt;
        {
            result = sut.FindAnagramSets(wordList);
        };

        It should_result_in_anagram_sets_collection_of_length_1 = () =&amp;gt;
        {
            result.Count().ShouldEqual(1);
        };

        It should_contain_2_specific_words_in_the_anagram_set = () =&amp;gt;
        {
            result.First().ShouldContain(&amp;quot;wordA&amp;quot;, &amp;quot;Aword&amp;quot;);
        };
    }
}&lt;/pre&gt;

  &lt;p&gt;Here too I have basically two assertions (but split it up differently than last time); should I also try harder to avoid this as well?&lt;/p&gt;

  &lt;p&gt;When contemplating how to further breakdown this responsibility of finding anagram sets into subtasks, I figure we would need one piece of code that would compare two words and answer whether they are anagrams of each other, and another piece of code that would keep track of the various anagram sets (perhaps a collection or dictionary of string arrays).&amp;#160; With what BDD has revealed to me thus far, I figure this means we are going to see two more dependencies (and interfaces, and spec tests) added to the codebase.&lt;/p&gt;

  &lt;p&gt;So I start with the word comparison task, thinking I could perhaps leverage the &lt;font face="Courier New"&gt;String.GetHashCode()&lt;/font&gt; method for assigning meaningful value to the list of characters in the string.&amp;#160; However, string hash codes give importance to letter casing and order of characters, so a modified strategy would have to be utilized, even though &lt;font face="Courier New"&gt;GetHashCode()&lt;/font&gt; seems quite close to filling our need.&lt;/p&gt;

  &lt;p&gt;Now I probably should have come up with this ingenious algorithm all by myself, but…it is what it is and this is the best, most concise solution that I can put together to solve the problem.&amp;#160; With that said, I ended up doing some internet searching in order to proof-of-concept my &lt;font face="Courier New"&gt;GetHashCode()&lt;/font&gt; idea.&amp;#160; I came across pure genius in the form of &lt;a href="http://www.formatexception.com/"&gt;Brian Mullen’s&lt;/a&gt; blog post entitled &lt;a href="http://www.formatexception.com/2009/03/linq-group-by-and-groupby/"&gt;LINQ group by and GroupBy&lt;/a&gt;, specifically the second-to-last code snippet.&amp;#160; Just &lt;font face="Courier New"&gt;String.ToCharArray()&lt;/font&gt; the word and then &lt;font face="Courier New"&gt;Array.Sort()&lt;/font&gt; the resultant array; now turn it back into a string and do &lt;font face="Courier New"&gt;GetHashCode()&lt;/font&gt; now.&amp;#160; (As a side note, we can worry about case-insensitivity later if desired by simply doing a &lt;font face="Courier New"&gt;String.ToLower()&lt;/font&gt; on the word before getting the hash code.)&amp;#160; Now anagrams will have the same hash code (Brian uses the term “canonical”, which I think is very fitting); it’s literally perfect in every way!&lt;/p&gt;

  &lt;p&gt;So now for the other dependency, I figure we could use a &lt;font face="Courier New"&gt;Dictionary&amp;lt;int, IList&amp;lt;string&amp;gt;&amp;gt;&lt;/font&gt;, where the key is the canonical hash code and the value is a string list collection representing an anagram set of words.&amp;#160; These two dependency implementations are so simple and straight forward that I have a hard time seeing a reason to continue breaking out full-fledged object dependencies with interfaces and Dependency Injection.&amp;#160; However, I will make a case for moving the canonical hash code logic outside this &lt;font face="Courier New"&gt;AnagramsGrouper&lt;/font&gt; class, but that will come later.&lt;/p&gt;

  &lt;p&gt;So let’s write some code to pass our test/spec:&lt;/p&gt;

  &lt;pre class="brush: csharp"&gt;public class AnagramGrouper : IAnagramGrouper
{
    public IEnumerable&amp;lt;IList&amp;lt;string&amp;gt;&amp;gt; FindAnagramSets(IEnumerable&amp;lt;string&amp;gt; wordList)
    {
        Dictionary&amp;lt;int, IList&amp;lt;string&amp;gt;&amp;gt; results = new Dictionary&amp;lt;int, IList&amp;lt;string&amp;gt;&amp;gt;();

        foreach (string word in wordList)
        {
            int canonicalHashCode = GetCanonicalHashCode(word);

            if (results.ContainsKey(canonicalHashCode))
            {
                results[canonicalHashCode].Add(word);
            }
            else
            {
                results.Add(canonicalHashCode, new List&amp;lt;string&amp;gt;(new[] { word }));
            }
        }

        return results.Values;
    }

    public static int GetCanonicalHashCode(string word)
    {
        char[] letters = word.ToCharArray();
        Array.Sort&amp;lt;char&amp;gt;(letters);

        return new string(letters).GetHashCode();
    }
}&lt;/pre&gt;

  &lt;p&gt;Maybe you noticed already, but there is a bug in my logic.&amp;#160; At that time, I didn’t see it and needed to debug the code while running.&amp;#160; Setting a debug break point in your code doesn’t work like you’re probably used to, and this is because we’re running the MSpec tests via its console test runner.&amp;#160; I had to do some searching to figure this one out, and that’s what we’ll cover next.&lt;/p&gt;

  &lt;h3&gt;How to Debug MSpec Specifications and the Code They Test&lt;/h3&gt;

  &lt;p&gt;After a short ride on the Google super highway, I found a &lt;a href="http://codebetter.com/blogs/aaron.jensen/archive/2008/09/02/mspec-v0-2.aspx#comments"&gt;blog post containing a November 2009 comment&lt;/a&gt; from &lt;a href="http://codebetter.com/blogs/aaron.jensen/default.aspx"&gt;Aaron Jensen&lt;/a&gt;, author of MSpec, that gave a few possible solutions.&amp;#160; I opted to go with the &lt;font face="Courier new"&gt;Debugger.Break()&lt;/font&gt; solution, which I put at the very top of my &lt;font face="Courier new"&gt;FindAnagramSets&lt;/font&gt; method.&amp;#160; It is a tad awkward because it will show a Windows error dialog box that says the program encountered a problem and Windows is searching the web for a solution.&amp;#160; If you wait through that, it will give you an option to debug the program via a Visual Studio dialog where you select the currently running instance of Visual Studio as your debugger instance.&amp;#160; You just remove the statement when you’re done (and you’ll really want to remember to remove it from your production code).&amp;#160; Does the trick, though it is a bit obtrusive to your code base.&lt;/p&gt;

  &lt;p&gt;If you haven’t already figured it out, the bug is that my &lt;font face="Courier New"&gt;FindAnagramSets&lt;/font&gt; method is returning all anagram sets found in the dictionary.&amp;#160; An anagram set of just one word is not really an anagram set, which is why our test specification which is based on that assumption failed.&amp;#160; The fix is to filter out anagram word sets with less than two words before returning, like this:&lt;/p&gt;

  &lt;pre class="brush: csharp"&gt;// Return anagram sets with more than one word in it
return results.Values.Where(x =&amp;gt; x.Count &amp;gt; 1);&lt;/pre&gt;

  &lt;p&gt;That will make the code pass the tests now.&amp;#160; Remember to remove the debugger statement we added.&lt;/p&gt;

  &lt;h3&gt;BDD, OOP…WTH?!&lt;/h3&gt;

  &lt;p&gt;Just kidding.&amp;#160; But I do want to discuss a reaction I had to BDD after implementing this anagram finding solution.&amp;#160; After I completed the code and finished the tests, I was feeling pretty great.&amp;#160; But then I started thinking about how I didn’t really go down the path that I felt BDD was originally trying to lead me down with &lt;font face="Courier New"&gt;AnagramGrouper&lt;/font&gt;, which was to create two more dependencies, just as I had done for the first managing class &lt;font face="Courier New"&gt;AnagramsFinder&lt;/font&gt;.&amp;#160; I mean I felt pretty frustrated because I was digging BDD to this point.&amp;#160; I’m not quite sure what my original expectations were, but I might have thought BDD was magically going to right my algorithms for me as well.&lt;/p&gt;

  &lt;p&gt;I decided to discuss the whole thing with &lt;a href="http://adotnetdude.blogspot.com/"&gt;Esteban&lt;/a&gt; to get some perspective on the issue.&amp;#160; He told me he didn’t know a thing about BDD and how it’s supposed to work, but he figured BDD would lead me to decoupled, cohesive code design, but he couldn’t see how it would aide with algorithmic solutions to the problems at hand.&amp;#160; I’m not sure why I was so naïve, but it makes complete sense.&amp;#160; I mean these methodologies are designed to keep you disciplined to some proven, guiding principles, but you still need to use your brain and creativity to actually solve the problem.&amp;#160; I guess it would help to get some feedback and suggestions from you all in regard to knowing when it is time to switch from BDD architecture design mode to algorithm design mode within the broken down dependencies that handle the very specific sub-responsibilities.&amp;#160; Please let me know if you have found some guiding concept or question you’ve come up with that you put to the test in order to lead you into the right coding mindset for that particular context (design or algorithm; or do I even have the right categories?).&lt;/p&gt;

  &lt;p&gt;Perhaps it’s just that this design process feels so new to me.&amp;#160; Maybe I have solely been in the coding, algorithm mindset this whole time throughout my career.&amp;#160; Perhaps it will become more natural with practice and experience.&amp;#160; I guess it just feels like I’m learning to drive a manual, stick-shift car or something, and I am a little rough with the clutch when transitioning between gears.&lt;/p&gt;

  &lt;p&gt;To take this discussion further, &lt;a href="http://adotnetdude.blogspot.com/"&gt;Esteban&lt;/a&gt; made a comment in our conversation about his personal thoughts on these methodologies (like BDD) and Object-Oriented Programming.&amp;#160; He has come to believe that we invent these methodologies like flavors of the week “because as programmers we don’t really get OOP.”&amp;#160; Perhaps OOP just comes more naturally to his thought process that most people (or he admits it’s possible he could just be completely ignorant), but he feels that there’s really just a few simple rules and guidelines to keep in mind at all times in order to keep your objects behaving like real life objects and not acting like data constructs.&amp;#160; I encouraged him to give his theory some additional thought and make it a little more formal by writing it down.&amp;#160; He did this and the following is a link to the resultant blog post:&lt;/p&gt;

  &lt;p&gt;&lt;a href="http://adotnetdude.blogspot.com/2010/01/3-simple-rules-to-good-object-oriented.html"&gt;3 Simple Rules to Good Object Oriented Code&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;Please read through that post and comment on it.&amp;#160; His idea needs to be criticized and/or validated.&amp;#160; He admits he may have no clue what he’s talking about, but I said that’s why it needs to be put to the test by posting it in the open.&amp;#160; We both think it’s an interesting theory, but we’re unsure if it even holds any water.&amp;#160; Perhaps Esteban’s theory is too simplistic to be practical for many programmers.&amp;#160; Provide feedback and lots of it.&lt;/p&gt;

  &lt;h3&gt;Summary&lt;/h3&gt;

  &lt;p&gt;So I think I need to wrap this post up about now.&amp;#160; I think I have raised enough discussion points to write a book, but please do take the time to comment on what I’m doing wrong, what I’m doing right, or how to make the BDD mentality more flowing and natural.&amp;#160; I promised to talk about having a Word class as opposed to using primitive strings everywhere, and that will definitely be in the next post.&amp;#160; Also, I think our &lt;font face="Courier New"&gt;AnagramGrouper&lt;/font&gt; class may need some more testing and flushing out of how it should handle spaces, punctuation, casing, and so on.&lt;/p&gt;

  &lt;p&gt;Thanks for sticking with me thus far.&lt;/p&gt;
&lt;/span&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1664217632350055880-8196351134851458941?l=murrayon.net' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://murrayon.net/feeds/8196351134851458941/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://murrayon.net/2010/01/anagram-code-kata-part-4-will-it-write.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/8196351134851458941?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/8196351134851458941?v=2" /><link rel="alternate" type="text/html" href="http://murrayon.net/2010/01/anagram-code-kata-part-4-will-it-write.html" title="Anagram Code Kata Part 4 – Will it Write My Code for Me?" /><author><name>Michael Murray</name><uri>https://profiles.google.com/109073345657761090655</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-Xf7Ccmz4FCQ/AAAAAAAAAAI/AAAAAAAAAGU/jOfxxMc1Lxo/s512-c/photo.jpg" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;D0cAR3s9fyp7ImA9WxBSEUU.&quot;"><id>tag:blogger.com,1999:blog-1664217632350055880.post-8576876761767361973</id><published>2009-12-18T18:10:00.001-07:00</published><updated>2009-12-18T18:10:46.567-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-12-18T18:10:46.567-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="New Technology" /><category scheme="http://www.blogger.com/atom/ns#" term="BDD" /><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="Testing" /><category scheme="http://www.blogger.com/atom/ns#" term="Anagram Code Kata Series" /><category scheme="http://www.blogger.com/atom/ns#" term="MSpec" /><category scheme="http://www.blogger.com/atom/ns#" term="Mocking" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="Visual Studio" /><category scheme="http://www.blogger.com/atom/ns#" term="Microsoft" /><category scheme="http://www.blogger.com/atom/ns#" term="SOLID" /><title>Anagram Code Kata Part 3 – Use Common Sense</title><content type="html">&lt;p&gt;This post is part of a &lt;a href="http://murrayon.net/2009/11/anagram-code-kata-bdd-mspec.html"&gt;series on coding Kata, BDD, MSpec, and SOLID principles&lt;/a&gt;.&amp;#160; Feel free to visit the above link which points to the introductory post, also containing an index of all posts in the series.&lt;/p&gt;  &lt;p&gt;In this post, we will discuss how far to take the idea of creating mockable contracts via interfaces for testing purposes and to keep responsibilities modular.&amp;#160; As I solicited feedback from commenters, I was getting hung up on applying this concept to every object and dependency.&amp;#160; We’ll talk about how I’m now thinking that overly strict adherence to the practice can drive you nuts and decrease productivity.&amp;#160; It is meant to help guide your design process along rather than hinder it.&amp;#160; We’ll finish this post off with one of our two dependencies implemented in an extremely simple manner.&lt;/p&gt; &lt;span class="fullpost"&gt;   &lt;h3&gt;&lt;font face="Courier New"&gt;IFileParser&lt;/font&gt; Dependency&lt;/h3&gt;    &lt;p&gt;When we left off last time, we had defined two separate responsibilities that &lt;font face="Courier New"&gt;AnagramsFinder&lt;/font&gt; was going to push down into its dependencies.&amp;#160; As I started to write a specification test for the newly created &lt;font face="Courier New"&gt;NewlineFileParser&lt;/font&gt;, I realized it would be difficult to stub based on providing a file path to a text file as the only parameter.&amp;#160; I began having thoughts that maybe I could break up another pair of dependencies for file parsing, namely obtaining a file stream (or file contents string) from a path and then parsing the file contents into a list of multiple lines.&amp;#160; But then again, my first of the two dependencies is still dependent on the physical file system and I cannot get away from this unless I somehow mock up an in-memory file system.&amp;#160; This began to feel silly to me as I was hoping I would just use the built-in .NET APIs for file systems and string parsing.&lt;/p&gt;    &lt;p&gt;After emailing &lt;a href="http://www.davesquared.net/" target="_blank"&gt;Dave&lt;/a&gt; and &lt;a href="http://adotnetdude.blogspot.com/" target="_blank"&gt;Esteban&lt;/a&gt; asking for guidance, the revelation I received via these two fine gentlemen is that file parsing is not an essential purpose of this application nor exercise (at least not yet; we won’t be focusing on performance unless it appears to be grossly deficient as we go along).&amp;#160; What we could do is write integration tests instead, where we have a set of simple test files sitting in a folder that we run our &lt;font face="Courier New"&gt;NewlineFileParser&lt;/font&gt; against to ensure it working as we expect.&amp;#160; But I don’t think I’m even going to do this at this point.&amp;#160; I really do want to get to the core of the application.&amp;#160; Plus, I feel certain that I can trust the implementation of &lt;font face="Courier New"&gt;File.ReadAllLines()&lt;/font&gt; from the .NET Framework.&lt;/p&gt;    &lt;p&gt;Unless it needs to be changed further down the road, the following code implementation will likely be sufficient for the remainder of this coding exercise:&lt;/p&gt;    &lt;pre class="brush: csharp"&gt;public class NewlineFileParser : IFileParser
{
    public IEnumerable&amp;lt;string&amp;gt; ExtractWordListFromFile(string filePath)
    {
        return File.ReadAllLines(filePath);
    }
}&lt;/pre&gt;

  &lt;p&gt;I feel great about this because I haven’t gone insane trying to mock and test everything in sight, nor have I broken up every single responsibility in such small chunks that it becomes tedious.&amp;#160; Instead, I have broken up the two core responsibilities of &lt;font face="Courier New"&gt;AnagramsFinder&lt;/font&gt; which means I can change and test the two dependencies independent of each other and from their manager class.&amp;#160; Even though I haven’t written a ton of code yet, I feel the tests specs have driven my design into a cohesive, decoupled design that should prove flexible if requirements change or if I encounter friction later on and need to rethink my design.&amp;#160; A flexible architecture for the application is being flushed out based on a “top-to-bottom” design view of the requirements.&lt;/p&gt;

  &lt;h3&gt;Create Stubs or Assert Methods were Called&lt;/h3&gt;

  &lt;p&gt;&lt;a href="http://papamufflon.blogspot.com/"&gt;Tobias Walter&lt;/a&gt; left &lt;a href="http://murrayon.net/2009/11/anagram-code-kata-part-2-mocking-and.html#comments"&gt;a comment on my last post&lt;/a&gt; with some suggestions that I would like to get clarification on and discuss further with my growing audience of three now.&amp;#160; He compared some of my implementation to &lt;a href="http://www.davesquared.net/2009/11/favour-test-driving-logic-over-data.html"&gt;Dave’s implementation on his Favour test driving logic over data post&lt;/a&gt;, but the comparison was lost on me.&amp;#160; It seemed Tobias was saying that I had gone too far breaking up the responsibilities into such small dependencies.&amp;#160; I am unsure what his argument was, but as far as I could tell both Dave and I broke up a managing class into two separate responsibilities so that the more crucial of the two could be tested independently.&amp;#160; Also in doing so, we both created stubs that were given specific dummy responses to method calls using Rhino.Mocks.&amp;#160; One more comment I would like to say is that I broke the file parsing responsibility away into its own dependency more because of the need to isolate the anagrams grouping than because file parsing is of itself a difficult task.&amp;#160; If Tobias or anyone else could try to clarify his argument, that would really help me understand the merits of his suggestions.&lt;/p&gt;

  &lt;p&gt;Another suggestion that Tobias brought up that I would like to petition feedback on is to use the mocking feature of testing whether a method was called instead of creating a stub programmed with a dummy response.&amp;#160; I have to admit, I’m not sure when to use one method over another.&amp;#160; I guess I’ve felt in the past that testing whether a method was called seems silly, since it seems odd to have your test know the implementation and inner method calls of the subject under test.&amp;#160; But I am now realizing that my stubs appear to be doing the same thing, but with dummy input and output programmed in.&amp;#160; If you have an opinion or experience on this subject, please leave some feedback in the comments of this post.&lt;/p&gt;

  &lt;h3&gt;What’s Next?&lt;/h3&gt;

  &lt;p&gt;In my next post I hope to summarize this mocking debate brought up above regarding two differing methods of verifying my manager class is correctly managing its dependencies.&amp;#160; I also hope to begin driving out the design and implementation of the &lt;font face="Courier New"&gt;AnagramGrouper&lt;/font&gt; dependency via behavior specification tests.&amp;#160; Thanks and any feedback is greatly appreciated.&lt;/p&gt;
&lt;/span&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1664217632350055880-8576876761767361973?l=murrayon.net' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://murrayon.net/feeds/8576876761767361973/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://murrayon.net/2009/12/anagram-code-kata-part-3-use-common.html#comment-form" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/8576876761767361973?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/8576876761767361973?v=2" /><link rel="alternate" type="text/html" href="http://murrayon.net/2009/12/anagram-code-kata-part-3-use-common.html" title="Anagram Code Kata Part 3 – Use Common Sense" /><author><name>Michael Murray</name><uri>https://profiles.google.com/109073345657761090655</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-Xf7Ccmz4FCQ/AAAAAAAAAAI/AAAAAAAAAGU/jOfxxMc1Lxo/s512-c/photo.jpg" /></author><thr:total>2</thr:total></entry><entry gd:etag="W/&quot;D0YNQnkycCp7ImA9WxBXEUQ.&quot;"><id>tag:blogger.com,1999:blog-1664217632350055880.post-1713026628293781613</id><published>2009-11-17T00:59:00.000-07:00</published><updated>2010-01-22T14:19:53.798-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-01-22T14:19:53.798-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="BDD" /><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="Dependency Injection" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="MSpec" /><category scheme="http://www.blogger.com/atom/ns#" term="Code Organization" /><category scheme="http://www.blogger.com/atom/ns#" term="Microsoft" /><category scheme="http://www.blogger.com/atom/ns#" term="New Technology" /><category scheme="http://www.blogger.com/atom/ns#" term="Testing" /><category scheme="http://www.blogger.com/atom/ns#" term="Anagram Code Kata Series" /><category scheme="http://www.blogger.com/atom/ns#" term="Mocking" /><category scheme="http://www.blogger.com/atom/ns#" term="Visual Studio" /><category scheme="http://www.blogger.com/atom/ns#" term="SOLID" /><title>Anagram Code Kata Part 2 – Mocking and SRP</title><content type="html">&lt;p&gt;This post is part of a &lt;a href="http://murrayon.net/2009/11/anagram-code-kata-bdd-mspec.html"&gt;series on coding Kata, BDD, MSpec, and SOLID principles&lt;/a&gt;.&amp;#160; Feel free to visit the above link which points to the introductory post, also containing an index of all posts in the series.&lt;/p&gt;  &lt;p&gt;In this post, we will rewrite our &lt;a href="http://murrayon.net/2009/11/anagram-code-kata-part-1-getting.html" target="_blank"&gt;first specification/test from the previous post&lt;/a&gt;, where we didn’t feel very confident about the direction we were heading.&amp;#160; We got some great feedback and will now better &lt;a href="http://www.davesquared.net/2009/11/favour-test-driving-logic-over-data.html" target="_blank"&gt;focus on test driving logic and not just data&lt;/a&gt;; we will also be more careful in extracting the requirements from problem statement and in giving our classes a &lt;a href="http://www.davesquared.net/2009/01/introduction-to-solid-principles-of-oo.html" target="_blank"&gt;Single Responsibility (the first of the SOLID principles of Object-Oriented Design)&lt;/a&gt;.&amp;#160; As we are test driving our design from a “top-down” perspective, we will encounter the need for dependencies not yet implemented while creating the higher level classes.&amp;#160; Instead of halting our rhythm and progress, we will utilize a mocking framework to stub out non-existent implementations according to interfaces.&amp;#160; This also encourages us to use &lt;a href="http://www.davesquared.net/2009/01/introduction-to-solid-principles-of-oo.html" target="_blank"&gt;Dependency Inversion (last of the SOLID principles; same link as above)&lt;/a&gt; and we can prove the validity of our modules by testing them in complete isolation from their dependencies.&lt;/p&gt; &lt;span class="fullpost"&gt;   &lt;h3&gt;Don’t Focus on Data (Yet) and SRP (Single Responsibility Principle)&lt;/h3&gt;    &lt;p&gt;I had a feeling I was focusing too much on testing data, and I also knew it seemed to odd to be writing my very first test to be dependent on solving a specific instance of the problem (especially a test case with thousands of words in it), instead of working on solving the problem generally.&amp;#160; After some terrific feedback from &lt;a href="http://www.davesquared.net/" target="_blank"&gt;David Tchepak&lt;/a&gt; in the &lt;a href="http://murrayon.net/2009/11/anagram-code-kata-part-1-getting.html#comments" target="_blank"&gt;comments of my last post&lt;/a&gt;, I now understand that wasn’t my only mistake.&amp;#160; I was also trying to give my &lt;font face="Courier New"&gt;AnagramsFinder&lt;/font&gt; class too many responsibilities (especially with just one &lt;font face="Courier New"&gt;ParseTextFile&lt;/font&gt; method), namely extracting the words out of the text file and then grouping the anagrams together.&amp;#160; Not only that, I also was returning a count of anagram sets when the &lt;a href="http://codekata.pragprog.com/2007/01/kata_six_anagra.html" target="_blank"&gt;problem statement specifically asked for output of the anagram sets themselves&lt;/a&gt;.&amp;#160; Even though this is a simple problem scenario, it can be foolish to make assumptions or simplifications like I did that are not part of the requirements.&lt;/p&gt;    &lt;p&gt;Let us first focus on the responsibilities required of our &lt;font face="Courier New"&gt;AnagramsFinder&lt;/font&gt; class.&amp;#160; It needs to parse a list of words out of a text file given the file path and it also needs to group words together into sets that are anagrams of each other.&amp;#160; However, we just named two responsibilities; implementing both in the same class would make it less cohesive and therefore less maintainable.&amp;#160; This is because our class would have more than one responsibility that could change in the future, and because these responsibilities are in the same class, they could be considered coupled and one could be affected by modifications to the other.&amp;#160; All of this train of thought falls under the Single Responsibility Principle from the SOLID principles (see links at the beginning of the post).&lt;/p&gt;    &lt;p&gt;To solve this predicament, we will make &lt;font face="Courier New"&gt;AnagramsFinder&lt;/font&gt; a managing class of dependencies that individually solve these separate concerns.&amp;#160; Each of these dependencies will adhere to the Single Responsibility principle, as does this higher abstraction manager class.&amp;#160; Its responsibility can be summarized as managing the inputs, outputs, and execution order of its dependencies.&amp;#160; We shall name the dependencies directly after each of their responsibilities, namely &lt;font face="Courier New"&gt;FileParser&lt;/font&gt; and &lt;font face="Courier New"&gt;AnagramGrouper&lt;/font&gt;.&amp;#160; However, I don’t want to go implement these dependencies and throw off the flow of fleshing out the design and logic of my manager class.&amp;#160; We are trying to design from a more “top-down” approach, instead of focusing on the lower level data concerns of the solution too early in the design process.&amp;#160; To accomplish completing the design of our manager class without actually implementing the dependencies, we will code to interfaces (namely &lt;font face="Courier New"&gt;IFileParser&lt;/font&gt; and &lt;font face="Courier New"&gt;IAnagramGrouper&lt;/font&gt;).&lt;/p&gt;    &lt;h3&gt;DIP (Dependency Inversion Principle)&lt;/h3&gt;    &lt;p&gt;The advantages of using interfaces are that we abstract out the actual implementation of the dependencies, allowing us to be more modular and less coupled between dependencies.&amp;#160; One class is not strongly tied to a specific implementation of its dependency, but rather the general contracts made available by the interface’s defined method signatures.&amp;#160; What this really means is that we can swap out actual implementations of the dependency without the consuming class being modified in the least bit.&amp;#160; This makes for truly maintainable code, especially when requirements change down the road after version one of the application is up and running.&amp;#160; This is what the Dependency Inversion Principle (again, from SOLID) is all about.&lt;/p&gt;    &lt;p&gt;We can take this a step further by passing the necessary dependencies into our consuming class at construction.&amp;#160; This allows for us to “inject” into the object any implementation of the interface we see fit; this technique is aptly named Dependency Injection (DI).&amp;#160; Our manager class doesn’t have to fret about any concrete details regarding its dependencies’ actual implementations, nor about any of the construction ceremony associated with “newing” up and initializing said dependencies.&amp;#160; We won’t be using any DI containers (as our dependency needs are very light in this exercise), but the use of the Dependency Inversion Principle does set us up nicely to use a mocking framework so that we can finish fleshing out our tests without implementing any of the dependencies yet.&lt;/p&gt;    &lt;h3&gt;Mocking Dependencies&lt;/h3&gt;    &lt;p&gt;We would like to finish the design of our top level &lt;font face="Courier New"&gt;AnagramsFinder&lt;/font&gt; manager class, but not fully commit to how its dependencies will be implemented yet.&amp;#160; We have a general idea of how we want our consuming class to interact with its dependencies via interfaces.&amp;#160; Let’s go ahead and look at our interfaces we named earlier:&lt;/p&gt;    &lt;pre class="brush: csharp"&gt;public interface IFileParser
{
    IEnumerable&amp;lt;string&amp;gt; ExtractWordListFromFile(string filePath);
}

public interface IAnagramGrouper
{
    IEnumerable&amp;lt;string[]&amp;gt; FindAnagramSets(IEnumerable&lt;string&gt; wordList);
}&lt;/pre&gt;

  &lt;p&gt;Now, we could create concrete implementations of the interfaces to be used in our tests that return simple dummy results.&amp;#160; An example could look like this:&lt;/p&gt;

  &lt;pre class="brush: csharp"&gt;public class TestFileParser : IFileParser
{
    public IEnumerable&amp;lt;string&amp;gt; ExtractWordListFromFile(string filePath)
    {
        return new[] { &amp;quot;wordA&amp;quot;, &amp;quot;wordB&amp;quot;, &amp;quot;Aword&amp;quot;, &amp;quot;wordC&amp;quot; };;
    }
}&lt;/pre&gt;

  &lt;p&gt;This is helpful because it keeps the one class we are interested in from being dependent on any real logic in its dependencies.&amp;#160; We want to be able to test our modules in isolation from any dependencies, so that no secondary or outside influences skew the test results.&amp;#160; We are basically providing dummy dependencies to our tests that have no logic that could cause side effects.&lt;/p&gt;

  &lt;p&gt;However, the software development community has provided tools called mocking frameworks that can create these dependency stubs for you.&amp;#160; This can save you from creating concrete interface implementations that have no use except for in testing.&amp;#160; The mocking framework I will try out is &lt;a href="http://www.ayende.com/projects/rhino-mocks.aspx" target="_blank"&gt;Rhino.Mocks from Ayende&lt;/a&gt;.&amp;#160; To get set up, you merely need to &lt;a href="http://www.ayende.com/projects/rhino-mocks/downloads.aspx" target="_blank"&gt;download the latest build zip archive&lt;/a&gt;, extract out the &lt;font face="Courier New"&gt;Rhino.Mocks.dll&lt;/font&gt; library to our &lt;font face="Courier New"&gt;Libraries&lt;/font&gt; folder created in the last blog post, and add a reference to the DLL in our Visual Studio solution.&amp;#160; I will next show you how to use Rhino.Mocks as we rewrite our first specification test.&lt;/p&gt;
  &lt;a name="firstSpec"&gt;&lt;/a&gt;

  &lt;h3&gt;Rewrite of Our First Specification/Test&lt;/h3&gt;

  &lt;p&gt;Armed with SRP, DIP, mocking, a more strict adherence to the actual problem statement, and a renewed focus on “favoring test driving logic over just testing data,” we now rewrite our first specification as follows:&lt;/p&gt;

  &lt;pre class="brush: csharp"&gt;[Subject(typeof(AnagramsFinder), &amp;quot;Finding Anagrams&amp;quot;)]
public class when_given_text_file_with_word_on_each_line
{
    static AnagramsFinder sut;
    static IEnumerable&amp;lt;string[]&amp;gt; result;
    static string filePath = &amp;quot;dummy_file_path.txt&amp;quot;;
    static IEnumerable&amp;lt;string&amp;gt; wordListFromFile = new[] { &amp;quot;wordA&amp;quot;, &amp;quot;wordB&amp;quot;, &amp;quot;Aword&amp;quot;, &amp;quot;wordC&amp;quot; };
    static IEnumerable&amp;lt;string[]&amp;gt; expected = new[] { new[] { &amp;quot;anagram&amp;quot;, &amp;quot;gramana&amp;quot; } };

    Establish context = () =&amp;gt;
    {
        var fileParser = MockRepository.GenerateStub&amp;lt;IFileParser&amp;gt;();
        fileParser.Stub(x =&amp;gt; x.ExtractWordListFromFile(filePath)).Return(wordListFromFile);

        var anagramGrouper = MockRepository.GenerateStub&amp;lt;IAnagramGrouper&amp;gt;();
        anagramGrouper.Stub(x =&amp;gt; x.FindAnagramSets(wordListFromFile)).Return(expected);

        sut = new AnagramsFinder(fileParser, anagramGrouper);
    };

    Because of = () =&amp;gt;
    {
        result = sut.ExtractAnagramSets(filePath);
    };

    It should_result_in_list_of_anagram_sets = () =&amp;gt;
    {
        result.ShouldEqual(expected);
    };
}&lt;/pre&gt;

  &lt;p&gt;I should mention a few notes about the code.&amp;#160; First, we need to add a using Rhino.Mocks statement at the top of our specification class file.&amp;#160; Second, our &lt;font face="Courier New"&gt;AnagramsFinder&lt;/font&gt; instance variable named &lt;font face="Courier New"&gt;sut&lt;/font&gt; stands for the “Subject Under Test,” a mannerism I picked up from David Tchepak, whom I’ve mentioned several times throughout this post series.&lt;/p&gt;

  &lt;p&gt;Our use of Rhino.Mocks is found when we call the &lt;font face="Courier New"&gt;MockRepository.GenerateStub&lt;/font&gt; method against an interface.&amp;#160; We then proceed to tell the stubbed object how to behave by specifying dummy return values when given methods of the object are called with given parameters.&amp;#160; The last line of our “context” setup is to then inject these two newly generated, stubbed dependencies into our manager class for testing.&amp;#160; It’s also interesting to note that none of my parameters and expected outputs really make much sense.&amp;#160; This is done on purpose to show that the data really doesn’t matter to this class, as we are not testing the logic of any data processing by the dependencies (remember, test in isolation).&amp;#160; It is true that this test really isn’t testing any real logic at this point, and it may even never evolve into testing any real logic either.&amp;#160; However, I think the important point is that it aided us in fleshing out the design, which we previously didn’t know how we were going to implement.&amp;#160; Not all tests will created equal in regard to validating our logic, but they all will play a part in driving the design of our code.&amp;#160; Hopefully these statements are true, and I would love to hear feedback on this topic in the comments.&lt;/p&gt;

  &lt;p&gt;I also want to touch on the Specification pattern of Behavior Driven Design and testing.&amp;#160; Specification tests are commonly set up into a workflow of context establishing, behavior performing, and results asserting.&amp;#160; The MSpec framework is designed to encourage this test organization.&lt;/p&gt;

  &lt;h3&gt;Red, Green, Refactor and MSpec Test Runner Output&lt;/h3&gt;

  &lt;p&gt;To make our first test run, we need to create the basic outline of our &lt;font face="Courier New"&gt;AnagramsFinder&lt;/font&gt; class as outlined in our test.&amp;#160; This includes a constructor that takes our two dependencies and an &lt;font face="Courier New"&gt;ExtractAnagramSets&lt;/font&gt; method:&lt;/p&gt;

  &lt;pre class="brush: csharp"&gt;public class AnagramsFinder
{
    private IFileParser fileParser;
    private IAnagramGrouper anagramGrouper;

    public AnagramsFinder(IFileParser fileParser, IAnagramGrouper anagramGrouper)
    {
        this.fileParser = fileParser;
        this.anagramGrouper = anagramGrouper;
    }

    public IEnumerable&amp;lt;string[]&amp;gt; ExtractAnagramSets(string filePath)
    {
        throw new NotImplementedException();
    }
}&lt;/pre&gt;

  &lt;p&gt;I am using the ConsoleRunner according to the method described in Rob Conery’s introductory MSpec and BDD post, including output to an HTML report.&amp;#160; After running our specifications, we get the following output:&lt;/p&gt;

  &lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 567px; padding-right: 5px; height: 239px; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;Specs in AnagramCodeKata:

AnagramsFinder Finding Anagrams, when given text file with word on each line
¯ should result in list of anagram sets (FAIL)
System.NotImplementedException: The method or operation is not implemented.
   ... Stack Trace here ...

Contexts: 1, Specifications: 1
  0 passed, 1 failed&lt;/pre&gt;&lt;/pre&gt;

  &lt;p&gt;…and the HTML report:&lt;/p&gt;

  &lt;p&gt;&lt;/p&gt;
  &lt;a href="http://lh6.ggpht.com/_rps657FzHZ0/SwJYInxVVSI/AAAAAAAAADU/jcI2a8kADCA/s1600-h/htmlReportError%5B3%5D.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="htmlReportError" border="0" alt="htmlReportError" src="http://lh3.ggpht.com/_rps657FzHZ0/SwJYJUz3eMI/AAAAAAAAADY/bBzyAsPXUsw/htmlReportError_thumb%5B1%5D.png?imgmax=800" width="564" height="445" /&gt;&lt;/a&gt; &lt;/a&gt;

  &lt;p&gt;A highly encouraged tenet of Test Driven Development is the practice of “Red, Green, Refactor.”&amp;#160; It is meant to denote the evolution of the results and state of your tests.&amp;#160; You are encouraged to write the test and then do the minimum work necessary to get the code base to compile and run.&amp;#160; You are first running your test in a control state where you know it should fail.&amp;#160; Most test runners will show the color Red in regard to failed tests, and thus the name of the first step.&amp;#160; Your next stage is to implement the code to make the test pass and turn the output color to Green, commonly indicating passing tests.&amp;#160; The Refactor stage is a time to pause and see if any code can be reorganized or simplified.&amp;#160; Then “rinse and repeat as necessary”, an applicable instruction from shampoo bottles.&lt;/p&gt;

  &lt;p&gt;To make our test pass, we implement real logic to coordinate the calls into our dependencies, like so:&lt;/p&gt;

  &lt;pre class="brush: csharp"&gt;public IEnumerable&amp;lt;string[]&amp;gt; ExtractAnagramSets(string filePath)
{
    var wordList = fileParser.ExtractWordListFromFile(filePath);
    return anagramGrouper.FindAnagramSets(wordList);
}&lt;/pre&gt;

  &lt;p&gt;…and the successful outputs of our test runner:&lt;/p&gt;

  &lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 579px; padding-right: 5px; height: 156px; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;Specs in AnagramCodeKata:

AnagramsFinder Finding Anagrams, when given text file with word on each line
¯ should result in list of anagram sets

Contexts: 1, Specifications: 1&lt;/pre&gt;&lt;/pre&gt;

  &lt;p&gt;&amp;#160;&lt;/p&gt;

  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_rps657FzHZ0/SwJYJpq8OfI/AAAAAAAAADc/noptZ4ChfGo/s1600-h/htmlReportSuccess%5B3%5D.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="htmlReportSuccess" border="0" alt="htmlReportSuccess" src="http://lh5.ggpht.com/_rps657FzHZ0/SwJYKGw-YWI/AAAAAAAAADg/0aPU7UnGVno/htmlReportSuccess_thumb%5B1%5D.png?imgmax=800" width="583" height="436" /&gt;&lt;/a&gt; &lt;/p&gt;

  &lt;h3&gt;Final Thoughts and Questions&lt;/h3&gt;

  &lt;p&gt;So what do you think of the new test?&amp;#160; Are we better able to see the Single Responsibility of the &lt;font face="Courier New"&gt;AnagramsFinder&lt;/font&gt; manager class?&amp;#160; Are we more inline with solving the actual problem statement?&amp;#160; Your feedback would be much appreciated.&lt;/p&gt;

  &lt;p&gt;In the next post, we will begin work on test driving the design and implementation of the dependencies…unless of course I get feedback that I should be focusing on a different area first or that this last rewrite still needs more help.&lt;/p&gt;
&lt;/span&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1664217632350055880-1713026628293781613?l=murrayon.net' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://murrayon.net/feeds/1713026628293781613/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://murrayon.net/2009/11/anagram-code-kata-part-2-mocking-and.html#comment-form" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/1713026628293781613?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/1713026628293781613?v=2" /><link rel="alternate" type="text/html" href="http://murrayon.net/2009/11/anagram-code-kata-part-2-mocking-and.html" title="Anagram Code Kata Part 2 – Mocking and SRP" /><author><name>Michael Murray</name><uri>https://profiles.google.com/109073345657761090655</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-Xf7Ccmz4FCQ/AAAAAAAAAAI/AAAAAAAAAGU/jOfxxMc1Lxo/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.ggpht.com/_rps657FzHZ0/SwJYJUz3eMI/AAAAAAAAADY/bBzyAsPXUsw/s72-c/htmlReportError_thumb%5B1%5D.png?imgmax=800" height="72" width="72" /><thr:total>2</thr:total></entry><entry gd:etag="W/&quot;CUMMQX45fyp7ImA9WxNbEEs.&quot;"><id>tag:blogger.com,1999:blog-1664217632350055880.post-8086161173571722818</id><published>2009-11-12T15:03:00.001-07:00</published><updated>2009-11-12T15:04:40.027-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-11-12T15:04:40.027-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="BDD" /><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="Testing" /><category scheme="http://www.blogger.com/atom/ns#" term="Anagram Code Kata Series" /><category scheme="http://www.blogger.com/atom/ns#" term="MSpec" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="Visual Studio" /><category scheme="http://www.blogger.com/atom/ns#" term="Microsoft" /><category scheme="http://www.blogger.com/atom/ns#" term="Code Organization" /><category scheme="http://www.blogger.com/atom/ns#" term="Algorithms" /><title>Anagram Code Kata Part 1 – Getting Started</title><content type="html">&lt;p&gt;This post is part of a &lt;a href="http://murrayon.net/2009/11/anagram-code-kata-bdd-mspec.html" target="_blank"&gt;series on coding Kata, BDD, MSpec, and SOLID principles&lt;/a&gt;.&amp;#160; Feel free to visit the above link which points to the introductory post, also containing an index of all posts in the series.&lt;/p&gt;  &lt;p&gt;This post will provide resources that serve as an introduction to the &lt;a href="http://github.com/machine/machine.specifications" target="_blank"&gt;MSpec BDD testing framework&lt;/a&gt;.&amp;#160; We will also set up our Visual Studio solution, explain the Anagram Code Kata problem statement, and write our first specification and test.&lt;/p&gt; &lt;span class="fullpost"&gt;   &lt;h3&gt;Machine.Specifications (MSpec)&lt;/h3&gt;    &lt;p&gt;The author of this BDD testing framework is &lt;a href="http://codebetter.com/blogs/aaron.jensen/default.aspx" target="_blank"&gt;Aaron Jensen&lt;/a&gt; of &lt;a href="http://codebetter.com/" target="_blank"&gt;CodeBetter.com&lt;/a&gt;.&amp;#160; Probably the best introduction to the framework (and lightly touching on general principles of BDD) is &lt;a href="http://blog.wekeroad.com/" target="_blank"&gt;Rob Conery’s&lt;/a&gt; post entitled &lt;a href="http://blog.wekeroad.com/blog/make-bdd-your-bff-2" target="_blank"&gt;Make BDD Your BFF&lt;/a&gt;.&amp;#160; MSpec is hosted on &lt;a href="http://github.com/" target="_blank"&gt;GitHub&lt;/a&gt; using the &lt;a href="http://en.wikipedia.org/wiki/Git_(software)" target="_blank"&gt;Git source control software&lt;/a&gt; (and &lt;a href="http://github.com/machine/machine.specifications" target="_blank"&gt;here is the link for the repository again&lt;/a&gt;).&amp;#160; Download the code and build it to get the necessary libraries that you need into a &lt;font face="Courier New"&gt;Bin&lt;/font&gt; folder.&amp;#160; My MSpec libraries are a few months old, as I haven’t upgraded my source since my initial download back then.&amp;#160; I have not set up TestDriven.NET and am not sure it will be necessary for this simple coding exercise.&amp;#160; I am using NUnit for the actual testing framework underneath MSpec; the following article may be of interest to you if you would like to use a newer version of NUnit than what MSpec targets:&lt;/p&gt;    &lt;p&gt;&lt;a href="http://codebetter.com/blogs/howard.dierking/archive/2009/11/08/using-latest-nunit-version-with-mspec.aspx" target="_blank"&gt;Using Latest NUnit Version with MSpec&lt;/a&gt;&lt;/p&gt;    &lt;p&gt;MSpec helps you organize your code into Specifications, a core BDD concept.&amp;#160; It is inspired by early BDD testing frameworks like Ruby’s &lt;a href="http://rspec.info/" target="_blank"&gt;RSpec&lt;/a&gt;.&amp;#160; The syntax and arrangement can be a little odd for a static language like C#, but I think I like the direction it is heading.&amp;#160; Again, the best way to get familiar with the syntax, set up the MSpec Test Runner, and configure HTML reporting output is to read &lt;a href="http://blog.wekeroad.com/blog/make-bdd-your-bff-2" target="_blank"&gt;Rob’s introductory post on BDD and MSpec&lt;/a&gt;.&lt;/p&gt;    &lt;h3&gt;Setting Up the Visual Studio Solution&lt;/h3&gt;    &lt;p&gt;Let’s begin by creating a C# console application named &lt;font face="Courier New"&gt;AnagramCodeKata&lt;/font&gt; targeting .NET Framework 3.5.&amp;#160; The solution name (and solution folder) will be the same.&amp;#160; For our dependencies on MSpec (and NUnit), I have created a physical file system folder named &lt;font face="Courier New"&gt;Libraries&lt;/font&gt; and made it a sibling folder to the project’s physical folder (same level as the solution file).&amp;#160; That folder contains the following DLL libraries from the &lt;font face="Courier New"&gt;Bin&lt;/font&gt; folder of the compiled MSpec source code:&lt;/p&gt;    &lt;ul&gt;     &lt;li&gt;&lt;font face="Courier New"&gt;CommandLine.dll&lt;/font&gt; &lt;/li&gt;      &lt;li&gt;&lt;font face="Courier New"&gt;Machine.Specifications.ConsoleRunner.exe&lt;/font&gt; &lt;/li&gt;      &lt;li&gt;&lt;font face="Courier New"&gt;Machine.Specifications.dll&lt;/font&gt; &lt;/li&gt;      &lt;li&gt;&lt;font face="Courier New"&gt;Machine.Specifications.NUnit.dll&lt;/font&gt; &lt;/li&gt;      &lt;li&gt;&lt;font face="Courier New"&gt;Machine.Specifications.Reporting.dll&lt;/font&gt; &lt;/li&gt;      &lt;li&gt;&lt;font face="Courier New"&gt;nunit.framework.dll&lt;/font&gt; &lt;/li&gt;   &lt;/ul&gt;    &lt;p&gt;Then within our Solution Explorer, we only need to make references to 3 of these libraries (namely &lt;font face="Courier New"&gt;Machine.Specifications.dll&lt;/font&gt;, &lt;font face="Courier New"&gt;Machine.Specifications.NUnit.dll&lt;/font&gt;, and &lt;font face="Courier New"&gt;nunit.framework.dll&lt;/font&gt;).&amp;#160; I have also created two folders within our project in Solution Explorer, naming them &lt;font face="Courier New"&gt;Reports&lt;/font&gt; and &lt;font face="Courier New"&gt;Specifications&lt;/font&gt;.&amp;#160; The &lt;font face="Courier New"&gt;Reports&lt;/font&gt; folder will contain the HTML-generated report of running our tests every time.&amp;#160; The &lt;font face="Courier New"&gt;Specifications&lt;/font&gt; folder will hold our specifications and tests.&amp;#160; This is what our current Solution Explorer looks like now:&lt;/p&gt;    &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_rps657FzHZ0/SvyGOtiALTI/AAAAAAAAADM/Xeygzia8cho/s1600-h/anagramScreenshot%5B6%5D.png"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="anagramScreenshot" border="0" alt="anagramScreenshot" src="http://lh4.ggpht.com/_rps657FzHZ0/SvyGO870IZI/AAAAAAAAADQ/lDW6F6bIuhI/anagramScreenshot_thumb%5B4%5D.png?imgmax=800" width="289" height="340" /&gt;&lt;/a&gt; &lt;/p&gt;    &lt;p&gt;Also notice the &lt;font face="Courier New"&gt;MSpecRunner &lt;/font&gt;button at the top, which I’ve created to execute a custom External Tool that points to &lt;font face="Courier New"&gt;Machine.Specifications.ConsoleRunner.exe&lt;/font&gt; and passes in the appropriate parameters.&amp;#160; This helpful setup was put forth by Rob Conery in the link to his introductory blog post that I provided above.&lt;/p&gt;    &lt;h3&gt;Anagram Problem Statement&lt;/h3&gt;    &lt;p&gt;So let’s go ahead and look at the &lt;a href="http://codekata.pragprog.com/2007/01/kata_six_anagra.html"&gt;Anagram problem&lt;/a&gt; we are trying to solve.&amp;#160; Here’s a quick summary of what we are trying to accomplish:&lt;/p&gt;    &lt;blockquote&gt;     &lt;p&gt;“The challenge is fairly simple: given a file containing one word per line, print out all the combinations of words that are anagrams; each line in the output contains all the words from the input that are anagrams of each other.”&lt;/p&gt;   &lt;/blockquote&gt;    &lt;p&gt;Unfortunately, Dave Thomas’ &lt;font face="Courier New"&gt;wordlist.txt&lt;/font&gt; link is broken on his blog post, and when responding to commenters asking for a fixed link to the text file, he pointed us all to some website that aggregates links to several word lists from around the Web.&amp;#160; Many of us readers wanted to know the exact word list file he used so that we could compare our results to his.&amp;#160; In his post, he states that there should be 2,530 sets of anagrams and 5,680 total words participating in those anagram sets.&amp;#160; The tough part about this is that we’ve got to have the same word list text file.&amp;#160; I searched the web and found a few, but I only found one that might be the same list.&amp;#160; It is from some guy’s personal GitHub repository:&lt;/p&gt;    &lt;p&gt;&lt;a title="http://github.com/krist0ff/code_kata/tree/ba975c3a64f11a81db2f3716b40de046f1ca7ef4/kata6" href="http://github.com/krist0ff/code_kata/tree/ba975c3a64f11a81db2f3716b40de046f1ca7ef4/kata6"&gt;http://github.com/krist0ff/code_kata/tree/ba975c3a64f11a81db2f3716b40de046f1ca7ef4/kata6&lt;/a&gt;&lt;/p&gt;    &lt;p&gt;If you’re visiting this blog post, months or years after it was originally written, I obviously can’t guarantee this file will still be out there.&amp;#160; You can try emailing me or commenting here and I can hopefully send it to you via email if I still have it.&amp;#160; But more importantly, as we progress with this series, we’ll find out if it’s even the right word list or not.&lt;/p&gt;    &lt;p&gt;Lastly, I’d like to look at the objectives for the coding kata that Dave Thomas outlines:&lt;/p&gt;    &lt;blockquote&gt;     &lt;p&gt;“Apart from having some fun with words, this kata should make you think somewhat about algorithms. The simplest algorithms to find all the anagram combinations may take inordinate amounts of time to do the job. Working though alternatives should help bring the time down by orders of magnitude. To give you a possible point of comparison, I hacked a solution together in 25 lines of Ruby. It runs on the word list from my web site in 1.5s on a 1GHz PPC. It’s also an interesting exercise in testing: can you write unit tests to verify that your code is working correctly before setting it to work on the full dictionary.”&lt;/p&gt;   &lt;/blockquote&gt;    &lt;p&gt;I don’t think we’ll focus on runtime as much, but we’ll certainly revisit our algorithm as necessary if we have unacceptable wait times for the code to finish.&amp;#160; To complete this section, let’s go ahead and add the &lt;font face="Courier New"&gt;wordlist.txt&lt;/font&gt; to our solution at the root of of our Project node.&lt;/p&gt;    &lt;h3&gt;How About Some Actual Code?!&lt;/h3&gt;    &lt;p&gt;So let’s now start off by creating our first specification.&amp;#160; I have created a new C# file named &lt;font face="Courier New"&gt;AnagramsFinderSpecs.cs&lt;/font&gt; under our &lt;font face="Courier New"&gt;Specifications&lt;/font&gt; folder in our solution.&amp;#160; I attempted to write our first test, but already feel like I’m not inline with “&lt;a href="http://www.davesquared.net/2009/11/favour-test-driving-logic-over-data.html"&gt;favoring test driving logic over just testing data&lt;/a&gt;.”&lt;/p&gt;    &lt;pre class="brush: csharp"&gt;using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Machine.Specifications;

namespace AnagramCodeKata.Specifications
{
    [Subject(typeof(AnagramsFinder), &amp;quot;Finding Anagrams&amp;quot;)]
    public class when_given_text_file_with_word_on_each_line
    {
        static int result;
        static AnagramsFinder sut;

        Establish context = () =&amp;gt;
        {
            sut = new AnagramsFinder();
        };

        Because of = () =&amp;gt;
        {
            result = sut.ParseTextFile(&amp;quot;wordslist.txt&amp;quot;);
        };

        It should_result_in_count_of_2530_anagram_sets = () =&amp;gt;
        {
            result.ShouldEqual(2530);
        };
    }
}&lt;/pre&gt;

  &lt;p&gt;If you can’t make out the funky structure and syntax, again refer back to Rob Conery’s introductory post to all of this stuff that I link to at the top.&amp;#160; He does a great job of explaining what’s going on here.&lt;/p&gt;

  &lt;p&gt;Also, please remember that I have no clue what I’m doing and I’m just trying to copy people who know what they are doing.&amp;#160; I am hoping that I’ll be corrected by those who have a better idea of how to approach all of this.&amp;#160; This is a good stopping point because of my desire for feedback and the feeling of uncertainty I’m already getting.&lt;/p&gt;

  &lt;p&gt;Seriously, please guide me in the right direction or validate where I’m headed, because even I’m not sure where that is yet.&amp;#160; I will say this, I’m pretty sure I shouldn’t be reading in &lt;font face="Courier New"&gt;wordslist.txt&lt;/font&gt; yet, and that this might be where I need to start using a mocking framework.&lt;/p&gt;

  &lt;p&gt;Stay tuned, there’s more to come.&amp;#160; I just can’t tell what it will be about yet.&amp;#160; It’ll still be fun though, I promise.&lt;/p&gt;
&lt;/span&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1664217632350055880-8086161173571722818?l=murrayon.net' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://murrayon.net/feeds/8086161173571722818/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://murrayon.net/2009/11/anagram-code-kata-part-1-getting.html#comment-form" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/8086161173571722818?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/8086161173571722818?v=2" /><link rel="alternate" type="text/html" href="http://murrayon.net/2009/11/anagram-code-kata-part-1-getting.html" title="Anagram Code Kata Part 1 – Getting Started" /><author><name>Michael Murray</name><uri>https://profiles.google.com/109073345657761090655</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-Xf7Ccmz4FCQ/AAAAAAAAAAI/AAAAAAAAAGU/jOfxxMc1Lxo/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/_rps657FzHZ0/SvyGO870IZI/AAAAAAAAADQ/lDW6F6bIuhI/s72-c/anagramScreenshot_thumb%5B4%5D.png?imgmax=800" height="72" width="72" /><thr:total>2</thr:total></entry><entry gd:etag="W/&quot;DE4MSXs6cSp7ImA9WxNUGUo.&quot;"><id>tag:blogger.com,1999:blog-1664217632350055880.post-2610711337538360854</id><published>2009-11-11T15:03:00.001-07:00</published><updated>2009-11-11T15:03:08.519-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-11-11T15:03:08.519-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="MSI" /><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="Build" /><category scheme="http://www.blogger.com/atom/ns#" term="Windows" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="Prerequisites" /><category scheme="http://www.blogger.com/atom/ns#" term="Microsoft" /><category scheme="http://www.blogger.com/atom/ns#" term="Deployment and Installers" /><category scheme="http://www.blogger.com/atom/ns#" term="Bootstrapper" /><category scheme="http://www.blogger.com/atom/ns#" term="MSBuild" /><category scheme="http://www.blogger.com/atom/ns#" term="Visual Studio" /><category scheme="http://www.blogger.com/atom/ns#" term="C/C++" /><category scheme="http://www.blogger.com/atom/ns#" term="Registry" /><category scheme="http://www.blogger.com/atom/ns#" term="Launch Condition" /><title>Custom Setup Bootstrappers</title><content type="html">&lt;p&gt;At work, we have a line of desktop products for one of clients installed on the user’s Windows machine via MSI installers.&amp;#160; We use Visual Studio Setup Projects to create the MSI installers.&amp;#160; Visual Studio also provides the ability to create setup bootstrappers to make sure the user has all the prerequisites needed for the MSI install technology and any dependencies required by your desktop product.&amp;#160; These bootstrappers are unmanaged code, because you can’t always depend on the .NET framework already being there (at least not until everyone is off of XP and lower).&amp;#160; I have developed a solution that has worked for the last year or more, but has felt clumsy and slapped together.&amp;#160; Recently I found the &lt;a href="http://dotnetinstaller.codeplex.com/"&gt;open source project called dotNetInstaller&lt;/a&gt; which has better provided me with the control and flexibility I need for custom logic during bootstrap and install time.&amp;#160; I’ll first introduce you to my “hack” solution.&lt;/p&gt; &lt;span class="fullpost"&gt;   &lt;p&gt;It has been difficult for me to find ways to tie into the bootstrapper logic workflow to add in custom logic during installs, and Visual Studio is very rigid in its flexibility and basic in its configuration options in this regard.&amp;#160; Until recently, I had settled on writing managed, C# code for this custom logic meant to run before or after the MSI installer, and then creating another unmanaged &lt;font face="Courier New"&gt;setup.exe&lt;/font&gt; bootstrapper to ensure the .NET framework was installed just so that executable could run.&amp;#160; So the user would run this new unmanaged &lt;font face="Courier New"&gt;setup.exe&lt;/font&gt; bootstrapper (making sure .NET framework was installed), which would then call my managed custom logic executable, which would then call the MSI installer’s unmanaged, Visual Studio-generated &lt;font face="Courier New"&gt;setup.exe&lt;/font&gt; bootstrapper (ensuring more prerequisites were installed), which would then finally install the MSI, and which would then lastly return to my managed custom logic executable for any post-install, clean up logic.&amp;#160; I describe this solution in full (and how to generate the same bootstrappers that Visual Studio creates using MSBuild outside the IDE) in a StackOverflow.com answer that I submitted:&lt;/p&gt;    &lt;p&gt;&lt;a title="http://stackoverflow.com/questions/1106768/how-can-i-reverse-engineer-an-installer-that-was-written-with-ghost-installer/1121846#1121846" href="http://stackoverflow.com/questions/1106768/how-can-i-reverse-engineer-an-installer-that-was-written-with-ghost-installer/1121846#1121846"&gt;http://stackoverflow.com/questions/1106768/how-can-i-reverse-engineer-an-installer-that-was-written-with-ghost-installer/1121846#1121846&lt;/a&gt;&lt;/p&gt;    &lt;p&gt;Well, recently I found the &lt;a href="http://dotnetinstaller.codeplex.com/"&gt;dotNetInstaller project on CodePlex.com&lt;/a&gt; and have found it to be the control and flexibility I need for custom logic when bootstrapping installers.&amp;#160; Not only does it give you great control over prerequisite install order, it also gives you the ability to run code after MSI install and to also filter on CPU architecture types, OS versions, and other registry or file system conditions.&amp;#160; I have currently setup branching scenarios for the prerequisites based on if the user has a 32-bit (x86) or 64-bit (x64) OS.&amp;#160; This utility will even let you brand the bootstrapper with a banner and icon, which is a change of pace from the plain and unlabeled bootstrapper that Visual Studio and MSBuild produce.&lt;/p&gt;    &lt;p&gt;If you have custom bootstrapping needs, I have to highly recommend you give dotNetInstaller a look.&amp;#160; Hopefully it will prove as flexible and powerful for your needs as it has for mine.&lt;/p&gt; &lt;/span&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1664217632350055880-2610711337538360854?l=murrayon.net' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://murrayon.net/feeds/2610711337538360854/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://murrayon.net/2009/11/custom-setup-bootstrappers.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/2610711337538360854?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/2610711337538360854?v=2" /><link rel="alternate" type="text/html" href="http://murrayon.net/2009/11/custom-setup-bootstrappers.html" title="Custom Setup Bootstrappers" /><author><name>Michael Murray</name><uri>https://profiles.google.com/109073345657761090655</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-Xf7Ccmz4FCQ/AAAAAAAAAAI/AAAAAAAAAGU/jOfxxMc1Lxo/s512-c/photo.jpg" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;DkQGQH4yfCp7ImA9Wx5XGUg.&quot;"><id>tag:blogger.com,1999:blog-1664217632350055880.post-5468325837601038177</id><published>2009-11-10T21:32:00.004-07:00</published><updated>2010-09-19T21:45:21.094-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-09-19T21:45:21.094-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="New Technology" /><category scheme="http://www.blogger.com/atom/ns#" term="BDD" /><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="Testing" /><category scheme="http://www.blogger.com/atom/ns#" term="Anagram Code Kata Series" /><category scheme="http://www.blogger.com/atom/ns#" term="MSpec" /><category scheme="http://www.blogger.com/atom/ns#" term="Code Organization" /><category scheme="http://www.blogger.com/atom/ns#" term="SOLID" /><title>Anagram Code Kata – BDD &amp; MSpec</title><content type="html">&lt;p&gt;Many forms of martial arts utilize the &lt;a href="http://en.wikipedia.org/wiki/Kata" target="_blank"&gt;kata&lt;/a&gt; as a means for detailed, focused practice and training.&amp;#160; The power of this practice comes from repetition and focus on simple fundamentals, “while attempting to maintain perfect form.”&amp;#160; The principle goal is to “try out different combinations of techniques in a safe, practice environment to ultimately find out how to defeat your opponent”.&amp;#160; In the last few years, this concept has taken hold within the software development world in the form of &lt;a href="http://en.wikipedia.org/wiki/Code_Kata" target="_blank"&gt;Code Kata&lt;/a&gt;, a phrase coined by &lt;a href="http://pragprog.com/" target="_blank"&gt;Pragmatic Programmer&lt;/a&gt; &lt;a href="http://pragdave.pragprog.com/" target="_blank"&gt;Dave Thomas&lt;/a&gt; (and &lt;a href="http://codekata.pragprog.com/" target="_blank"&gt;here is his blog dedicated to the Code Kata&lt;/a&gt;).&lt;/p&gt;  &lt;p&gt;My inspiration for all of this is &lt;a href="http://www.davesquared.net/2009/10/calculators-and-tale-of-two-tdds-pt-1.html" target="_blank"&gt;David Tchepak’s Calculator Kata series of posts&lt;/a&gt;.&amp;#160; Hopefully he will stop by and provide some helpful guidance and advice.&amp;#160; I have selected the &lt;a href="http://codekata.pragprog.com/2007/01/kata_six_anagra.html" target="_blank"&gt;Anagram Kata (code kata number 6)&lt;/a&gt; for me to practice.&amp;#160; My goals for this series of posts are:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;To practice coding and share my learning experience with you, while also soliciting feedback and suggestions. &lt;/li&gt;    &lt;li&gt;To more fully understand the concepts driving &lt;a href="http://en.wikipedia.org/wiki/Behavior_Driven_Development" target="_blank"&gt;Behavior Driven Development (BDD)&lt;/a&gt; via a testing framework designed for such a practice, namely &lt;a href="http://blog.wekeroad.com/blog/make-bdd-your-bff-2/" target="_blank"&gt;MSpec&lt;/a&gt;. &lt;/li&gt;    &lt;li&gt;To keep to the &lt;a href="http://www.davesquared.net/2009/01/introduction-to-solid-principles-of-oo.html" target="_blank"&gt;SOLID principles of Object-Oriented Design&lt;/a&gt;, especially &lt;a href="http://www.davesquared.net/2009/11/favour-test-driving-logic-over-data.html" target="_blank"&gt;in favor of test driving logic over just testing data&lt;/a&gt;. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Below I will keep an updated list of blog posts in this series.&amp;#160; Hope you enjoy it!&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://murrayon.net/2009/11/anagram-code-kata-part-1-getting.html"&gt;Part 1 – Getting Started&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://murrayon.net/2009/11/anagram-code-kata-part-2-mocking-and.html" target="_blank"&gt;Part 2 – Mocking and SRP (Single Responsibility Principle)&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://murrayon.net/2009/12/anagram-code-kata-part-3-use-common.html" target="_blank"&gt;Part 3 – Use Common Sense&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://murrayon.net/2010/01/anagram-code-kata-part-4-will-it-write.html"&gt;Part 4 – Will it Write My Code for Me?&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://murrayon.net/2010/02/anagram-code-kata-part-5-domain-objects.html"&gt;Part 5 – Domain Objects Over Language Primitives&lt;/a&gt;&amp;#160;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;UPDATE (07/18/10)&lt;/u&gt;&lt;/strong&gt;:&amp;#160; I was asked where the source code to this post series could be found.&amp;#160; I decided to host it at BitBucket.org using Mercurial for the source control:&amp;#160; &lt;a href="http://bitbucket.org/murrayondotnet/anagramkata/"&gt;http://bitbucket.org/murrayondotnet/anagramkata/&lt;/a&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1664217632350055880-5468325837601038177?l=murrayon.net' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://murrayon.net/feeds/5468325837601038177/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://murrayon.net/2009/11/anagram-code-kata-bdd-mspec.html#comment-form" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/5468325837601038177?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/5468325837601038177?v=2" /><link rel="alternate" type="text/html" href="http://murrayon.net/2009/11/anagram-code-kata-bdd-mspec.html" title="Anagram Code Kata – BDD &amp;amp; MSpec" /><author><name>Michael Murray</name><uri>https://profiles.google.com/109073345657761090655</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-Xf7Ccmz4FCQ/AAAAAAAAAAI/AAAAAAAAAGU/jOfxxMc1Lxo/s512-c/photo.jpg" /></author><thr:total>1</thr:total></entry><entry gd:etag="W/&quot;D04DR3c7fCp7ImA9WxNVFkQ.&quot;"><id>tag:blogger.com,1999:blog-1664217632350055880.post-1145610350465942405</id><published>2009-10-23T19:54:00.006-06:00</published><updated>2009-10-27T20:12:56.904-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-27T20:12:56.904-06:00</app:edited><title>Trying Out SyntaxHighlighter</title><content type="html">&lt;p&gt;This is just a test post to see if I'm smart enough to add the javascript &lt;a href="http://alexgorbatchev.com/wiki/SyntaxHighlighter:Demo"&gt;SyntaxHighlighter&lt;/a&gt; to my blog. ;)&lt;/p&gt;

&lt;pre class="brush: csharp"&gt;
public int ScoreDice(int[] selectedDice)
{
    int score = 0;

    foreach (IScoringCombo scoreCombo in ScoringCombos)
    {
        IEnumerable&amp;lt;int&amp;gt; satisfyingIndices = scoreCombo.SatisfiedBy(selectedDice);

        if (satisfyingIndices.Count() &amp;gt; 0)
        {
            score += scoreCombo.ScoreDice(selectedDice);

            // Remove the dice at satisfyingIndices from selectedDice collection so not double-scored
            selectedDice = selectedDice
                .Select((value, index) =&amp;gt; index)
                .Except(satisfyingIndices)
                .ToArray();
        }
    }

    return score;
}
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1664217632350055880-1145610350465942405?l=murrayon.net' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://murrayon.net/feeds/1145610350465942405/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://murrayon.net/2009/10/trying-out-syntaxhighlighter.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/1145610350465942405?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/1145610350465942405?v=2" /><link rel="alternate" type="text/html" href="http://murrayon.net/2009/10/trying-out-syntaxhighlighter.html" title="Trying Out SyntaxHighlighter" /><author><name>Michael Murray</name><uri>https://profiles.google.com/109073345657761090655</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-Xf7Ccmz4FCQ/AAAAAAAAAAI/AAAAAAAAAGU/jOfxxMc1Lxo/s512-c/photo.jpg" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;DU4FRH09eCp7ImA9WxNXEks.&quot;"><id>tag:blogger.com,1999:blog-1664217632350055880.post-3190857442496647610</id><published>2009-09-29T17:45:00.001-06:00</published><updated>2009-09-29T17:45:15.360-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-09-29T17:45:15.360-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Web" /><category scheme="http://www.blogger.com/atom/ns#" term="New Technology" /><category scheme="http://www.blogger.com/atom/ns#" term="MSI" /><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="Deployment and Installers" /><category scheme="http://www.blogger.com/atom/ns#" term="IIS" /><category scheme="http://www.blogger.com/atom/ns#" term="Windows" /><category scheme="http://www.blogger.com/atom/ns#" term="Visual Studio" /><category scheme="http://www.blogger.com/atom/ns#" term="PowerShell" /><category scheme="http://www.blogger.com/atom/ns#" term="Microsoft" /><title>Web Deployment Tool</title><content type="html">&lt;p&gt;It appears that the IIS team at Microsoft has officially completed a 1.0 version of the deployment utility I mentioned at the end of &lt;a title="Visual Studio Web Deployment Projects" href="http://murrayon.net/2008/02/visual-studio-web-deployment-projects.html"&gt;my previous post about Visual Studio Web Deployment Projects&lt;/a&gt; almost two years ago.&amp;#160; It includes a command-line tool named MSDeploy that helps with deploying and syncing web projects across all of the web servers in your server farm.&amp;#160; The installation also includes all kinds of packaging and deployment features that seem really handy.&amp;#160; You can even do migrations from IIS 6 to IIS 7 or package your apps to be distributed via the &lt;a href="http://www.microsoft.com/web/downloads/platform.aspx"&gt;Microsoft Web Platform Installer&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;I thought that maybe the tool creates MSI installers, but it appears the package may actually just be a ZIP file that you can import and export through the IIS Manager or various other command-line tools or PowerShell Cmdlets.&lt;/p&gt;  &lt;p&gt;Here is the page where you can read more about the toolset and download the installer:&amp;#160; &lt;a title="http://www.iis.net/extensions/WebDeploymentTool" href="http://www.iis.net/extensions/WebDeploymentTool"&gt;http://www.iis.net/extensions/WebDeploymentTool&lt;/a&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1664217632350055880-3190857442496647610?l=murrayon.net' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://murrayon.net/feeds/3190857442496647610/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://murrayon.net/2009/09/web-deployment-tool.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/3190857442496647610?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/3190857442496647610?v=2" /><link rel="alternate" type="text/html" href="http://murrayon.net/2009/09/web-deployment-tool.html" title="Web Deployment Tool" /><author><name>Michael Murray</name><uri>https://profiles.google.com/109073345657761090655</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-Xf7Ccmz4FCQ/AAAAAAAAAAI/AAAAAAAAAGU/jOfxxMc1Lxo/s512-c/photo.jpg" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;DkQGSH85fip7ImA9WxNVGEg.&quot;"><id>tag:blogger.com,1999:blog-1664217632350055880.post-7109317170222139904</id><published>2009-09-23T16:09:00.000-06:00</published><updated>2009-10-29T16:12:09.126-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-29T16:12:09.126-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Error" /><category scheme="http://www.blogger.com/atom/ns#" term="Java" /><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="C/C++" /><category scheme="http://www.blogger.com/atom/ns#" term="Microsoft" /><title>Variable Declaration Restrictions</title><content type="html">&lt;p&gt;I came across a surprising restriction on variable declarations that I thought I would share.&lt;/p&gt;  &lt;p&gt;My own investigation into C# was prompted by &lt;a title="Declaring variables inside a switch statement - Stack Overflow" href="http://stackoverflow.com/questions/1231198/declaring-variables-inside-a-switch-statement/1231209#1231209"&gt;an answer given to a Stack Overflow question regarding an odd Objective-C compiler error&lt;/a&gt;.&amp;#160; The answer seemed to allude to a language specification somewhere that doesn’t allow variable declarations in one-liner embedded statements of control structures (loops, &lt;font face="Courier New"&gt;switch&lt;/font&gt; statements, &lt;font face="Courier New"&gt;if-else&lt;/font&gt; statements, etc.).&amp;#160; Not only that, Objective-C wasn’t unique in this restriction; it appears Java and even C itself have had this restriction for quite a while as well.&lt;/p&gt;  &lt;p&gt;Naturally this got my curiosity going and I decided to see if C# had also carried over such a restriction.&amp;#160; The answer is yes; it appears that Java, C#, and Objective-C have all kept this language restriction from their common ancestor C.&amp;#160; Let’s get right to what it looks like:&lt;/p&gt; &lt;span class='fullpost'&gt; &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_rps657FzHZ0/SrqdKFwyBLI/AAAAAAAAADA/8fajE6RTF0o/s1600-h/code_screenshot%5B6%5D.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="code_screenshot" border="0" alt="code_screenshot" src="http://lh3.ggpht.com/_rps657FzHZ0/SrqdKaU74uI/AAAAAAAAADE/HGin3AOsrsM/code_screenshot_thumb%5B4%5D.png?imgmax=800" width="416" height="712" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The red-underlined errors read “&lt;font face="Courier New"&gt;embedded statement cannot be a declaration or labeled statement&lt;/font&gt;” and the green-underlined warnings read “&lt;font face="Courier New"&gt;the variable ‘i’ is assigned but its value is never used&lt;/font&gt;.”&amp;#160; You’ve probably seen the warning quite a few times before, but the compiler error really caught me by surprise.&amp;#160; I mean it makes sense that declaring a variable in the one-line body of a control structure is pointless, as it would immediately go out of scope (and usefulness) after declared.&amp;#160; But I thought it odd for the compiler to actually deny such a program statement, as if it put the compiler in an invalid state or maybe it was somehow dangerous.&lt;/p&gt;  &lt;p&gt;One thing I do want to point out that appears to be different from the other languages (or at the very least Objective-C) is that the variable declaration is allowed in the &lt;font face="Courier New"&gt;switch&lt;/font&gt; statement, as illustrated in the image above.&lt;/p&gt;  &lt;p&gt;To figure out why this isn’t allowed, I decided to actually look into the &lt;a href="http://msdn.microsoft.com/en-us/vcsharp/aa336809.aspx"&gt;C# 3.0 Language Specification&lt;/a&gt; to see if there were any comments or explanations regarding this issue.&amp;#160; I ended up finding the C# language grammar in Appendix B.&amp;#160; I was able to parse through the grammar and find this restriction in the grammar itself.&amp;#160; Take a look:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;B.2.5 &lt;/strong&gt;&lt;a name="_Toc174237894"&gt;&lt;strong&gt;Statements&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;  &lt;div&gt;&lt;em&gt;statement: &lt;/em&gt;&lt;/div&gt;  &lt;div style="margin-left: 40px"&gt;&lt;em&gt;labeled-statement      &lt;br /&gt;declaration-statement       &lt;br /&gt;embedded-statement&lt;/em&gt;&lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div&gt;&lt;em&gt;embedded-statement: &lt;/em&gt;&lt;/div&gt;  &lt;div style="margin-left: 40px"&gt;&lt;em&gt;block      &lt;br /&gt;empty-statement       &lt;br /&gt;expression-statement       &lt;br /&gt;selection-statement       &lt;br /&gt;iteration-statement       &lt;br /&gt;jump-statement       &lt;br /&gt;try-statement       &lt;br /&gt;checked-statement       &lt;br /&gt;unchecked-statement       &lt;br /&gt;lock-statement       &lt;br /&gt;using-statement       &lt;br /&gt;yield-statement&lt;/em&gt;&lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div&gt;&lt;em&gt;block: &lt;/em&gt;&lt;/div&gt;  &lt;div style="margin-left: 40px"&gt;{&lt;em&gt; statement-list&lt;sub&gt;optional&lt;/sub&gt;&lt;/em&gt; }&lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div&gt;&lt;em&gt;statement-list: &lt;/em&gt;&lt;/div&gt;  &lt;div style="margin-left: 40px"&gt;&lt;em&gt;statement      &lt;br /&gt;statement-list statement&lt;/em&gt;&lt;/div&gt;  &lt;p&gt;...&lt;/p&gt;  &lt;div&gt;&lt;em&gt;selection-statement: &lt;/em&gt;&lt;/div&gt;  &lt;div style="margin-left: 40px"&gt;&lt;em&gt;if-statement      &lt;br /&gt;switch-statement&lt;/em&gt;&lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div&gt;&lt;em&gt;if-statement: &lt;/em&gt;&lt;/div&gt;  &lt;div style="margin-left: 40px"&gt;if ( &lt;em&gt;boolean-expression&lt;/em&gt; ) &lt;em&gt;embedded-statement&lt;/em&gt;     &lt;br /&gt;if ( &lt;em&gt;boolean-expression&lt;/em&gt; ) &lt;em&gt;embedded-statement&lt;/em&gt; else &lt;em&gt;embedded-statement&lt;/em&gt;&lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div&gt;&lt;em&gt;switch-statement: &lt;/em&gt;&lt;/div&gt;  &lt;div style="margin-left: 40px"&gt;switch ( &lt;em&gt;expression&lt;/em&gt; ) &lt;em&gt;switch-block&lt;/em&gt;&lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div&gt;&lt;em&gt;switch-block: &lt;/em&gt;&lt;/div&gt;  &lt;div style="margin-left: 40px"&gt;{ &lt;em&gt;switch-sections&lt;sub&gt;optional&lt;/sub&gt;&lt;/em&gt; }&lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div&gt;&lt;em&gt;switch-sections: &lt;/em&gt;&lt;/div&gt;  &lt;div style="margin-left: 40px"&gt;&lt;em&gt;switch-section      &lt;br /&gt;switch-sections switch-section&lt;/em&gt;&lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div&gt;&lt;em&gt;switch-section: &lt;/em&gt;&lt;/div&gt;  &lt;div style="margin-left: 40px"&gt;&lt;em&gt;switch-labels statement-list&lt;/em&gt;&lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div&gt;&lt;em&gt;switch-labels: &lt;/em&gt;&lt;/div&gt;  &lt;div style="margin-left: 40px"&gt;&lt;em&gt;switch-label      &lt;br /&gt;switch-labels switch-label&lt;/em&gt;&lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div&gt;&lt;em&gt;switch-label: &lt;/em&gt;&lt;/div&gt;  &lt;div style="margin-left: 40px"&gt;case &lt;em&gt;constant-expression&lt;/em&gt; :     &lt;br /&gt;default :&lt;/div&gt;  &lt;p&gt;...&lt;/p&gt;  &lt;p&gt;You’ll notice that there is the concept of a &lt;font face="Courier New"&gt;statement&lt;/font&gt; and an &lt;font face="Courier New"&gt;embedded-statement&lt;/font&gt;; also note that &lt;font face="Courier New"&gt;statement&lt;/font&gt; is a superset of &lt;font face="Courier New"&gt;embedded-statement&lt;/font&gt;, adding two other types of statements: &lt;font face="Courier New"&gt;labeled-statement&lt;/font&gt; and &lt;font face="Courier New"&gt;declaration-statement&lt;/font&gt;.&amp;#160; If a particular grammar rule requires an &lt;font face="Courier New"&gt;embedded-statement&lt;/font&gt;, then those two latter types of statements are not allowed in that context.&amp;#160; If we wanted to allow those two statement types, our grammar rule should specify the generic &lt;font face="Courier New"&gt;statement&lt;/font&gt; category instead.&lt;/p&gt;  &lt;p&gt;Also noteworthy is that an &lt;font face="Courier New"&gt;embedded-statement&lt;/font&gt; can resolve to a &lt;font face="Courier New"&gt;block&lt;/font&gt;, which includes braces around a &lt;font face="Courier New"&gt;statement-list&lt;/font&gt;.&amp;#160; A &lt;font face="Courier New"&gt;statement-list&lt;/font&gt; is one or more &lt;font face="Courier New"&gt;statement&lt;/font&gt; entities, which would now allow us to use the two statement types excluded from &lt;font face="Courier New"&gt;embedded-statement&lt;/font&gt;.&lt;/p&gt;  &lt;p&gt;Something interesting that is not included in the grammar snippet above, the following statement constructs (and language keywords) require the &lt;font face="Courier New"&gt;block&lt;/font&gt; statement construct, bypassing the option of a grammar resolution to &lt;font face="Courier New"&gt;embedded-statement&lt;/font&gt; directly and allowing you to leave off surrounding braces: &lt;font face="Courier New"&gt;try&lt;/font&gt;, &lt;font face="Courier New"&gt;catch&lt;/font&gt;, &lt;font face="Courier New"&gt;finally&lt;/font&gt;, &lt;font face="Courier New"&gt;checked&lt;/font&gt;, and &lt;font face="Courier New"&gt;unchecked&lt;/font&gt;.&lt;/p&gt;  &lt;p&gt;Almost all of the other statement constructs allow you the option of a one-liner &lt;font face="Courier New"&gt;embedded-statement&lt;/font&gt; (or which can be resolved into a &lt;font face="Courier New"&gt;block&lt;/font&gt;), including &lt;font face="Courier New"&gt;if&lt;/font&gt;, &lt;font face="Courier New"&gt;if-else&lt;/font&gt;, &lt;font face="Courier New"&gt;while&lt;/font&gt;, &lt;font face="Courier New"&gt;do-while&lt;/font&gt;, &lt;font face="Courier New"&gt;for&lt;/font&gt;, and &lt;font face="Courier New"&gt;foreach&lt;/font&gt;.&amp;#160; The one surprising exception is the &lt;font face="Courier New"&gt;switch&lt;/font&gt; construct, which allows for a &lt;font face="Courier New"&gt;statement-list&lt;/font&gt; after each &lt;font face="Courier New"&gt;switch-label&lt;/font&gt;.&amp;#160; As a side note, this allows you to put useless braces (via &lt;font face="Courier New"&gt;block&lt;/font&gt;) around your &lt;font face="Courier New"&gt;statement-list&lt;/font&gt; (or anywhere inside it) after any of your &lt;font face="Courier New"&gt;switch-label&lt;/font&gt; statements if you wanted.&amp;#160; But back to the point, these facts explain the existence of (and in one case, lack of) compiler errors regarding embedded statements in the code screenshot near the top.&lt;/p&gt;  &lt;p&gt;Now you will know exactly what the compiler means when it gives you the error that an “&lt;font face="Courier New"&gt;embedded statement cannot be a declaration or labeled statement.&lt;/font&gt;”&lt;/p&gt; &lt;/span&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1664217632350055880-7109317170222139904?l=murrayon.net' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://murrayon.net/feeds/7109317170222139904/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://murrayon.net/2009/09/variable-declaration-restrictions.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/7109317170222139904?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/7109317170222139904?v=2" /><link rel="alternate" type="text/html" href="http://murrayon.net/2009/09/variable-declaration-restrictions.html" title="Variable Declaration Restrictions" /><author><name>Michael Murray</name><uri>https://profiles.google.com/109073345657761090655</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-Xf7Ccmz4FCQ/AAAAAAAAAAI/AAAAAAAAAGU/jOfxxMc1Lxo/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.ggpht.com/_rps657FzHZ0/SrqdKaU74uI/AAAAAAAAADE/HGin3AOsrsM/s72-c/code_screenshot_thumb%5B4%5D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;DkUDRXY7fip7ImA9WxNVGEg.&quot;"><id>tag:blogger.com,1999:blog-1664217632350055880.post-1297116529623161852</id><published>2009-05-21T16:29:00.001-06:00</published><updated>2009-10-29T16:11:14.806-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-29T16:11:14.806-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Threading" /><category scheme="http://www.blogger.com/atom/ns#" term="Delegates" /><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="Windows Forms" /><category scheme="http://www.blogger.com/atom/ns#" term="UI" /><category scheme="http://www.blogger.com/atom/ns#" term="Microsoft" /><title>BeginInvoke Methods and OneWay Attribute</title><content type="html">&lt;p&gt;In a previous post I talked about &lt;a href="http://murrayon.net/2008/01/windows-forms-ui-threading.html"&gt;Windows Forms UI Threading&lt;/a&gt; using delegates.&amp;#160; Recently I needed to replicate what I had learned, and I happened to do so without reviewing my notes in the former blog post.&amp;#160; Apparently I didn’t remember everything I had learned and became confused over thread invocation techniques, introducing a bug into my thread messaging.&amp;#160; The UI thread was blocking while the background worker thread was processing.&lt;/p&gt;  &lt;p&gt;The cause was related to figuring out which &lt;font face="Courier New"&gt;BeginInvoke&lt;/font&gt; method to call when kicking off the worker thread, the &lt;font face="Courier New"&gt;BeginInvoke&lt;/font&gt; of the &lt;font face="Courier New"&gt;Form&lt;/font&gt; object or the &lt;font face="Courier New"&gt;BeginInvoke&lt;/font&gt; of the delegate.&amp;#160; The answer is the &lt;font face="Courier New"&gt;BeginInvoke&lt;/font&gt; method of the delegate.&amp;#160; All of the examples linked to in the previous post had it right, but I got it mixed up with making &lt;font face="Courier New"&gt;Invoke&lt;/font&gt; or &lt;font face="Courier New"&gt;BeginInvoke&lt;/font&gt; calls from the background thread using the &lt;font face="Courier New"&gt;Form&lt;/font&gt; object’s version of these methods.&amp;#160; If you don’t care whether your messaging from the background thread is synchronous or asynchronous, then you can use either methods and you can use the ones from the &lt;font face="Courier New"&gt;Form&lt;/font&gt; object.&lt;/p&gt;  &lt;p&gt;The problem is using these &lt;font face="Courier New"&gt;Form&lt;/font&gt; object methods in order to kick off your background worker thread.&amp;#160; The description of the &lt;font face="Courier New"&gt;BeginInvoke&lt;/font&gt; method on the &lt;font face="Courier New"&gt;Form&lt;/font&gt; object states that it &lt;font face="Courier New"&gt;“executes the specified delegate asynchronously on the thread that the control’s underlying handle was created on.”&lt;/font&gt;&amp;#160; I translate this to mean that calling your &lt;font face="Courier New"&gt;Form&lt;/font&gt; object’s &lt;font face="Courier New"&gt;BeginInvoke&lt;/font&gt; will process on your UI thread.&amp;#160; Apparently the delegate &lt;font face="Courier New"&gt;BeginInvoke&lt;/font&gt; method has no such wording in its description.&lt;/p&gt; &lt;span class='fullpost'&gt; &lt;p&gt;Rereading &lt;a title="Safe, Simple Multithreading in Windows Forms, Part 1" href="http://msdn.microsoft.com/en-us/library/ms951089.aspx"&gt;this MSDN article&lt;/a&gt; from the former post and reading the &lt;font face="Courier New"&gt;Form&lt;/font&gt; object’s &lt;font face="Courier New"&gt;BeginInvoke&lt;/font&gt; method description which I quoted above set me straight and got rid of my blocking UI problem (again).&lt;/p&gt;  &lt;p&gt;During this research session, I came across another informative resource when trying to figure out what was meant by potential resource leaks when using &lt;font face="Courier New"&gt;BeginInvoke&lt;/font&gt; with out later calling &lt;font face="Courier New"&gt;EndInvoke&lt;/font&gt;.&amp;#160; From reading the article, I gather you can substitute the &lt;font face="Courier New"&gt;EndInvoke&lt;/font&gt; call with the &lt;a title="OneWayAttribute Class" href="http://msdn.microsoft.com/en-us/library/system.runtime.remoting.messaging.onewayattribute.aspx"&gt;&lt;font face="Courier New"&gt;OneWay&lt;/font&gt; attribute&lt;/a&gt;&lt;font face="Courier New"&gt;&lt;font color="#333333"&gt;&lt;font face="Verdana"&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt; on the method being invoked, as long as it has a &lt;font face="Courier New"&gt;void&lt;/font&gt; return signature and no &lt;font face="Courier New"&gt;ref&lt;/font&gt; nor &lt;font face="Courier New"&gt;out&lt;/font&gt; parameters.&amp;#160; Please let me know if my interpretation is wrong.&amp;#160; I added the attribute to my code in the applicable spots and have seen no side effects of doing so.&amp;#160; Here is the article on read mentioning this attribute:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/rosherove/pages/DefensiveEventPublishing.aspx"&gt;Defensive Event Publishing in .NET – Part I&lt;/a&gt;&lt;/p&gt; &lt;/span&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1664217632350055880-1297116529623161852?l=murrayon.net' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://murrayon.net/feeds/1297116529623161852/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://murrayon.net/2009/05/begininvoke-methods-and-oneway.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/1297116529623161852?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/1297116529623161852?v=2" /><link rel="alternate" type="text/html" href="http://murrayon.net/2009/05/begininvoke-methods-and-oneway.html" title="BeginInvoke Methods and OneWay Attribute" /><author><name>Michael Murray</name><uri>https://profiles.google.com/109073345657761090655</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-Xf7Ccmz4FCQ/AAAAAAAAAAI/AAAAAAAAAGU/jOfxxMc1Lxo/s512-c/photo.jpg" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;CUAMSHg_eyp7ImA9WxNVGEg.&quot;"><id>tag:blogger.com,1999:blog-1664217632350055880.post-5809824401959368426</id><published>2009-03-24T14:08:00.001-06:00</published><updated>2009-10-29T16:03:09.643-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-29T16:03:09.643-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="New Technology" /><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="Windows" /><category scheme="http://www.blogger.com/atom/ns#" term="PowerShell" /><category scheme="http://www.blogger.com/atom/ns#" term="Microsoft" /><title>PowerShell 1.0 for Windows XP</title><content type="html">&lt;p&gt;I just noticed in today's Tuesday round of Microsoft Updates that there is an optional software download for PowerShell 1.0 on Windows XP.&amp;#160; PowerShell has been one of those things that I really want to learn, but just haven't devoted the time.&amp;#160; I desperately would like to replace my use of the Command Prompt with Windows PowerShell.&lt;/p&gt;  &lt;p&gt;PowerShell really is more than just your regular &lt;a href="http://en.wikipedia.org/wiki/Command_line_interpreter"&gt;Command Line Interpreter&lt;/a&gt; that you have been used to, especially compared to the &lt;a href="http://en.wikipedia.org/wiki/Command_Prompt_(Windows)"&gt;Windows Command Prompt&lt;/a&gt;.&amp;#160; You get the power of the command line shells that you may have tried out in Linux environments, but it really does go beyond those as well.&amp;#160; PowerShell is (from Wikipedia) &lt;font face="Courier New"&gt;&amp;quot;Microsoft's new extensible command line shell and task-based scripting technology.&amp;quot;&lt;/font&gt;&amp;#160; Here is a quote from an MSDN document that better explains the advantages PowerShell brings:&lt;/p&gt; &lt;span class='fullpost'&gt; &lt;blockquote&gt;   &lt;p&gt;&amp;quot;Unlike most shells, which accept and return text, Windows PowerShell is built on top of the .NET common language runtime (CLR) and the .NET Framework, and accepts and returns .NET objects. This fundamental change in the environment brings entirely new tools and methods to the management and configuration of Windows.&lt;/p&gt;   &lt;p&gt;&amp;quot;Windows PowerShell introduces the concept of a cmdlet (pronounced &amp;quot;command-let&amp;quot;), a simple, single-function command-line tool built into the shell. You can use each cmdlet separately, but their power is realized when you use these simple tools in combination to perform complex tasks. Windows PowerShell includes more than one hundred basic core cmdlets, and you can write your own cmdlets and share them with other users.&lt;/p&gt;   &lt;p&gt;&amp;quot;Like many shells, Windows PowerShell gives you access to the file system on the computer. In addition, Windows PowerShell providers enable you to access other data stores, such as the registry and the digital signature certificate stores, as easily as you access the file system.&amp;quot;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;If you want to know more about PowerShell, I would suggest the following readings:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://en.wikipedia.org/wiki/Windows_PowerShell"&gt;Wikipedia - Windows PowerShell&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/aa973757(VS.85).aspx"&gt;MSDN - Windows PowerShell Getting Started Guide&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Also, &lt;a href="http://codebetter.com/members/james.kovacs.aspx"&gt;James Kovacs&lt;/a&gt; from &lt;a href="http://codebetter.com/"&gt;CodeBetter.com&lt;/a&gt; has been doing &lt;a href="http://codebetter.com/blogs/james.kovacs/archive/tags/PowerShell/default.aspx"&gt;a series of posts on the basics of PowerShell&lt;/a&gt; that is a great light introduction to how a developer would find the tool most useful.&amp;#160; Here are two of the starting posts from that series:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://codebetter.com/blogs/james.kovacs/archive/2009/02/01/getting-started-with-powershell-developer-edition.aspx"&gt;Getting Started with PowerShell - Developer Edition&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://codebetter.com/blogs/james.kovacs/archive/2009/02/07/powershell-processes-and-piping.aspx"&gt;PowerShell, Processes, and Piping&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/span&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1664217632350055880-5809824401959368426?l=murrayon.net' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://murrayon.net/feeds/5809824401959368426/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://murrayon.net/2009/03/powershell-10-for-windows-xp.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/5809824401959368426?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/5809824401959368426?v=2" /><link rel="alternate" type="text/html" href="http://murrayon.net/2009/03/powershell-10-for-windows-xp.html" title="PowerShell 1.0 for Windows XP" /><author><name>Michael Murray</name><uri>https://profiles.google.com/109073345657761090655</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-Xf7Ccmz4FCQ/AAAAAAAAAAI/AAAAAAAAAGU/jOfxxMc1Lxo/s512-c/photo.jpg" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;CUAEQn49cCp7ImA9WxNVGEg.&quot;"><id>tag:blogger.com,1999:blog-1664217632350055880.post-2958618679460091764</id><published>2009-03-02T17:51:00.000-07:00</published><updated>2009-10-29T16:01:43.068-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-29T16:01:43.068-06:00</app:edited><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="Microsoft" /><category scheme="http://www.blogger.com/atom/ns#" term="LINQ" /><title>The "Yield" Keyword in C# (AKA Execution Control Hot Potato)</title><content type="html">&lt;p&gt;I actually read about this topic a while back when I saw a C# code sample containing an unfamiliar keyword -- &lt;font face="Courier New"&gt;yield&lt;/font&gt;.&amp;#160; Well just today I saw another code sample using this keyword again, and had since forgotten everything I had learned.&amp;#160; As I Googled the topic again, I happened across the same article I read back then, a terrific blog post by &lt;a href="http://flimflan.com/blog/"&gt;Joshua Flanagan&lt;/a&gt;.&amp;#160; Here is a link to Joshua's simple introduction to the &lt;font face="Courier New"&gt;yield&lt;/font&gt; C# keyword:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://flimflan.com/blog/ThePowerOfYieldReturn.aspx"&gt;The power of yield (return)&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;I would also suggest the following MSDN article as well:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx"&gt;yield (C# Reference)&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;I'll give you the gist of this .NET 2.0 feature.&amp;#160; If you have a method that returns an iterative collection and the consumers of your function will only need &lt;a title="foreach, in (C# Reference)" href="http://msdn.microsoft.com/en-us/library/ttw7t8t6.aspx"&gt;&lt;font face="Courier New"&gt;foreach&lt;/font&gt;&lt;/a&gt;, read-only access to that collection, then it is good practice to have your method return the universal &lt;font face="Courier New"&gt;IEnumerable&lt;/font&gt; collection (as opposed to you arbitrarily selecting an implementation-specific collection to be forced upon the consumers of your code).&amp;#160; In C#, when you are returning an &lt;font face="Courier New"&gt;IEnumerable&lt;/font&gt; collection from a method, you are allowed to use the &lt;font face="Courier New"&gt;yield&lt;/font&gt; keyword in order to transform your method into what Microsoft calls an &amp;quot;iterator block&amp;quot;.&amp;#160; Here is a quote from the MSDN article I mentioned above to explain this phrase and the &lt;font face="Courier New"&gt;yield&lt;/font&gt; keyword:&lt;/p&gt; &lt;span class='fullpost'&gt; &lt;blockquote&gt;   &lt;p&gt;&amp;quot;The &lt;strong&gt;yield&lt;/strong&gt; keyword signals to the compiler that the method in which it appears is an iterator block.&amp;#160; The compiler generates a class to implement the behavior that is expressed in the iterator block.&amp;#160; In the iterator block, the &lt;strong&gt;yield&lt;/strong&gt; keyword is used together with the &lt;strong&gt;return&lt;/strong&gt; keyword to provide a value to the enumerator object.&amp;#160; This is the value that is returned, for example, in each loop of a &lt;strong&gt;foreach&lt;/strong&gt; statement.&amp;#160; The &lt;strong&gt;yield&lt;/strong&gt; keyword is also used with &lt;strong&gt;break&lt;/strong&gt; to signal the end of iteration.&amp;quot;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;You are essentially building an enumerator (or iterator), and with each iteration of your &lt;font face="Courier New"&gt;foreach&lt;/font&gt; loop in the calling, consuming code, you are executing your method containing the iterator block until it reaches a &lt;font face="Courier New"&gt;yield return&lt;/font&gt; statement.&amp;#160; At this point, only one item at a time in the collection is returned back to the consuming &lt;font face="Courier New"&gt;foreach&lt;/font&gt; loop for immediate logic execution by the body of the loop.&amp;#160; On the next iteration of the consuming &lt;font face="Courier New"&gt;foreach&lt;/font&gt; loop, the method containing the iterator block will be called again and another single item in the collection will be returned for similar logic execution.&lt;/p&gt;  &lt;p&gt;This game of execution control &lt;a href="http://en.wikipedia.org/wiki/Hot_Potato_(game)"&gt;Hot Potato&lt;/a&gt; has its benefits, in that you can stop the flip-flop of execution control when the music has stopped.&amp;#160; In this analogy, I am comparing the &amp;quot;music stopping&amp;quot; to your code satisfying some logic that makes the rest of your &lt;font face="Courier New"&gt;foreach&lt;/font&gt; loop iterations unnecessary (perhaps you have found the one item in the collection you were looking for by meeting the sufficient requirements of an &lt;font face="Courier New"&gt;if&lt;/font&gt; statement).&amp;#160; The benefit is that when you &lt;font face="Courier New"&gt;&lt;a title="break (C# Reference)" href="http://msdn.microsoft.com/en-us/library/adbctzc4.aspx"&gt;break&lt;/a&gt;&lt;/font&gt; out of the consuming &lt;font face="Courier New"&gt;foreach&lt;/font&gt; loop before completing the iterations, you prevent wasteful code execution and memory use.&amp;#160; This feature prevents one piece of code from fully populating a potentially large collection of items, just to have another consuming piece of code iterate partially through the list and then prematurely exit.&amp;#160; Consider the savings especially when adding each item to the collection may be computationally intensive or if each item in the collection is itself a very large data structure.&amp;#160; In the Hot Potato analogy, this would be as if the kids in the circle who didn't touch the &amp;quot;hot potato&amp;quot; never existed in the game, saving floor space and unnecessary tosses to people you don't really want to target for humiliating removal from the game.&lt;/p&gt;  &lt;p&gt;For good sample code snippets of using the &lt;font face="Courier New"&gt;yield&lt;/font&gt; keyword, see the links provided above.&lt;/p&gt;  &lt;p&gt;I hope to identify situations in the near future where I could benefit from using this C# keyword.&amp;#160; Hopefully then I won't forget a year down the road from now and then have to re-search the internet for Joshua's blog post and the MSDN article to remind myself what I already learned.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;FOLLOWUP (03/26/09)&lt;/u&gt;&lt;/strong&gt;:&amp;#160; So I'm not sure how I missed it, but it took reading a very recent article by &lt;a href="http://www.4guysfromrolla.com/ScottMitchell.shtml"&gt;Scott Mitchell&lt;/a&gt; of &lt;a href="http://www.4guysfromrolla.com/"&gt;4GuysFromRolla.com&lt;/a&gt; to understand the real reason the &lt;font face="Courier New"&gt;yield&lt;/font&gt; keyword was introduced.&amp;#160; The introduction of this keyword in .NET 2.0 was most helpful in the case of fulfilling the requirements of implementing the &lt;font face="Courier New"&gt;IEnumerable&lt;/font&gt; interface, which typically required you to create a sub-class that implemented &lt;font face="Courier New"&gt;IEnumerator&lt;/font&gt;.&amp;#160; In many cases, it allowed you to take about 50 lines of code and turn it into 2 lines of code using the &lt;font face="Courier New"&gt;yield&lt;/font&gt; keyword.&amp;#160; The introduction of LINQ brought heavy reliance on enumerating using the &lt;font face="Courier New"&gt;yield&lt;/font&gt; keyword in order to &lt;a href="http://en.wikipedia.org/wiki/Pipeline_(software)"&gt;pipeline&lt;/a&gt; &lt;a href="http://en.wikipedia.org/wiki/Language_Integrated_Query#Standard_Query_Operators"&gt;query operators&lt;/a&gt; together.&amp;#160; Scott Mitchell takes you through this whole journey, tying all of the loose ends together to create a complete history of this keyword and how it's most commonly utilized.&amp;#160; Please visit the following article to get the whole picture:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://aspnet.4guysfromrolla.com/articles/032509-1.aspx"&gt;An Extensive Examination of LINQ: The Ins and Outs of Query Operators&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/span&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1664217632350055880-2958618679460091764?l=murrayon.net' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://murrayon.net/feeds/2958618679460091764/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://murrayon.net/2009/03/keyword-in-c-aka-execution-control-hot.html#comment-form" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/2958618679460091764?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/2958618679460091764?v=2" /><link rel="alternate" type="text/html" href="http://murrayon.net/2009/03/keyword-in-c-aka-execution-control-hot.html" title="The &amp;quot;Yield&amp;quot; Keyword in C# (AKA Execution Control Hot Potato)" /><author><name>Michael Murray</name><uri>https://profiles.google.com/109073345657761090655</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-Xf7Ccmz4FCQ/AAAAAAAAAAI/AAAAAAAAAGU/jOfxxMc1Lxo/s512-c/photo.jpg" /></author><thr:total>1</thr:total></entry><entry gd:etag="W/&quot;D0YDQ30-cSp7ImA9WxVWEEs.&quot;"><id>tag:blogger.com,1999:blog-1664217632350055880.post-3770422546436181972</id><published>2009-02-19T10:36:00.000-07:00</published><updated>2009-02-19T10:39:32.359-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-02-19T10:39:32.359-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Error" /><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET" /><category scheme="http://www.blogger.com/atom/ns#" term="Database" /><category scheme="http://www.blogger.com/atom/ns#" term="SQL" /><category scheme="http://www.blogger.com/atom/ns#" term="Microsoft" /><title>SQL Server Express 2005 Remote Connections</title><content type="html">&lt;p&gt;I developed an ASP.NET site connecting to a local SQL Server Express 2005 instance on my development box.&amp;#160; To further my informal testing, I wanted to host the site under IIS 7 on a Windows Server 2008 box, but I wanted to just use the same database remotely.&amp;#160; I got the following error message when the site tried to hit the database:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&amp;quot;An error has occurred while establishing a connection to the server.&amp;#160; When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections.&amp;#160; (provider: Named Pipes Provider, error: 40 - Could not open connection to SQL Server)&amp;quot;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Apparently SQL Server Express 2005 is not automatically configured for remote access during installation (which really isn't surprising as it is the Express version).&amp;#160; Remote connections can be enabled though with a few relatively painless steps.&amp;#160; See the following link for the procedure:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://www.datamasker.com/SSE2005_NetworkCfg.htm"&gt;Configuring SQL Server Express 2005 for Remote Access when SQL Server does not allow remote connections&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Hope this is helpful.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1664217632350055880-3770422546436181972?l=murrayon.net' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://murrayon.net/feeds/3770422546436181972/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://murrayon.net/2009/02/sql-server-express-2005-remote.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/3770422546436181972?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/3770422546436181972?v=2" /><link rel="alternate" type="text/html" href="http://murrayon.net/2009/02/sql-server-express-2005-remote.html" title="SQL Server Express 2005 Remote Connections" /><author><name>Michael Murray</name><uri>https://profiles.google.com/109073345657761090655</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-Xf7Ccmz4FCQ/AAAAAAAAAAI/AAAAAAAAAGU/jOfxxMc1Lxo/s512-c/photo.jpg" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;CUYAQ305eyp7ImA9WxNVGEg.&quot;"><id>tag:blogger.com,1999:blog-1664217632350055880.post-2050413284625674348</id><published>2009-02-17T17:38:00.000-07:00</published><updated>2009-10-29T15:52:22.323-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-29T15:52:22.323-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Error" /><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="Server 2008" /><category scheme="http://www.blogger.com/atom/ns#" term="ISAPI" /><category scheme="http://www.blogger.com/atom/ns#" term="IIS" /><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET Providers Series" /><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET" /><category scheme="http://www.blogger.com/atom/ns#" term="Windows" /><category scheme="http://www.blogger.com/atom/ns#" term="Vista" /><category scheme="http://www.blogger.com/atom/ns#" term="Microsoft" /><title>IIS 7 and ASP.NET Providers (Membership, Roles, and Profile)</title><content type="html">&lt;p&gt;I talked at length about the ASP.NET &lt;a href="http://en.wikipedia.org/wiki/Provider_model"&gt;Provider Model pattern&lt;/a&gt; and the ease of implementing several built-in services you can have for free with ASP.NET 2.0 in &lt;a title="ASP.NET Providers - Membership, Role, and Profile" href="http://murrayon.net/2009/01/aspnet-providers-membership-role-and.html"&gt;my previous series of posts&lt;/a&gt;.&amp;#160; In this post, I will be discussing the advantages of using the ASP.NET Provider system under IIS 7 found in Windows Vista and Server 2008.&amp;#160; I will also talk about an issue with using this Provider system to try and add authentication to an ASP.NET website that contains non-ASP.NET content, like static HTML pages.&amp;#160; I think I'll start with the discussion about non-ASP.NET content first.&lt;/p&gt; &lt;span class='fullpost'&gt; &lt;h3&gt;IIS 7.0 Integrated Pipeline&lt;/h3&gt;  &lt;p&gt;Since I don't really understand much about &lt;a title="Internet Server Application Programming Interface" href="http://en.wikipedia.org/wiki/ISAPI"&gt;ISAPI&lt;/a&gt; filters and such, I'm just going to quote stuff from random pages on the &lt;a href="http://www.iis.net/"&gt;official Microsoft IIS site&lt;/a&gt;:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&amp;quot;IIS 6.0 and previous versions allowed the development of .NET application components via the ASP.NET platform. ASP.NET integrated with IIS via an ISAPI extension, and exposed its own application and request processing model. This effectively exposed two separate server pipelines, one for native ISAPI filters and extension components, and another for managed application components. ASP.NET components would execute entirely inside the ASP.NET ISAPI extension bubble and only for requests mapped to ASP.NET in the IIS script map configuration.&lt;/p&gt;    &lt;p&gt;&amp;quot;IIS 7.0 integrates the ASP.NET runtime with the core web server, providing a unified request processing pipeline that is exposed to both native and managed components known as modules.&amp;quot;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;and...&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&amp;quot;This allows developers to fully extend the IIS 7.0 server with the richness of ASP.NET 2.0 and the .NET Framework, instead of using the lower level IIS C++ APIs. Existing ASP.NET applications also immediately benefit from tighter integration using existing ASP.NET features like Forms Authentication, Roles, and Output Caching for all content.&amp;quot;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Note the last three words there:&amp;#160; &amp;quot;&lt;font face="Courier New"&gt;for all content.&lt;/font&gt;&amp;quot;&amp;#160; When I started this endeavor to implement the ASP.NET Provider system, I didn't realize that it would really only apply to ASP.NET content and pages.&amp;#160; I didn't realize on IIS 6 I would have to fiddle with ISAPI filters and content filters to make my implementation work.&amp;#160; And when I tried to doing a mapping (like &lt;font face="Courier New"&gt;*.html&lt;/font&gt; to the ASP.NET ISAPI DLL), I either didn't do it right or it just isn't very reliable and stable; here is a link to &lt;a title="IIS problems with forms authentication &amp;amp; HTML pages" href="http://forums.asp.net/t/1184547.aspx"&gt;the thread I followed in trying to make the appropriate changes in IIS 6&lt;/a&gt;.&amp;#160; I will note however that &lt;a href="http://www.ultidev.com/products/Cassini/index.htm"&gt;UltiDev's Cassini Web Server&lt;/a&gt; can host such a site with absolutely no extra modification, where even IIS 7 required a little configuration.&lt;/p&gt;  &lt;p&gt;By default in IIS 7, an &lt;a title="IIS and ASP.NET: The Application Pool" href="http://www.developer.com/net/asp/article.php/2245511"&gt;application pool&lt;/a&gt; runs using the &lt;font face="Courier ne"&gt;Integrated&lt;/font&gt; mode for the &lt;font face="Courier New"&gt;Managed Pipeline&lt;/font&gt; configuration.&amp;#160; But there is also the option of &lt;font face="Courier New"&gt;Classic&lt;/font&gt; mode, which is helpful if you are needing strict compatibility with how you ran a website under IIS 6.&amp;#160; In many cases, however, an existing ASP.NET website runs under &lt;font face="Courier New"&gt;Integrated&lt;/font&gt; mode without any problems.&lt;/p&gt;  &lt;h3&gt;Configuring an ASP.NET Website to Run on IIS 7&lt;/h3&gt;  &lt;p&gt;First, I think I will point you to a great video and document on Microsoft's official IIS site that explains everything you really need to know.&amp;#160; Then I will emphasize a few of the important points and also a gotcha or two.&amp;#160; Here are the links you should reference:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://learn.iis.net/page.aspx/377/using-aspnet-forms-authentication/"&gt;Using ASP.NET Forms Authentication&lt;/a&gt; (video) &lt;/li&gt;    &lt;li&gt;&lt;a href="http://learn.iis.net/page.aspx/243/aspnet-integration-with-iis7/"&gt;ASP.NET Integration with IIS 7.0&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The video above is also a terrific introduction to IIS 7.&amp;#160; Below is a screenshot of IIS's UI for configuring a website:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_rps657FzHZ0/SZtZDNleinI/AAAAAAAAACw/b2kq_6XIGD8/s1600-h/IIS_screenshot%5B5%5D.png"&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="IIS_screenshot" src="http://lh5.ggpht.com/_rps657FzHZ0/SZtZDjlrIRI/AAAAAAAAAC0/cC29GwiAIhk/IIS_screenshot_thumb%5B3%5D.png?imgmax=800" width="475" height="368" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Notice the &lt;font face="Courier New"&gt;ASP.NET&lt;/font&gt; section and how it's able to use your Providers to bring back Users, Roles, and Profile data back from the database.&amp;#160; This UI reads from and writes to your &lt;font face="Courier New"&gt;Web.config&lt;/font&gt; and is really slick in my opinion.&lt;/p&gt;  &lt;p&gt;The only real &amp;quot;breaking change&amp;quot; for running your website under IIS 7 is the fact that your &lt;font face="Courier New"&gt;Web.config&lt;/font&gt; might need a little migration to be compatible with &lt;font face="Courier New"&gt;Integrated&lt;/font&gt; mode.&amp;#160; Apparently IIS gives you helpful error messages if it detects you haven't run through the migration yet.&amp;#160; The two most common things that ASP.NET websites have that require this migration are if you have declared &lt;font face="Courier New"&gt;httpModules&lt;/font&gt; or &lt;font face="Courier New"&gt;httpHandlers&lt;/font&gt; in your &lt;font face="Courier New"&gt;Web.config&lt;/font&gt;.&amp;#160; The fix is to either copy or move these sections under &lt;font face="Courier New"&gt;system.web&lt;/font&gt; to their corresponding &lt;font face="Courier New"&gt;system.webServer&lt;/font&gt; sections.&amp;#160; If you duplicate the sections, then you should be able run under both &lt;font face="Courier New"&gt;Integrated&lt;/font&gt; and &lt;font face="Courier New"&gt;Classic&lt;/font&gt; modes without any problems.&amp;#160; You can even still run on IIS 6 I believe, as IIS 6 will ignore this new section called &lt;font face="Courier New"&gt;system.webServer&lt;/font&gt; which is IIS 7 specific.&amp;#160; IIS 7 even provides a utility that can perform this migration for you; here is the what you would type in a Command Prompt:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;%windir%\system32\inetsrv\APPCMD.EXE migrate config &amp;lt;Application Path&amp;gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;...where &lt;font face="Courier New"&gt;&amp;lt;Application Path&amp;gt;&lt;/font&gt; would be something like &lt;font face="Courier New"&gt;&amp;quot;Default Web Site/&amp;quot;&lt;/font&gt; or &lt;font face="Courier New"&gt;&amp;quot;Default Web Site/someApp&amp;quot;&lt;/font&gt;.&amp;#160; If you manually migrate your &lt;font face="Courier New"&gt;Web.config&lt;/font&gt; and are still getting migration error messages, here is a snippet you can add right under the &lt;font face="Courier New"&gt;system.webServer&lt;/font&gt; node to disable the error message:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;&amp;lt;validation validateIntegratedModeConfiguration=&amp;quot;false&amp;quot; /&amp;gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Also, I want to make note of something I couldn't get to work that they showed in the tutorial video.&amp;#160; At about the &lt;font face="Courier New"&gt;13:50&lt;/font&gt; mark in the video, they go into the &lt;font face="Courier New"&gt;Modules&lt;/font&gt; section of the UI.&amp;#160; These are the modules of the IIS pipeline for both managed .NET code and unmanaged code.&amp;#160; In order to make their video file hidden behind ASP.NET Forms Authentication, they change a property on two authentication modules to allow these modules to execute on non-managed content as well.&amp;#160; However, this did not work for me for some reason when I was trying to secure HTML pages behind Forms Authentication.&amp;#160; What did work for me (and may have been a drastic solution) was to put the following attribute on the &lt;font face="Courier New"&gt;modules&lt;/font&gt; node under &lt;font face="Courier New"&gt;system.webServer&lt;/font&gt;:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;&amp;lt;modules runAllManagedModulesForAllRequests=&amp;quot;true&amp;quot; /&amp;gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;I believe the issue in my case was their was another few managed modules that I needed to clear that checkbox for in the UI.&amp;#160; But I didn't take the time to find out which ones I was missing and opted for the drastic solution.&amp;#160; I will also say there is a way to configure these managed modules through your &lt;font face="Courier New"&gt;Web.config&lt;/font&gt; so you don't have to have your IT Admin configure properties manually in the UI.&amp;#160; In fact, it seems that much of IIS 7 configuration for your website can be done this way.&lt;/p&gt;  &lt;h3&gt;A Few Error Messages (and Their Solutions)&lt;/h3&gt;  &lt;p&gt;If you get error messages while using the IIS Manager user interface like this...&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&amp;quot;This feature cannot be used because the default provider type could not be determined to check whether it is a trusted provider.&lt;/p&gt;    &lt;p&gt;&amp;quot;You can use this feature only when the default provider is a trusted provider.&amp;#160; If you are a server administrator, you can make a provider a trusted provider by adding the provider type to the trusted providers list in the Administration.config file.&amp;#160; The provider has to be strongly typed and added to the GAC (Global Assembly Cache).&amp;quot;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;...then you should visit the following article for detailed information about resolving the error message:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://www.iis.net/ConfigReference/system.webServer/management/trustedProviders/add"&gt;Adding Management Trusted Providers &amp;lt;add&amp;gt;&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Getting rid of this error message is really only for convenience in letting the IIS Manager UI be able to query your database using your providers and give you a basic view of your Membership, Roles, and Profile fields.&amp;#160; Also, if you are trying to find your &lt;font face="Courier New"&gt;Administration.config&lt;/font&gt; file, it is likely located under the &lt;font face="Courier New"&gt;C:\Windows\System32\inetsrv\config\&lt;/font&gt; folder.&lt;/p&gt;  &lt;p&gt;The other error message I received was not in the IIS Manager UI, but rather when you attempt to view your website in a browser (after you think you got all the configuration bugs out!).&amp;#160; A &lt;font face="Courier New"&gt;BadImageFormatException&lt;/font&gt; is thrown and the error details say something like this:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;Could not load file or assembly 'AssemlyNameHere' or one of its dependencies.&amp;#160; An attempt was made to load a program with an incorrect format.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The likely cause of this error is that the assembly referenced in the error message was compiled only for 32-bit machines and you are running a 64-bit operating system.&amp;#160; With Windows Server 2008 x64, the application pools in IIS have an &lt;font face="Courier New"&gt;Advanced Setting&lt;/font&gt; called &lt;font face="Courier New"&gt;Enable 32-Bit Applications&lt;/font&gt;, that is disabled by default.&amp;#160; Enable this setting and you're probably good to go.&amp;#160; See the following link for more detailed information and some nice screenshots:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://blog.crowe.co.nz/archive/2007/11/05/Could-not-load-file-or-assembly-name-or-one-of.aspx"&gt;Could not load file or assembly 'name' or one of its dependencies&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;Additional Reading&lt;/h3&gt;  &lt;p&gt;I just barely noticed that &lt;a href="http://www.4guysfromrolla.com/"&gt;4GuysFromRolla.com&lt;/a&gt; recently posted an article about the exact same stuff I was attempting to explain.&amp;#160; To read additional details about IIS 7's Integrated Pipeline and ASP.NET Forms Authentication with static content, check out the following link:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://aspnet.4guysfromrolla.com/articles/122408-1.aspx"&gt;Apply ASP.NET Authentication and Authorization Rules to Static Content with IIS 7.0's Integrated Pipeline Feature&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;Conclusion&lt;/h3&gt;  &lt;p&gt;I hope you've enjoyed &lt;a title="ASP.NET Providers - Membership, Role, and Profile" href="http://murrayon.net/2009/01/aspnet-providers-membership-role-and.html"&gt;this series on the ASP.NET Provider System&lt;/a&gt;.&amp;#160; My desire is that you've found these posts helpful in getting you started with implementing such a versatile and customizable model for adding common membership and authorization functionality to your new and existing websites.&amp;#160; There are quite a few advantages in hosting your site on IIS 7 in Windows Server 2008, as I've outlined in this specific post.&amp;#160; Hopefully these discussions about errors and gotchas will also help you get going in no time!&lt;/p&gt; &lt;/span&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1664217632350055880-2050413284625674348?l=murrayon.net' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://murrayon.net/feeds/2050413284625674348/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://murrayon.net/2009/02/iis-7-and-aspnet-providers-membership.html#comment-form" title="3 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/2050413284625674348?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1664217632350055880/posts/default/2050413284625674348?v=2" /><link rel="alternate" type="text/html" href="http://murrayon.net/2009/02/iis-7-and-aspnet-providers-membership.html" title="IIS 7 and ASP.NET Providers (Membership, Roles, and Profile)" /><author><name>Michael Murray</name><uri>https://profiles.google.com/109073345657761090655</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-Xf7Ccmz4FCQ/AAAAAAAAAAI/AAAAAAAAAGU/jOfxxMc1Lxo/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh5.ggpht.com/_rps657FzHZ0/SZtZDjlrIRI/AAAAAAAAAC0/cC29GwiAIhk/s72-c/IIS_screenshot_thumb%5B3%5D.png?imgmax=800" height="72" width="72" /><thr:total>3</thr:total></entry></feed>

