<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Elegant Code &#187; Design Patterns</title>
	<atom:link href="http://elegantcode.com/tag/design-patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Where to start a new program</title>
		<link>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=where-to-start-a-new-program</link>
		<comments>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/#comments</comments>
		<pubDate>Sun, 08 Jun 2008 19:50:39 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/06/08/where-to-start-a-new-program/</guid>
		<description><![CDATA[Here as an interesting question that popped up at my user group meeting last night, when you are starting a new program (green field development), where do you get started?&#160; I find these questions interesting because there really is no correct answer, but a persons answer will tell you something about how they problem solve. [...]]]></description>
			<content:encoded><![CDATA[<p>Here as an interesting question that popped up at my user group meeting last night, when you are starting a new program (green field development), where do you get started?&nbsp; I find these questions interesting because there really is no correct answer, but a persons answer will tell you something about how they problem solve.</p> <p>Depending on who you ask you will likely get one of two answers.&nbsp; If you are talking with a data guy, he will say to start with the database.&nbsp; If you are dealing with a visual guy, he will say to start with the UI.&nbsp; Historically, I did a conglomeration of those two ideas, creating both the UI and the database at the same time, letting the two drive each other.&nbsp; But I'm weird, I'm a visual/data guy.</p> <p>Lately though, I've started to thing about a third approach.&nbsp; Starting in the middle.&nbsp; No UI, no database, just the data classes.&nbsp; This approach has been spurred on buy a couple of things.&nbsp; Namely, test driven development (TDD), design patterns, and domain driven design.</p> <p>TDD really is the glue that allows all of this to happen.&nbsp; I can start in the middle because I can run the code in the middle any time I want.&nbsp; I just have to write a test.&nbsp; No having to wade through multiple layers of UI to test out some small chunk ok UI is a great productivity gain for me.</p> <p>Next is design patterns.&nbsp; This is what gives you a reference for how to approach your code.&nbsp; That said, this isn't about making everything absolutely perfect from the get go, but it is nice to know where you are going to made your comprises early on.</p> <p>But all of that does not explain where I begin.&nbsp; That is where Domain Driven Design (DDD) kicks in.&nbsp; For those not familiar with the concept, you can read up on <a href="http://en.wikipedia.org/wiki/Domain-driven_design">wikipedia</a>, buy <a href="http://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1212806974&amp;sr=8-1">The Book</a>, or buy one of the <a href="http://www.amazon.com/s?ie=UTF8&amp;tag=mozilla-20&amp;index=blended&amp;link%5Fcode=qs&amp;field-keywords=domain%20driven%20design&amp;sourceid=Mozilla-search">various other references</a>.&nbsp;&nbsp; One of the concepts in DDD (I'm still reading the book) is setting up something called a <a href="http://www.domaindrivendesign.org/discussion/messageboardarchive/UbiquitousLanguage.html">ubiquitous language</a>.</p> <p>The ubiquitous language is actually trying to solve a problem that I have seen happen on many projects I've been apart of (but not always my fault).&nbsp; At some point there is a linguistic difference between the nouns that the customer uses and the nouns that the programmers use.&nbsp; By focusing on the language first, you are attempting to head that off at the pass.</p> <p>These nouns consist of your domain classes and your tables and columns, they move everywhere...but not always consistently.&nbsp; Consistency does matter.&nbsp; Inconsistency is what leads to rather confusing conversations with people and everyone scratching their heads about "what just happened".&nbsp; So, this is the middle that I start with.</p> <p>But before you start...</p> <p>One of the other guys in the group was an actual data guy.&nbsp; The systems he has written worked with more data than yours does.&nbsp; Trust me on that.&nbsp; Historically, his biggest consideration was data input speed.&nbsp; I don't think I would start in the middle if that was my biggest concern.&nbsp; In that case you do start with the database, you tune for speed.&nbsp; You also don't use as many domain classes, and you tend to throw away your ORM solution and just hit the database layer directly via Ado.Net.</p> <p>But lets face it, that doesn't happen very often.&nbsp; Most business applications aren't going to touch what the architecture can take.&nbsp; It is amazing how many horribly written applications with very inefficient code run just fine.&nbsp; Most of my speed issues could be solved by indexing tables and changing generic lists to generic dictionaries (more on that later).&nbsp; Architect your applications for how they are going to be used.&nbsp; Most of my web applications have a max usage of 100 simultaneous users.&nbsp; If the data access is slightly slower I'll take that hit for programmer efficiency.&nbsp; If you are designing Amazon.com, you code for through-put.</p> <p>OK, someone is going to disagree with this...I can already feel it.&nbsp; Please take a moment to register your displeasure in the comments.&nbsp; I would love to hear it.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Learning Design Patterns</title>
		<link>http://elegantcode.com/2008/04/28/learning-design-patterns/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=learning-design-patterns</link>
		<comments>http://elegantcode.com/2008/04/28/learning-design-patterns/#comments</comments>
		<pubDate>Mon, 28 Apr 2008 19:01:17 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/28/learning-design-patterns/</guid>
		<description><![CDATA[To me this seems like an interesting way to learn about design patterns (watch the video). I wonder if it also works when I hit an ivory-tower architect with POEAA or DDD? I'll keep you posted. PS: I'm really looking forward to the next episode on code reviews and retrospectives.]]></description>
			<content:encoded><![CDATA[To me <a href="http://codebetter.com/blogs/glenn.block/archive/2008/04/28/extreme-patterns-with-oren.aspx" target="_blank">this</a> seems like an interesting way to learn about design patterns (watch the video). I wonder if it also works when I hit an ivory-tower architect with <a href="http://www.bookpool.com/sm/0321127420" target="_blank">POEAA</a> or <a href="http://www.bookpool.com/sm/0321125215" target="_blank">DDD</a>? I'll keep you posted.

PS: I'm really looking forward to the next episode on code reviews and retrospectives.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/28/learning-design-patterns/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>DTO&#8217;s or Serialized Domain Entities?</title>
		<link>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=dtos-or-serialized-domain-entities</link>
		<comments>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/#comments</comments>
		<pubDate>Sun, 27 Apr 2008 08:09:02 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/</guid>
		<description><![CDATA[I hear this question come up time and time again. It is something that I have been dealing with for a while now, and have some experience with doing both ways. I started writing this in response to a DDD thread, but it got rather large and so I turned it into a post. So [...]]]></description>
			<content:encoded><![CDATA[<p>I hear this question come up time and time again. It is something that I have been dealing with for a while now, and have some experience with doing both ways. I started writing this in response to a <a href="http://tech.groups.yahoo.com/group/domaindrivendesign/message/7286" target="_blank">DDD thread</a>, but it got rather large and so I turned it into a post. So here ya go!  <p>I think you have to write DTO's for any sort of long term Enterprise 'system of systems' solution. I fought this for some time, but since moving to an Assembler/DTO I much enjoy the freedom of being able to easily tweak my DTO "snapshot" of the aggregate root I am working with. Maintaining them is very little overhead.  <p>I tend to think of the DTO as a message, a value object. This is necessary because once an entity leaves the boundary it can no longer function within its original meaning. Its methods may be intended to only run within a given context, which is not the same once on another tier. (Things like lazy loading are a good example) Once the DTO/Message arrives to the caller, it very well may (and probably should) be translated/assembled into another model which can represent its new semantics.  <p>Some other advantages of DTO w/ Assembler or Builder:<br>-Can explicitly control the depth of the graph/tree you are serializing.<br>-Can flatten out the data where needed<br>-Can return significantly less data (maybe you only need 3 fields instead of 15)<br>-Not tightly coupled between remote boundaries (this could be a mess when working with a large team)<br>-Interoperable  <p>I wrote a post a while back showing a way to '<a href="http://elegantcode.com/2007/12/05/fun-with-generics-from-repository-to-dto/" target="_blank">templatize</a>' the process. My current implementations are much different with LINQ now, but maybe it could stimulate some ideas for whomever.  <p><em>The next thing I would like to do is remove the assembler altogether. By flagging fields with attributes or using config to specify a mapping, I could then feed <a href="http://blogs.msdn.com/meek/archive/2008/04/25/using-linq-expressions-to-generate-dynamic-methods.aspx" target="_blank">dynamic methods</a> and run a translation/builder strategy.</em>  <p>I know there are concerns about writing and maintaining assembler and DTO, but to me this is the least of the worry, a more challenging scenario would be the UnitOfWork (How to you intended to track remote state, concurrency etc) How about versioning? performance? scala.. well ilities in general?  <p>These kinds of problems lead one from Request/Response, Patterns of Enterprise Architecture &amp; DDD to a Pub/Sub Message Bus - Enterprise Integration Patterns &amp; DDDD.  <p>And to think all you want to do is send some frickin entities to a remote client&nbsp; :)</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Test Data Builders Refined</title>
		<link>http://elegantcode.com/2008/04/26/test-data-builders-refined/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=test-data-builders-refined</link>
		<comments>http://elegantcode.com/2008/04/26/test-data-builders-refined/#comments</comments>
		<pubDate>Sat, 26 Apr 2008 23:08:27 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/26/test-data-builders-refined/</guid>
		<description><![CDATA[Last year, I blogged about Test Data Builders here and here. I still use them heavily in my unit tests for creating objects with test data. Heck, I also use this pattern for fluent interfaces in production code. Here is a simple example of this approach: &#160; 1: public class CustomerBuilder 2: { 3: public [...]]]></description>
			<content:encoded><![CDATA[<p>Last year, I blogged about Test Data Builders <a href="http://vanryswyckjan.blogspot.com/2007/09/test-data-builder-versus-object-mother.html" target="_blank">here</a> and <a href="http://vanryswyckjan.blogspot.com/2007/12/test-data-builder.html" target="_blank">here</a>. I still use them heavily in my unit tests for creating objects with test data. Heck, I also use this pattern for fluent interfaces in production code. Here is a simple example of this approach:</p>  <p>&#160;</p>  <div>   <div class="csharpcode">     <pre class="alteven"><span class="lnum">   1:</span> <span class="kwrd">public</span> <span class="kwrd">class</span> CustomerBuilder</pre>

    <pre class="alteven"><span class="lnum">   2:</span> {</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     <span class="kwrd">public</span> String _firstName = <span class="str">&quot;Homer&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     <span class="kwrd">public</span> String _lastName = <span class="str">&quot;Simpson&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   5:</span>         </pre>

    <pre class="alteven"><span class="lnum">   6:</span>     <span class="kwrd">public</span> CustomerBuilder WithFirstName(String firstName)</pre>

    <pre class="alteven"><span class="lnum">   7:</span>     {</pre>

    <pre class="alteven"><span class="lnum">   8:</span>         _firstName = firstName;</pre>

    <pre class="alteven"><span class="lnum">   9:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;    </pre>

    <pre class="alteven"><span class="lnum">  10:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  11:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  12:</span>     <span class="kwrd">public</span> CustomerBuilder WithLastName(String lastName)</pre>

    <pre class="alteven"><span class="lnum">  13:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  14:</span>         _lastName = lastName;</pre>

    <pre class="alteven"><span class="lnum">  15:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven"><span class="lnum">  16:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  17:</span>         </pre>

    <pre class="alteven"><span class="lnum">  18:</span>     <span class="kwrd">public</span> Customer Build()</pre>

    <pre class="alteven"><span class="lnum">  19:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  20:</span>         <span class="kwrd">return</span> <span class="kwrd">new</span> Customer(_firstName, _lastName);    </pre>

    <pre class="alteven"><span class="lnum">  21:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  22:</span> }</pre>
  </div>
</div>

<pre class="alteven">This fluent builder class can then be used this way:</pre>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> Customer customer = <span class="kwrd">new</span> CustomerBuilder()</pre>

    <pre class="alteven"><span class="lnum">   2:</span>     .WithFirstName(<span class="str">&quot;Homer&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     .WithLastName(<span class="str">&quot;Simpson&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     .Build();</pre>
  </div>
</div>

<p>A while ago, <a href="http://codebetter.com/blogs/gregyoung/default.aspx" target="_blank">Greg Young</a> started a series of blog posts on DDDD (Distributed Domain-Driven Design), which I can highly recommend. Make sure to catch up now you still can because I think that he has a lot of stuff coming up, which I'm really looking forward to.</p>

<p>Anyhow, Greg had a couple of posts on fluent builders, which you can read <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/15/dddd-5-messages-have-fluent-builders.aspx" target="_blank">here</a>, <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/16/dddd-6-fluent-builders-alternate-ending.aspx" target="_blank">here</a> and <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/22/dddd-8-fluent-builders-and-tests.aspx" target="_blank">here</a>. I noticed an interesting approach in the way that the target object is built. Here is an example of this approach:</p>

<p>&#160;</p>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> <span class="kwrd">public</span> <span class="kwrd">class</span> CustomerBuilder</pre>

    <pre class="alteven"><span class="lnum">   2:</span> {</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     <span class="kwrd">public</span> String _firstName = <span class="str">&quot;Homer&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     <span class="kwrd">public</span> String _lastName = <span class="str">&quot;Simpson&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   5:</span>         </pre>

    <pre class="alteven"><span class="lnum">   6:</span>     <span class="kwrd">public</span> CustomerBuilder WithFirstName(String firstName)</pre>

    <pre class="alteven"><span class="lnum">   7:</span>     {</pre>

    <pre class="alteven"><span class="lnum">   8:</span>         _firstName = firstName;</pre>

    <pre class="alteven"><span class="lnum">   9:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;    </pre>

    <pre class="alteven"><span class="lnum">  10:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  11:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  12:</span>     <span class="kwrd">public</span> CustomerBuilder WithLastName(String lastName)</pre>

    <pre class="alteven"><span class="lnum">  13:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  14:</span>         _lastName = lastName;</pre>

    <pre class="alteven"><span class="lnum">  15:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven"><span class="lnum">  16:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  17:</span>         </pre>

    <pre class="alteven"><span class="lnum">  18:</span>     <span class="kwrd">public</span> Customer Build()</pre>

    <pre class="alteven"><span class="lnum">  19:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  20:</span>         <span class="kwrd">return</span> <span class="kwrd">new</span> Customer(_firstName, _lastName);    </pre>

    <pre class="alteven"><span class="lnum">  21:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  22:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  23:</span>     <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">implicit</span> <span class="kwrd">operator</span> Customer(</pre>

    <pre class="alteven"><span class="lnum">  24:</span>         CustomerBuilder builder) </pre>

    <pre class="alteven"><span class="lnum">  25:</span>     {  </pre>

    <pre class="alteven"><span class="lnum">  26:</span>         <span class="kwrd">return</span> builder.Build()    </pre>

    <pre class="alteven"><span class="lnum">  27:</span>     } </pre>

    <pre class="alteven"><span class="lnum">  28:</span> }</pre>
  </div>
</div>

<p>which results in the following usage:</p>

<p>&#160;</p>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> Customer customer = <span class="kwrd">new</span> CustomerBuilder()</pre>

    <pre class="alteven"><span class="lnum">   2:</span>     .WithFirstName(<span class="str">&quot;Homer&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     .WithLastName(<span class="str">&quot;Simpson&quot;</span>);</pre>
  </div>

  <br />Adding an implicit cast operator to the builder class makes that its no longer required to explicitly call the <em>Build</em> method. I keep the <em>Build</em> method around for backwards-compatibility reasons or in case I ever need it again (violating YAGNI in the process, I know, I know). I find that adding the implicit cast operator adds to the readability of the fluent interface, don't you agree?</div>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/26/test-data-builders-refined/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Name Those Patterns</title>
		<link>http://elegantcode.com/2008/04/24/name-those-patterns/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=name-those-patterns</link>
		<comments>http://elegantcode.com/2008/04/24/name-those-patterns/#comments</comments>
		<pubDate>Thu, 24 Apr 2008 16:16:13 +0000</pubDate>
		<dc:creator>Tony Rasa</dc:creator>
				<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/24/name-those-patterns/</guid>
		<description><![CDATA[In yesterday's discussion of Gang of Four Design Patterns, I said &#34;Never name your classes after the pattern itself.&#34;&#160; But then someone pointed out &#34;What about Factory classes?&#34;&#160; &#34;Oh, ok, ALWAYS name classes after the pattern.&#34;&#160; Except for Singleton or Fa&#231;ade, and maybe a few others.&#160; Ok then, NEVER name after the pattern, except when [...]]]></description>
			<content:encoded><![CDATA[<p>In yesterday's discussion of Gang of Four Design Patterns, I said &quot;Never name your classes after the pattern itself.&quot;&#160; But then someone pointed out &quot;What about Factory classes?&quot;&#160; &quot;Oh, ok, ALWAYS name classes after the pattern.&quot;&#160; Except for Singleton or Fa&#231;ade, and maybe a few others.&#160; Ok then, NEVER name after the pattern, except when you do, sometimes, and then ALWAYS name those, unless otherwise noted.</p>  <p>So, here's a list of the 23 Gang of Four patterns, along with when I'd use the name of the pattern in the implementation.&#160; Unless I didn't want to, and then I wouldn't.&#160; Feel free to agree with this list, unless you don't care to.</p>  <h3>Creational Patterns</h3>  <p><strong>Abstract Factory</strong> - After consideration, I have to say that I call my factories Factory, unless they're Repositories, Data Access Layers, DAOs, or something else.&#160; But most of the time, Factory.&#160; Because this would just be wrong:</p>  <pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> FooCreatorMakerThingy { ... }</pre>

<p><strong>Builder</strong> - Hmm, another noun. Guess I'd use this one too, for the same reason as Abstract Factory. Most of the time.&#160; </p>

<p><strong>Factory Method</strong> - I probably would not call my Factory Methods &quot;GetFooFactoryMethod()&quot; because that just looks weird.&#160;&#160; Maybe if we verbify the noun:</p>

<pre class="csharpcode"><span class="kwrd">public</span> Foo FactorialMethodizeFoo() {...}</pre>

<p>Nope, that's still dumb.</p>

<p><strong>Prototype</strong> - &quot;Specify the kinds of objects to create using a prototypical instance...&quot; So we don't care that the class is a &quot;Prototype,&quot; but we do care about the &quot;kind of object&quot; part.&#160; So don't name your prototypical classes &quot;FooPrototype.&quot;&#160; (Unless you just want to.)</p>

<p><strong>Singleton</strong> - Somebody have a case where it makes sense to name your Singleton &quot;Singleton&quot;?&#160; I can't come up with one.&#160; Anyway, you shouldn't be using singletons anymore anyway.&#160; :)</p>

<h3>Structural Patterns</h3>

<p><strong>Adapter</strong> - Adapters are pretty much called Adapters.&#160; Except when they're called Wrappers.&#160; So you can use this name too.</p>

<p><strong>Bridge</strong> - I wouldn't label a participant in a bridge pattern &quot;FooBridge,&quot; unless I was trying to impress on an interview test or something.&#160; And even then, that's a bad idea.&#160; &quot;Ohh, look, I know a complicated design pattern!&#160; Lookitme!&quot;&#160; So don't call your bridges Bridges.&#160; </p>

<p><strong>Composite</strong> - &quot;One of the goals of the Composite&#160; pattern is to make clients unaware of the specific Leaf or Composite classes they're using.&quot;&#160; So you don't want to point out that Composites are in play, that breaks the suspension of disbelief for your audience.&#160; Or something.&#160; Anyway Composites get used all over the place, if we had to label them all then it'd be FooComposite, BarComposite, TreeComposite, EverythingComposite, and what's the fun in that.</p>

<p><strong>Decorator </strong>- I think in general people don't label their Decorators Decorators.&#160; I'm sure there's a counter-example to this, but all of the Stream- and Xml-handling classes I can think of don't point out that they are Decorators.&#160; You'd probably want to stick to more descriptive names, like ASCIIStreamReader instead of cluttering things up with ASCIIStreamDecorator.&#160; We're not decorating the stream (in a literal sense), we're reading it.&#160; </p>

<p><strong>Fa&#231;ade</strong> - Don't call your Fa&#231;ade &quot;FooFa&#231;ade,&quot; that's just silly.&#160; Besides, its too much work to type that cedille into the editor if you're using an ignorant American keyboard.</p>

<p><strong>Flyweight </strong>- This is another pattern where we're going to disguise what's going on inside, in order to simplify the client's usage and make better use of resources.&#160; No reason to flaunt to the client that we're low on resources, because that's a personal and private matter - times are tough all over, and there's no shame in struggling a little bit, and hey who could have predicted that the sub-prime loan market was going to ... anyway, back to patterns.&#160;&#160; Don't call your Flyweights Flyweights, unless what you're modeling is actually a Flyweight, and then thats OK.</p>

<p><strong>Proxy </strong>- This one is standard terminology.&#160; So Proxy's ok.</p>

<h3>Behavioral Patterns (or &quot;<em>MAN this book has a lot of pages in it..</em>&quot;)</h3>

<p><strong>Chain of Responsibility </strong>- Don't even think of naming a class ChainOfResponsibility.&#160; Even with ReSharper, who'd want to have to type that?</p>

<p><strong>Command</strong> - Another convenient noun.&#160; Ok, you can create Commands.&#160; </p>

<p><strong>Interpreter </strong>- This is another pattern that doesn't have one identifying class that you could label &quot;The Interpreter.&quot;&#160; But you might create a Fa&#231;ade, and call that class Interpreter.&#160; I'm not sure if naming one pattern after another is OK or not.&#160; Makes my head hurt.&#160; </p>

<p>Iterator - Another convenient noun.&#160; Iterate away!&#160; Not that you'll have to worry too much about naming classes with this pattern if you use C#, Java, C++ or pretty much any other modern language, since this pattern is built-in.</p>

<p><strong>Mediator </strong>- This pattern promotes loose coupling, most of the time I'd call what it does a &quot;Controller,&quot; or what's being Mediated is more on a service level, so perhaps the word &quot;Service&quot; would end up in the name.&#160; Can't say I've ever called a class specifically &quot;FooMediator&quot;, but I suppose I don't have a problem with it if you want to do that.</p>

<p><strong>Memento </strong>- This pattern captures and externalizes an object's state.&#160; So I think I'd call something like it &quot;State&quot; before &quot;Memento,&quot; since it's fewer letters to type.&#160; Besides, I had to see that movie twice before I really appreciated it.&#160; </p>

<p>Ok, that was just lame, sorry.</p>

<p><strong>Observer </strong>- Oh, you mean like an Event?&#160; Or more like a delegate?&#160; :)</p>

<p><strong>State </strong>- I'd call my implementation of this pattern &quot;Memento,&quot; just to confuse you.&#160; That's just how I roll.</p>

<p><strong>Strategy </strong>- This is another pattern that it seems like we use all the time, without having to give it a fancy Ivory Tower name.&#160; As someone pointed out (sorry I lost the reference to who said that) Microsoft calls all their Strategy implementations Providers now anyway.&#160; Rather than label something a &quot;Strategy&quot; I'd probably just say what the thing does: &quot;CryptographicProvider.&quot;</p>

<p><strong>Template Method</strong> - Yet another pattern where there's no obvious place to hang the name off of, so don't try to force it in there and impress your coworkers with how smart you are.&#160; We know, you're a Design Pattern God, no need to brag about it.</p>

<p><strong>Visitor </strong>- Hmm, not sure where I stand on this one.&#160; I have used this pattern where I've named a visitor &quot;SqlGeneratingVisitor.&quot;&#160; But maybe that was a mistake?&#160; So how about we just declare this one &quot;Free Parking,&quot; and you can do what you want.</p>

<p>&#160;</p>

<p>So, there you go.&#160; A bunch of design patterns, summarized into one neat page.&#160; Now you're ready to ace that interview!&#160; </p>

<p>&#160;</p>

<p><em>disclaimer: I make no guarantees that any or all of the information will not harm you in a technical interview, and if repeating this to an interview board gets your removed from the building by security, its not my fault.&#160; This information comes without warantees or conditions of any kind, and you probably shouldn't have bothered reading it..</em></p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/24/name-those-patterns/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.Net HierarchicalDataSource&lt;T&gt;</title>
		<link>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=aspnet-hierarchicaldatasourcet</link>
		<comments>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/#comments</comments>
		<pubDate>Sun, 06 Apr 2008 06:45:44 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Extension Methods]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/</guid>
		<description><![CDATA[The Asp.net 2.0 tree control, as well as many 3rd party controls binds to hierarchical data via an IHierarchicalDataSource interface. Asp.net provides an implementation OOTB with the HierarchicalDataSourceControl! Easy, let’s hook that up to our… wait this control only takes data in xml format or from a sitemap? Hmm, we don’t want to convert our [...]]]></description>
			<content:encoded><![CDATA[<p>The Asp.net 2.0 tree control, as well as many 3rd party controls binds to hierarchical data via an IHierarchicalDataSource interface. Asp.net provides an implementation OOTB with the HierarchicalDataSourceControl! Easy, let’s hook that up to our… wait this control only takes data in xml format or from a sitemap? Hmm, we don’t want to convert our model to xml, and a sitemap seems inappropriate. We spend a lot time doing great patterns such as MVP &amp; MVC, we should just be able to bind up a list of model objects we already have.  <p>Good thing MS has exposed a set of interfaces and abstract classes in System.Web.UI for us to implement our own IHierarchicalDataSource and HierarchicalDataSourceView and IHierarchicalEnumerable and then IHierarchyData... geesh.  <p>Data? So let's see here, in order to bind our model to the tree control it needs to implement an interface in System.Web.UI? Well that's just not going to happen, so its time for some Elegant Code.  <p><strong>Story:</strong>  <p>Dude, as a software developer, needs to create a custom implementation of IHierarchicalDataSource, and the rest of its parts, so that he can bind a list of objects to a tree control in asp.net  <ul> <li>Use existing model to bind to tree control  <li>Must be able to reuse data source for future models  <li>Model must not contain a reference to System.Web.UI </li></ul> <p>In order to setup our model to address the acceptance criteria we are going to use an interface with a self-referencing generic declaration, where the type will specify itself as the concrete type. This will let us create a nice generic hierarchical model to pass into our datasource without resorting to any sort of base class.  <div><pre class="csharpcode"><span class="kwrd">namespace</span> POC.Common
{
    <span class="kwrd">public</span> <span class="kwrd">interface</span> IModelWithHierarchy&lt;T&gt;
    {
        <span class="kwrd">string</span> Name { get; set; }
        T Parent { get; set; }
        List&lt;T&gt; Children { get; set; }
    }
}</pre><em>Lets note that we have placed the interface in a Common namespace which represents a shared.dll between Web &amp; Model</em> </div>
<p>The UI part of criteria is now a bit more tricky because the HierarchicalDataSourceView expects a IHierarchicalEnumerable datasource which contains IHierarchyData. Said IHierarchyData != IModelWithHierarchy&lt;T&gt;.&nbsp; Luckily someone else has already figured this one out for us. We need an <a href="http://www.dofactory.com/Patterns/PatternAdapter.aspx" target="_blank">adapter</a>. 
<p><a href="http://elegantcode.com/wp-content/uploads/2008/04/image7.png" target="_blank"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="123" alt="image" src="http://elegantcode.com/wp-content/uploads/2008/04/image-thumb7.png" width="244" border="0"></a> 
<p>To instantiate our HierarchyData adapter objects we will use an extension method and linq 'Select' to convert our datasource IEnumerable&lt;T&gt; of IModelWithHierarchy&lt;T&gt; 's to an IHierarchicalEnumerable of IHierarchyData. 
<div><pre class="csharpcode"><span class="kwrd">namespace</span> POC.Web.HierarchyExtensions
{
    <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">class</span> HierarchyConverter
    {
        <span class="kwrd">public</span> <span class="kwrd">static</span> HierarchicalModelList
            ToHierarchicalModelList&lt;T&gt;(<span class="kwrd">this</span> IEnumerable&lt;T&gt; modelWithHierarchy)
            <span class="kwrd">where</span> T : IModelWithHierarchy&lt;T&gt;
        {
            <span class="kwrd">return</span> <span class="kwrd">new</span> HierarchicalModelList(
                modelWithHierarchy.Select(m =&gt; <span class="kwrd">new</span> HierarchyData&lt;T&gt;(m) <span class="kwrd">as</span> IHierarchyData));
        }
    }
}</pre></div>
<p>I am not going to bore you with the implementation details of the HierarachyData&lt;T&gt; adapter and the .net abstractions, but have dropped the <a href="http://elegantcode.com/wp-content/uploads/2008/04/hierarchydatasourcepoc.zip" target="_blank">source code here</a> if your interested. The end result: 
<div><pre class="csharpcode">TreeView1.DataSource = <span class="kwrd">new</span> HierarchicalModelDataSource&lt;Category&gt; { DataSource = Categories };
TreeView1.DataBind();
TreeView1.CollapseAll();</pre></div>
<p>Story complete!&nbsp; </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is Elegant Code to me?</title>
		<link>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-is-elegant-code-to-me</link>
		<comments>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/#comments</comments>
		<pubDate>Mon, 31 Mar 2008 03:47:45 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Patterns and Practices]]></category>
		<category><![CDATA[Widgets of Wisdom]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/</guid>
		<description><![CDATA[This question keeps popping up around here ("around here" being the loose conglomeration that makes up the Elegant Code group).&#160; It isn't easy to describe.&#160; And really, the notion of what constitutes elegance in code changes over time.&#160; There is no static "this is good code" test, and I doubt there ever will be.&#160; Plus, [...]]]></description>
			<content:encoded><![CDATA[<p>This question keeps popping up around here ("around here" being the loose conglomeration that makes up the Elegant Code group).&nbsp; It isn't easy to describe.&nbsp; And really, the notion of what constitutes elegance in code changes over time.&nbsp; There is no static "this is good code" test, and I doubt there ever will be.&nbsp; Plus, what makes good code in one language, may not apply to the next.</p> <p>So let me state for the record: today's elegant code is tomorrow's drivel.&nbsp; Don't feel bad, many writers have the same problems.&nbsp; What was super amazing back in the day is now rubbish or near unreadable.&nbsp; I am thinking of the Victorian writing that Hemingway usurped, and now Hemingway himself is almost unreadable (to me anyway).&nbsp; Tastes change with the times.&nbsp; That is a simple fact.</p> <p>So what can you do about this?&nbsp; As I say that old writing sucks to read (read Washington Irvine lately?), great works of literature still abound (for instance, Hemingway is still a great writer -- even if I prefer Tolkien) .&nbsp; So take some notes from them, and from other crafts in looking for the answer.&nbsp; If you want a cliff notes version of what this is going to tell you, it is this: you must always push yourself to get better.</p> <ol> <li>Find a good teacher.&nbsp; Nothing is better than sitting at the feet of a master who can nudge you along in the right direction.&nbsp; While early mistakes can often be corrected easily with a little bit of guidance, after they have had some time to fester all bets are off.&nbsp; I believe a fare number of "Anit-patterns" were created by self taught developers (find <a href="http://elegantcode.com/2008/03/21/sql-ejaculation/">SQL Ejaculation</a> on this site).&nbsp; <li>Read.&nbsp;&nbsp; Good writer are first good readers.&nbsp; Start with Code Complete, move into a good Patterns book, get MSDN Magazine, find some bloggers your like, but keep moving.&nbsp; You will NEVER be done reading.&nbsp; I imagine that even Martin Fowler has a "to read" booklist a mile long.  <li>Read code.&nbsp;&nbsp; There are a plethora of open source project just waiting to be read -- do so.&nbsp; This is the single best way to expand your coding repertoire, find things your language can do that you didn't even know about..&nbsp;&nbsp; Scott Hanselman has recently popularized this idea, and I thank him for it.  <li>Practice.&nbsp; This means writing small applications at home.&nbsp; You get an idea that you want to try out -- do it.&nbsp; I don't mean you have to write finished applications, just have some exploring time.&nbsp; I remember talking with an 80 year old master woodworker (he lives down the street from me), who was telling me how many time he would practice making dovetail joints before he felt competent.&nbsp; It was years worth.  <li>Pay Attention.&nbsp; Being good at anything really amounts to that.&nbsp; And don't just pay attention when reading, writing, or talking about code.&nbsp; Inspiration come from everywhere, any good artist will tell you that.&nbsp; Pay attention when you cook, when you work on the car, when you are wood working, playing an instrument, whatever it is that you do.&nbsp; This will help you in the end, event if it is just learning the amount of dedication it really takes to become good at something.  <li>Beware of preferences.&nbsp; Any time I hear someone start a statement with "I prefer code to ..." you know things are going downhill.&nbsp; If you find someone who cares more about how your code is formatted then how it is written you have found yourself in a mess.&nbsp; More importantly, beware of them in yourself.&nbsp;&nbsp; Having code style standard is important, but it isn't worth loosing sleep over.&nbsp; This also pertains to inheritance, patterns, use of particular classes (e.g. always using the generic list class in C# when a Dictionary would be better).&nbsp; <li>No Sacred Cows. Believe no single source of information.&nbsp; This means not thinking that Microsoft, Sun, IBM, Richard Stallman, or anyone else will have all of the correct answers.&nbsp; Apply the scientific method and do some research, experiment, and question the authority.&nbsp;&nbsp; There should be no sacred cows in programming.  <li>Talk with others.&nbsp; Join a user group, show up every now and then, heckle the presenter, and -maybe- speak.&nbsp; You want a broad range of people who you can talk to and bounce ideas off of.&nbsp; This will help you from becoming that crazy guy in the corner who vehemently states that all your code should be in one file.&nbsp; Having a mentor (mentioned earlier) is great, but having people around to push you from multiple directions is also good.  <li>Learn languages.&nbsp; This gets back to the "read code" idea. Make that multiple types of languages as well.&nbsp; If you know C# or Java and SQL, learn Python or Ruby, get really good at JavaScript.&nbsp; If you want something really out there, learn MDX.&nbsp; This is also a repertoire thing.&nbsp; Seeing how other languages work will make you rethink your ideas about what your current code, in whatever languages you are working in, should look like.  <li>Keep Thinking!!!&nbsp; That is possibly the most important point.&nbsp; Keep thinking.&nbsp; Don't just evaluate something once and never return, go back and reevaluate. Did the technique work?&nbsp; How could it have been better?&nbsp;&nbsp; The idea is continual improvement.  <li>Switch jobs every now and then.&nbsp; When I announced I was leaving my first programming job out of college, the VP of R&amp;D had me sit down with him and talk.&nbsp; He wasn't trying to keep me (he knew I was moving to be closer to family), he wanted to give me some advice.&nbsp; He told me to change jobs every three years.&nbsp; After you have been with a place for three years you have probably learned everything you are going to learn and it time to move on.&nbsp; This doesn't mean changing companies either.&nbsp; I've been with the same company now for four years, but I've had three different jobs.&nbsp; If you have been writing commercial applications, become a consultant - or vise-versa, become a test engineer, expand your boundaries.&nbsp; Again, this is about pushing yourself to be better.  <li>Have a Hobby. But don't ask about me, I have too many.&nbsp;&nbsp; Creating software is about creating things.&nbsp; So I suggest picking a hobby that involves creating something.&nbsp; Popular hobbies amongst programmers I know include: cooking, music, woodworking, beer making, and BBQ (which is different than cooking).&nbsp; But don't discount sports (golf is always popular), computer games, and board games.&nbsp; Anything that promotes the development of skill levels can't be a bad thing. </li></ol> <p>And I'm stopping there.&nbsp; This is my beginner's guide to how to become the writer elegant code.&nbsp; If you have others you think I missed, add them too the comments.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>LINQ Framework Design Guidelines</title>
		<link>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=linq-framework-design-guidelines-2</link>
		<comments>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/#comments</comments>
		<pubDate>Wed, 19 Mar 2008 01:53:22 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[Microsoft]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/</guid>
		<description><![CDATA[Excellent article on design considerations for LINQ. Looks like these concepts will be incorporated into the 2nd Edition of Framework Design Guidelines: Conventions, Idioms, and Patterns for Reuseable .NET Libraries. Cant wait to get my hands on a copy of that!]]></description>
			<content:encoded><![CDATA[<p>Excellent article on <a href="http://blogs.msdn.com/mirceat/archive/2008/03/13/linq-framework-design-guidelines.aspx" target="_blank">design considerations for LINQ</a>. Looks like these concepts will be incorporated into the 2nd Edition of <a href="http://www.amazon.com/Framework-Design-Guidelines-Conventions-Development/dp/0321545613/ref=pd_bbs_sr_2?ie=UTF8&amp;s=books&amp;qid=1200508590&amp;sr=1-2" target="_blank">Framework Design Guidelines: Conventions, Idioms, and Patterns for Reuseable .NET Libraries</a>. Cant wait to get my hands on a copy of that!</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Boise Code Camp 2008</title>
		<link>http://elegantcode.com/2008/03/09/boise-code-camp-2008/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=boise-code-camp-2008</link>
		<comments>http://elegantcode.com/2008/03/09/boise-code-camp-2008/#comments</comments>
		<pubDate>Mon, 10 Mar 2008 02:25:35 +0000</pubDate>
		<dc:creator>Alex Mueller</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Code Camp]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Patterns and Practices]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/09/boise-code-camp-2008/</guid>
		<description><![CDATA[Boise Code Camp 2008 was awesome. Thank you to David Starr and his wife, Eleanor, for taking ownership of this event and dedicating many months of their time to make this a reality. Thank you to the presenters for having the passion and desire to present technologies and practices, both new and not-as-new. Thank you [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Boise Code Camp 2008" href="http://www.boisecodecamp.org/" target="_blank">Boise Code Camp 2008</a> was awesome. Thank you to <a title="David Starr" href="http://elegantcode.com/about/david-starr/" target="_blank">David Starr</a> and his wife, <a title="Domestic Oblivion" href="http://domesticoblivion.com/" target="_blank">Eleanor</a>, for taking ownership of this event and dedicating many months of their time to make this a reality. Thank you to the presenters for having the passion and desire to present technologies and practices, both new and not-as-new. Thank you to the many volunteers who put in countless hours to help coordinate the weekend's events. Thank you to the campers for attending and making this year's code camp a success.</p>  <p>I am sure everyone will be blogging about all the great sessions they attended at code camp. I enjoyed all of the presentations I attended, and I am excited to explore some new material. </p>  <p>I want to share a few thoughts on my presentation dealing with Model-View-Presenter in ASP.NET after listening to <a title="Scott Hanselman" href="http://www.hanselman.com" target="_blank">Scott Hanselman's</a> session on the <a title="ASP. NET MVC Project" href="http://www.asp.net/mvc" target="_blank">ASP.NET MVC Project</a>. I may have the percentage incorrect, but Scott mentioned something like it is predicted that only 10% of the ASP.NET community currently using Web Forms will adopt and use the MVC framework. The MVC framework is an addition to ASP.NET, it is not a replacement. What this means is that Model-View-Presenter will still be a viable pattern to implement with your ASP.NET applications, and it is not going away. ASP.NET Web Forms will not be going away either.</p>  <p>Use MVP to get your third party controls under test. Use MVP to provide that separation of concerns in your legacy applications. Use it entirely or in conjunction with the MVC framework. It is all about testability. <a title="Glenn Block" href="http://blogs.msdn.com/gblock/" target="_blank">Glenn Block</a> presented on the Web Client Software Factory, and what pattern does this implement? Model-View-Presenter.</p>  <p>I am providing my <a title="Presentation and Source Code" href="http://muellerdesigns.net/DasBlog/content/binary/ModeViewPresenter.AlexMueller.BoiseCodeCamp2008.rar" target="_blank">presentation and source code (3.67 MB)</a> from my talk on MVP. It will be available via the <a title="Boise Code Camp" href="http://www.boisecodecamp.org/" target="_blank">Boise Code Camp</a> site as well. </p>  <p>Again, thank the many individuals and their families who sacrificed their time to bring to the local community this years code camp.</p>  <p>Around the office's water cooler on Monday, I will be able to say, &quot;and this one time, at code camp...&quot;</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/09/boise-code-camp-2008/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Microsoft releases Unity, an IoC library</title>
	<atom:link href="http://elegantcode.com/tag/design-patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Elegant Code &#187; Design Patterns</title>
	<atom:link href="http://elegantcode.com/tag/design-patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Where to start a new program</title>
		<link>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=where-to-start-a-new-program</link>
		<comments>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/#comments</comments>
		<pubDate>Sun, 08 Jun 2008 19:50:39 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/06/08/where-to-start-a-new-program/</guid>
		<description><![CDATA[Here as an interesting question that popped up at my user group meeting last night, when you are starting a new program (green field development), where do you get started?&#160; I find these questions interesting because there really is no correct answer, but a persons answer will tell you something about how they problem solve. [...]]]></description>
			<content:encoded><![CDATA[<p>Here as an interesting question that popped up at my user group meeting last night, when you are starting a new program (green field development), where do you get started?&nbsp; I find these questions interesting because there really is no correct answer, but a persons answer will tell you something about how they problem solve.</p> <p>Depending on who you ask you will likely get one of two answers.&nbsp; If you are talking with a data guy, he will say to start with the database.&nbsp; If you are dealing with a visual guy, he will say to start with the UI.&nbsp; Historically, I did a conglomeration of those two ideas, creating both the UI and the database at the same time, letting the two drive each other.&nbsp; But I'm weird, I'm a visual/data guy.</p> <p>Lately though, I've started to thing about a third approach.&nbsp; Starting in the middle.&nbsp; No UI, no database, just the data classes.&nbsp; This approach has been spurred on buy a couple of things.&nbsp; Namely, test driven development (TDD), design patterns, and domain driven design.</p> <p>TDD really is the glue that allows all of this to happen.&nbsp; I can start in the middle because I can run the code in the middle any time I want.&nbsp; I just have to write a test.&nbsp; No having to wade through multiple layers of UI to test out some small chunk ok UI is a great productivity gain for me.</p> <p>Next is design patterns.&nbsp; This is what gives you a reference for how to approach your code.&nbsp; That said, this isn't about making everything absolutely perfect from the get go, but it is nice to know where you are going to made your comprises early on.</p> <p>But all of that does not explain where I begin.&nbsp; That is where Domain Driven Design (DDD) kicks in.&nbsp; For those not familiar with the concept, you can read up on <a href="http://en.wikipedia.org/wiki/Domain-driven_design">wikipedia</a>, buy <a href="http://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1212806974&amp;sr=8-1">The Book</a>, or buy one of the <a href="http://www.amazon.com/s?ie=UTF8&amp;tag=mozilla-20&amp;index=blended&amp;link%5Fcode=qs&amp;field-keywords=domain%20driven%20design&amp;sourceid=Mozilla-search">various other references</a>.&nbsp;&nbsp; One of the concepts in DDD (I'm still reading the book) is setting up something called a <a href="http://www.domaindrivendesign.org/discussion/messageboardarchive/UbiquitousLanguage.html">ubiquitous language</a>.</p> <p>The ubiquitous language is actually trying to solve a problem that I have seen happen on many projects I've been apart of (but not always my fault).&nbsp; At some point there is a linguistic difference between the nouns that the customer uses and the nouns that the programmers use.&nbsp; By focusing on the language first, you are attempting to head that off at the pass.</p> <p>These nouns consist of your domain classes and your tables and columns, they move everywhere...but not always consistently.&nbsp; Consistency does matter.&nbsp; Inconsistency is what leads to rather confusing conversations with people and everyone scratching their heads about "what just happened".&nbsp; So, this is the middle that I start with.</p> <p>But before you start...</p> <p>One of the other guys in the group was an actual data guy.&nbsp; The systems he has written worked with more data than yours does.&nbsp; Trust me on that.&nbsp; Historically, his biggest consideration was data input speed.&nbsp; I don't think I would start in the middle if that was my biggest concern.&nbsp; In that case you do start with the database, you tune for speed.&nbsp; You also don't use as many domain classes, and you tend to throw away your ORM solution and just hit the database layer directly via Ado.Net.</p> <p>But lets face it, that doesn't happen very often.&nbsp; Most business applications aren't going to touch what the architecture can take.&nbsp; It is amazing how many horribly written applications with very inefficient code run just fine.&nbsp; Most of my speed issues could be solved by indexing tables and changing generic lists to generic dictionaries (more on that later).&nbsp; Architect your applications for how they are going to be used.&nbsp; Most of my web applications have a max usage of 100 simultaneous users.&nbsp; If the data access is slightly slower I'll take that hit for programmer efficiency.&nbsp; If you are designing Amazon.com, you code for through-put.</p> <p>OK, someone is going to disagree with this...I can already feel it.&nbsp; Please take a moment to register your displeasure in the comments.&nbsp; I would love to hear it.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Learning Design Patterns</title>
		<link>http://elegantcode.com/2008/04/28/learning-design-patterns/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=learning-design-patterns</link>
		<comments>http://elegantcode.com/2008/04/28/learning-design-patterns/#comments</comments>
		<pubDate>Mon, 28 Apr 2008 19:01:17 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/28/learning-design-patterns/</guid>
		<description><![CDATA[To me this seems like an interesting way to learn about design patterns (watch the video). I wonder if it also works when I hit an ivory-tower architect with POEAA or DDD? I'll keep you posted. PS: I'm really looking forward to the next episode on code reviews and retrospectives.]]></description>
			<content:encoded><![CDATA[To me <a href="http://codebetter.com/blogs/glenn.block/archive/2008/04/28/extreme-patterns-with-oren.aspx" target="_blank">this</a> seems like an interesting way to learn about design patterns (watch the video). I wonder if it also works when I hit an ivory-tower architect with <a href="http://www.bookpool.com/sm/0321127420" target="_blank">POEAA</a> or <a href="http://www.bookpool.com/sm/0321125215" target="_blank">DDD</a>? I'll keep you posted.

PS: I'm really looking forward to the next episode on code reviews and retrospectives.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/28/learning-design-patterns/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>DTO&#8217;s or Serialized Domain Entities?</title>
		<link>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=dtos-or-serialized-domain-entities</link>
		<comments>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/#comments</comments>
		<pubDate>Sun, 27 Apr 2008 08:09:02 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/</guid>
		<description><![CDATA[I hear this question come up time and time again. It is something that I have been dealing with for a while now, and have some experience with doing both ways. I started writing this in response to a DDD thread, but it got rather large and so I turned it into a post. So [...]]]></description>
			<content:encoded><![CDATA[<p>I hear this question come up time and time again. It is something that I have been dealing with for a while now, and have some experience with doing both ways. I started writing this in response to a <a href="http://tech.groups.yahoo.com/group/domaindrivendesign/message/7286" target="_blank">DDD thread</a>, but it got rather large and so I turned it into a post. So here ya go!  <p>I think you have to write DTO's for any sort of long term Enterprise 'system of systems' solution. I fought this for some time, but since moving to an Assembler/DTO I much enjoy the freedom of being able to easily tweak my DTO "snapshot" of the aggregate root I am working with. Maintaining them is very little overhead.  <p>I tend to think of the DTO as a message, a value object. This is necessary because once an entity leaves the boundary it can no longer function within its original meaning. Its methods may be intended to only run within a given context, which is not the same once on another tier. (Things like lazy loading are a good example) Once the DTO/Message arrives to the caller, it very well may (and probably should) be translated/assembled into another model which can represent its new semantics.  <p>Some other advantages of DTO w/ Assembler or Builder:<br>-Can explicitly control the depth of the graph/tree you are serializing.<br>-Can flatten out the data where needed<br>-Can return significantly less data (maybe you only need 3 fields instead of 15)<br>-Not tightly coupled between remote boundaries (this could be a mess when working with a large team)<br>-Interoperable  <p>I wrote a post a while back showing a way to '<a href="http://elegantcode.com/2007/12/05/fun-with-generics-from-repository-to-dto/" target="_blank">templatize</a>' the process. My current implementations are much different with LINQ now, but maybe it could stimulate some ideas for whomever.  <p><em>The next thing I would like to do is remove the assembler altogether. By flagging fields with attributes or using config to specify a mapping, I could then feed <a href="http://blogs.msdn.com/meek/archive/2008/04/25/using-linq-expressions-to-generate-dynamic-methods.aspx" target="_blank">dynamic methods</a> and run a translation/builder strategy.</em>  <p>I know there are concerns about writing and maintaining assembler and DTO, but to me this is the least of the worry, a more challenging scenario would be the UnitOfWork (How to you intended to track remote state, concurrency etc) How about versioning? performance? scala.. well ilities in general?  <p>These kinds of problems lead one from Request/Response, Patterns of Enterprise Architecture &amp; DDD to a Pub/Sub Message Bus - Enterprise Integration Patterns &amp; DDDD.  <p>And to think all you want to do is send some frickin entities to a remote client&nbsp; :)</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Test Data Builders Refined</title>
		<link>http://elegantcode.com/2008/04/26/test-data-builders-refined/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=test-data-builders-refined</link>
		<comments>http://elegantcode.com/2008/04/26/test-data-builders-refined/#comments</comments>
		<pubDate>Sat, 26 Apr 2008 23:08:27 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/26/test-data-builders-refined/</guid>
		<description><![CDATA[Last year, I blogged about Test Data Builders here and here. I still use them heavily in my unit tests for creating objects with test data. Heck, I also use this pattern for fluent interfaces in production code. Here is a simple example of this approach: &#160; 1: public class CustomerBuilder 2: { 3: public [...]]]></description>
			<content:encoded><![CDATA[<p>Last year, I blogged about Test Data Builders <a href="http://vanryswyckjan.blogspot.com/2007/09/test-data-builder-versus-object-mother.html" target="_blank">here</a> and <a href="http://vanryswyckjan.blogspot.com/2007/12/test-data-builder.html" target="_blank">here</a>. I still use them heavily in my unit tests for creating objects with test data. Heck, I also use this pattern for fluent interfaces in production code. Here is a simple example of this approach:</p>  <p>&#160;</p>  <div>   <div class="csharpcode">     <pre class="alteven"><span class="lnum">   1:</span> <span class="kwrd">public</span> <span class="kwrd">class</span> CustomerBuilder</pre>

    <pre class="alteven"><span class="lnum">   2:</span> {</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     <span class="kwrd">public</span> String _firstName = <span class="str">&quot;Homer&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     <span class="kwrd">public</span> String _lastName = <span class="str">&quot;Simpson&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   5:</span>         </pre>

    <pre class="alteven"><span class="lnum">   6:</span>     <span class="kwrd">public</span> CustomerBuilder WithFirstName(String firstName)</pre>

    <pre class="alteven"><span class="lnum">   7:</span>     {</pre>

    <pre class="alteven"><span class="lnum">   8:</span>         _firstName = firstName;</pre>

    <pre class="alteven"><span class="lnum">   9:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;    </pre>

    <pre class="alteven"><span class="lnum">  10:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  11:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  12:</span>     <span class="kwrd">public</span> CustomerBuilder WithLastName(String lastName)</pre>

    <pre class="alteven"><span class="lnum">  13:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  14:</span>         _lastName = lastName;</pre>

    <pre class="alteven"><span class="lnum">  15:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven"><span class="lnum">  16:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  17:</span>         </pre>

    <pre class="alteven"><span class="lnum">  18:</span>     <span class="kwrd">public</span> Customer Build()</pre>

    <pre class="alteven"><span class="lnum">  19:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  20:</span>         <span class="kwrd">return</span> <span class="kwrd">new</span> Customer(_firstName, _lastName);    </pre>

    <pre class="alteven"><span class="lnum">  21:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  22:</span> }</pre>
  </div>
</div>

<pre class="alteven">This fluent builder class can then be used this way:</pre>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> Customer customer = <span class="kwrd">new</span> CustomerBuilder()</pre>

    <pre class="alteven"><span class="lnum">   2:</span>     .WithFirstName(<span class="str">&quot;Homer&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     .WithLastName(<span class="str">&quot;Simpson&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     .Build();</pre>
  </div>
</div>

<p>A while ago, <a href="http://codebetter.com/blogs/gregyoung/default.aspx" target="_blank">Greg Young</a> started a series of blog posts on DDDD (Distributed Domain-Driven Design), which I can highly recommend. Make sure to catch up now you still can because I think that he has a lot of stuff coming up, which I'm really looking forward to.</p>

<p>Anyhow, Greg had a couple of posts on fluent builders, which you can read <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/15/dddd-5-messages-have-fluent-builders.aspx" target="_blank">here</a>, <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/16/dddd-6-fluent-builders-alternate-ending.aspx" target="_blank">here</a> and <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/22/dddd-8-fluent-builders-and-tests.aspx" target="_blank">here</a>. I noticed an interesting approach in the way that the target object is built. Here is an example of this approach:</p>

<p>&#160;</p>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> <span class="kwrd">public</span> <span class="kwrd">class</span> CustomerBuilder</pre>

    <pre class="alteven"><span class="lnum">   2:</span> {</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     <span class="kwrd">public</span> String _firstName = <span class="str">&quot;Homer&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     <span class="kwrd">public</span> String _lastName = <span class="str">&quot;Simpson&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   5:</span>         </pre>

    <pre class="alteven"><span class="lnum">   6:</span>     <span class="kwrd">public</span> CustomerBuilder WithFirstName(String firstName)</pre>

    <pre class="alteven"><span class="lnum">   7:</span>     {</pre>

    <pre class="alteven"><span class="lnum">   8:</span>         _firstName = firstName;</pre>

    <pre class="alteven"><span class="lnum">   9:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;    </pre>

    <pre class="alteven"><span class="lnum">  10:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  11:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  12:</span>     <span class="kwrd">public</span> CustomerBuilder WithLastName(String lastName)</pre>

    <pre class="alteven"><span class="lnum">  13:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  14:</span>         _lastName = lastName;</pre>

    <pre class="alteven"><span class="lnum">  15:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven"><span class="lnum">  16:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  17:</span>         </pre>

    <pre class="alteven"><span class="lnum">  18:</span>     <span class="kwrd">public</span> Customer Build()</pre>

    <pre class="alteven"><span class="lnum">  19:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  20:</span>         <span class="kwrd">return</span> <span class="kwrd">new</span> Customer(_firstName, _lastName);    </pre>

    <pre class="alteven"><span class="lnum">  21:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  22:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  23:</span>     <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">implicit</span> <span class="kwrd">operator</span> Customer(</pre>

    <pre class="alteven"><span class="lnum">  24:</span>         CustomerBuilder builder) </pre>

    <pre class="alteven"><span class="lnum">  25:</span>     {  </pre>

    <pre class="alteven"><span class="lnum">  26:</span>         <span class="kwrd">return</span> builder.Build()    </pre>

    <pre class="alteven"><span class="lnum">  27:</span>     } </pre>

    <pre class="alteven"><span class="lnum">  28:</span> }</pre>
  </div>
</div>

<p>which results in the following usage:</p>

<p>&#160;</p>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> Customer customer = <span class="kwrd">new</span> CustomerBuilder()</pre>

    <pre class="alteven"><span class="lnum">   2:</span>     .WithFirstName(<span class="str">&quot;Homer&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     .WithLastName(<span class="str">&quot;Simpson&quot;</span>);</pre>
  </div>

  <br />Adding an implicit cast operator to the builder class makes that its no longer required to explicitly call the <em>Build</em> method. I keep the <em>Build</em> method around for backwards-compatibility reasons or in case I ever need it again (violating YAGNI in the process, I know, I know). I find that adding the implicit cast operator adds to the readability of the fluent interface, don't you agree?</div>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/26/test-data-builders-refined/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Name Those Patterns</title>
		<link>http://elegantcode.com/2008/04/24/name-those-patterns/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=name-those-patterns</link>
		<comments>http://elegantcode.com/2008/04/24/name-those-patterns/#comments</comments>
		<pubDate>Thu, 24 Apr 2008 16:16:13 +0000</pubDate>
		<dc:creator>Tony Rasa</dc:creator>
				<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/24/name-those-patterns/</guid>
		<description><![CDATA[In yesterday's discussion of Gang of Four Design Patterns, I said &#34;Never name your classes after the pattern itself.&#34;&#160; But then someone pointed out &#34;What about Factory classes?&#34;&#160; &#34;Oh, ok, ALWAYS name classes after the pattern.&#34;&#160; Except for Singleton or Fa&#231;ade, and maybe a few others.&#160; Ok then, NEVER name after the pattern, except when [...]]]></description>
			<content:encoded><![CDATA[<p>In yesterday's discussion of Gang of Four Design Patterns, I said &quot;Never name your classes after the pattern itself.&quot;&#160; But then someone pointed out &quot;What about Factory classes?&quot;&#160; &quot;Oh, ok, ALWAYS name classes after the pattern.&quot;&#160; Except for Singleton or Fa&#231;ade, and maybe a few others.&#160; Ok then, NEVER name after the pattern, except when you do, sometimes, and then ALWAYS name those, unless otherwise noted.</p>  <p>So, here's a list of the 23 Gang of Four patterns, along with when I'd use the name of the pattern in the implementation.&#160; Unless I didn't want to, and then I wouldn't.&#160; Feel free to agree with this list, unless you don't care to.</p>  <h3>Creational Patterns</h3>  <p><strong>Abstract Factory</strong> - After consideration, I have to say that I call my factories Factory, unless they're Repositories, Data Access Layers, DAOs, or something else.&#160; But most of the time, Factory.&#160; Because this would just be wrong:</p>  <pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> FooCreatorMakerThingy { ... }</pre>

<p><strong>Builder</strong> - Hmm, another noun. Guess I'd use this one too, for the same reason as Abstract Factory. Most of the time.&#160; </p>

<p><strong>Factory Method</strong> - I probably would not call my Factory Methods &quot;GetFooFactoryMethod()&quot; because that just looks weird.&#160;&#160; Maybe if we verbify the noun:</p>

<pre class="csharpcode"><span class="kwrd">public</span> Foo FactorialMethodizeFoo() {...}</pre>

<p>Nope, that's still dumb.</p>

<p><strong>Prototype</strong> - &quot;Specify the kinds of objects to create using a prototypical instance...&quot; So we don't care that the class is a &quot;Prototype,&quot; but we do care about the &quot;kind of object&quot; part.&#160; So don't name your prototypical classes &quot;FooPrototype.&quot;&#160; (Unless you just want to.)</p>

<p><strong>Singleton</strong> - Somebody have a case where it makes sense to name your Singleton &quot;Singleton&quot;?&#160; I can't come up with one.&#160; Anyway, you shouldn't be using singletons anymore anyway.&#160; :)</p>

<h3>Structural Patterns</h3>

<p><strong>Adapter</strong> - Adapters are pretty much called Adapters.&#160; Except when they're called Wrappers.&#160; So you can use this name too.</p>

<p><strong>Bridge</strong> - I wouldn't label a participant in a bridge pattern &quot;FooBridge,&quot; unless I was trying to impress on an interview test or something.&#160; And even then, that's a bad idea.&#160; &quot;Ohh, look, I know a complicated design pattern!&#160; Lookitme!&quot;&#160; So don't call your bridges Bridges.&#160; </p>

<p><strong>Composite</strong> - &quot;One of the goals of the Composite&#160; pattern is to make clients unaware of the specific Leaf or Composite classes they're using.&quot;&#160; So you don't want to point out that Composites are in play, that breaks the suspension of disbelief for your audience.&#160; Or something.&#160; Anyway Composites get used all over the place, if we had to label them all then it'd be FooComposite, BarComposite, TreeComposite, EverythingComposite, and what's the fun in that.</p>

<p><strong>Decorator </strong>- I think in general people don't label their Decorators Decorators.&#160; I'm sure there's a counter-example to this, but all of the Stream- and Xml-handling classes I can think of don't point out that they are Decorators.&#160; You'd probably want to stick to more descriptive names, like ASCIIStreamReader instead of cluttering things up with ASCIIStreamDecorator.&#160; We're not decorating the stream (in a literal sense), we're reading it.&#160; </p>

<p><strong>Fa&#231;ade</strong> - Don't call your Fa&#231;ade &quot;FooFa&#231;ade,&quot; that's just silly.&#160; Besides, its too much work to type that cedille into the editor if you're using an ignorant American keyboard.</p>

<p><strong>Flyweight </strong>- This is another pattern where we're going to disguise what's going on inside, in order to simplify the client's usage and make better use of resources.&#160; No reason to flaunt to the client that we're low on resources, because that's a personal and private matter - times are tough all over, and there's no shame in struggling a little bit, and hey who could have predicted that the sub-prime loan market was going to ... anyway, back to patterns.&#160;&#160; Don't call your Flyweights Flyweights, unless what you're modeling is actually a Flyweight, and then thats OK.</p>

<p><strong>Proxy </strong>- This one is standard terminology.&#160; So Proxy's ok.</p>

<h3>Behavioral Patterns (or &quot;<em>MAN this book has a lot of pages in it..</em>&quot;)</h3>

<p><strong>Chain of Responsibility </strong>- Don't even think of naming a class ChainOfResponsibility.&#160; Even with ReSharper, who'd want to have to type that?</p>

<p><strong>Command</strong> - Another convenient noun.&#160; Ok, you can create Commands.&#160; </p>

<p><strong>Interpreter </strong>- This is another pattern that doesn't have one identifying class that you could label &quot;The Interpreter.&quot;&#160; But you might create a Fa&#231;ade, and call that class Interpreter.&#160; I'm not sure if naming one pattern after another is OK or not.&#160; Makes my head hurt.&#160; </p>

<p>Iterator - Another convenient noun.&#160; Iterate away!&#160; Not that you'll have to worry too much about naming classes with this pattern if you use C#, Java, C++ or pretty much any other modern language, since this pattern is built-in.</p>

<p><strong>Mediator </strong>- This pattern promotes loose coupling, most of the time I'd call what it does a &quot;Controller,&quot; or what's being Mediated is more on a service level, so perhaps the word &quot;Service&quot; would end up in the name.&#160; Can't say I've ever called a class specifically &quot;FooMediator&quot;, but I suppose I don't have a problem with it if you want to do that.</p>

<p><strong>Memento </strong>- This pattern captures and externalizes an object's state.&#160; So I think I'd call something like it &quot;State&quot; before &quot;Memento,&quot; since it's fewer letters to type.&#160; Besides, I had to see that movie twice before I really appreciated it.&#160; </p>

<p>Ok, that was just lame, sorry.</p>

<p><strong>Observer </strong>- Oh, you mean like an Event?&#160; Or more like a delegate?&#160; :)</p>

<p><strong>State </strong>- I'd call my implementation of this pattern &quot;Memento,&quot; just to confuse you.&#160; That's just how I roll.</p>

<p><strong>Strategy </strong>- This is another pattern that it seems like we use all the time, without having to give it a fancy Ivory Tower name.&#160; As someone pointed out (sorry I lost the reference to who said that) Microsoft calls all their Strategy implementations Providers now anyway.&#160; Rather than label something a &quot;Strategy&quot; I'd probably just say what the thing does: &quot;CryptographicProvider.&quot;</p>

<p><strong>Template Method</strong> - Yet another pattern where there's no obvious place to hang the name off of, so don't try to force it in there and impress your coworkers with how smart you are.&#160; We know, you're a Design Pattern God, no need to brag about it.</p>

<p><strong>Visitor </strong>- Hmm, not sure where I stand on this one.&#160; I have used this pattern where I've named a visitor &quot;SqlGeneratingVisitor.&quot;&#160; But maybe that was a mistake?&#160; So how about we just declare this one &quot;Free Parking,&quot; and you can do what you want.</p>

<p>&#160;</p>

<p>So, there you go.&#160; A bunch of design patterns, summarized into one neat page.&#160; Now you're ready to ace that interview!&#160; </p>

<p>&#160;</p>

<p><em>disclaimer: I make no guarantees that any or all of the information will not harm you in a technical interview, and if repeating this to an interview board gets your removed from the building by security, its not my fault.&#160; This information comes without warantees or conditions of any kind, and you probably shouldn't have bothered reading it..</em></p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/24/name-those-patterns/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.Net HierarchicalDataSource&lt;T&gt;</title>
		<link>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=aspnet-hierarchicaldatasourcet</link>
		<comments>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/#comments</comments>
		<pubDate>Sun, 06 Apr 2008 06:45:44 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Extension Methods]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/</guid>
		<description><![CDATA[The Asp.net 2.0 tree control, as well as many 3rd party controls binds to hierarchical data via an IHierarchicalDataSource interface. Asp.net provides an implementation OOTB with the HierarchicalDataSourceControl! Easy, let’s hook that up to our… wait this control only takes data in xml format or from a sitemap? Hmm, we don’t want to convert our [...]]]></description>
			<content:encoded><![CDATA[<p>The Asp.net 2.0 tree control, as well as many 3rd party controls binds to hierarchical data via an IHierarchicalDataSource interface. Asp.net provides an implementation OOTB with the HierarchicalDataSourceControl! Easy, let’s hook that up to our… wait this control only takes data in xml format or from a sitemap? Hmm, we don’t want to convert our model to xml, and a sitemap seems inappropriate. We spend a lot time doing great patterns such as MVP &amp; MVC, we should just be able to bind up a list of model objects we already have.  <p>Good thing MS has exposed a set of interfaces and abstract classes in System.Web.UI for us to implement our own IHierarchicalDataSource and HierarchicalDataSourceView and IHierarchicalEnumerable and then IHierarchyData... geesh.  <p>Data? So let's see here, in order to bind our model to the tree control it needs to implement an interface in System.Web.UI? Well that's just not going to happen, so its time for some Elegant Code.  <p><strong>Story:</strong>  <p>Dude, as a software developer, needs to create a custom implementation of IHierarchicalDataSource, and the rest of its parts, so that he can bind a list of objects to a tree control in asp.net  <ul> <li>Use existing model to bind to tree control  <li>Must be able to reuse data source for future models  <li>Model must not contain a reference to System.Web.UI </li></ul> <p>In order to setup our model to address the acceptance criteria we are going to use an interface with a self-referencing generic declaration, where the type will specify itself as the concrete type. This will let us create a nice generic hierarchical model to pass into our datasource without resorting to any sort of base class.  <div><pre class="csharpcode"><span class="kwrd">namespace</span> POC.Common
{
    <span class="kwrd">public</span> <span class="kwrd">interface</span> IModelWithHierarchy&lt;T&gt;
    {
        <span class="kwrd">string</span> Name { get; set; }
        T Parent { get; set; }
        List&lt;T&gt; Children { get; set; }
    }
}</pre><em>Lets note that we have placed the interface in a Common namespace which represents a shared.dll between Web &amp; Model</em> </div>
<p>The UI part of criteria is now a bit more tricky because the HierarchicalDataSourceView expects a IHierarchicalEnumerable datasource which contains IHierarchyData. Said IHierarchyData != IModelWithHierarchy&lt;T&gt;.&nbsp; Luckily someone else has already figured this one out for us. We need an <a href="http://www.dofactory.com/Patterns/PatternAdapter.aspx" target="_blank">adapter</a>. 
<p><a href="http://elegantcode.com/wp-content/uploads/2008/04/image7.png" target="_blank"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="123" alt="image" src="http://elegantcode.com/wp-content/uploads/2008/04/image-thumb7.png" width="244" border="0"></a> 
<p>To instantiate our HierarchyData adapter objects we will use an extension method and linq 'Select' to convert our datasource IEnumerable&lt;T&gt; of IModelWithHierarchy&lt;T&gt; 's to an IHierarchicalEnumerable of IHierarchyData. 
<div><pre class="csharpcode"><span class="kwrd">namespace</span> POC.Web.HierarchyExtensions
{
    <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">class</span> HierarchyConverter
    {
        <span class="kwrd">public</span> <span class="kwrd">static</span> HierarchicalModelList
            ToHierarchicalModelList&lt;T&gt;(<span class="kwrd">this</span> IEnumerable&lt;T&gt; modelWithHierarchy)
            <span class="kwrd">where</span> T : IModelWithHierarchy&lt;T&gt;
        {
            <span class="kwrd">return</span> <span class="kwrd">new</span> HierarchicalModelList(
                modelWithHierarchy.Select(m =&gt; <span class="kwrd">new</span> HierarchyData&lt;T&gt;(m) <span class="kwrd">as</span> IHierarchyData));
        }
    }
}</pre></div>
<p>I am not going to bore you with the implementation details of the HierarachyData&lt;T&gt; adapter and the .net abstractions, but have dropped the <a href="http://elegantcode.com/wp-content/uploads/2008/04/hierarchydatasourcepoc.zip" target="_blank">source code here</a> if your interested. The end result: 
<div><pre class="csharpcode">TreeView1.DataSource = <span class="kwrd">new</span> HierarchicalModelDataSource&lt;Category&gt; { DataSource = Categories };
TreeView1.DataBind();
TreeView1.CollapseAll();</pre></div>
<p>Story complete!&nbsp; </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is Elegant Code to me?</title>
		<link>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-is-elegant-code-to-me</link>
		<comments>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/#comments</comments>
		<pubDate>Mon, 31 Mar 2008 03:47:45 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Patterns and Practices]]></category>
		<category><![CDATA[Widgets of Wisdom]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/</guid>
		<description><![CDATA[This question keeps popping up around here ("around here" being the loose conglomeration that makes up the Elegant Code group).&#160; It isn't easy to describe.&#160; And really, the notion of what constitutes elegance in code changes over time.&#160; There is no static "this is good code" test, and I doubt there ever will be.&#160; Plus, [...]]]></description>
			<content:encoded><![CDATA[<p>This question keeps popping up around here ("around here" being the loose conglomeration that makes up the Elegant Code group).&nbsp; It isn't easy to describe.&nbsp; And really, the notion of what constitutes elegance in code changes over time.&nbsp; There is no static "this is good code" test, and I doubt there ever will be.&nbsp; Plus, what makes good code in one language, may not apply to the next.</p> <p>So let me state for the record: today's elegant code is tomorrow's drivel.&nbsp; Don't feel bad, many writers have the same problems.&nbsp; What was super amazing back in the day is now rubbish or near unreadable.&nbsp; I am thinking of the Victorian writing that Hemingway usurped, and now Hemingway himself is almost unreadable (to me anyway).&nbsp; Tastes change with the times.&nbsp; That is a simple fact.</p> <p>So what can you do about this?&nbsp; As I say that old writing sucks to read (read Washington Irvine lately?), great works of literature still abound (for instance, Hemingway is still a great writer -- even if I prefer Tolkien) .&nbsp; So take some notes from them, and from other crafts in looking for the answer.&nbsp; If you want a cliff notes version of what this is going to tell you, it is this: you must always push yourself to get better.</p> <ol> <li>Find a good teacher.&nbsp; Nothing is better than sitting at the feet of a master who can nudge you along in the right direction.&nbsp; While early mistakes can often be corrected easily with a little bit of guidance, after they have had some time to fester all bets are off.&nbsp; I believe a fare number of "Anit-patterns" were created by self taught developers (find <a href="http://elegantcode.com/2008/03/21/sql-ejaculation/">SQL Ejaculation</a> on this site).&nbsp; <li>Read.&nbsp;&nbsp; Good writer are first good readers.&nbsp; Start with Code Complete, move into a good Patterns book, get MSDN Magazine, find some bloggers your like, but keep moving.&nbsp; You will NEVER be done reading.&nbsp; I imagine that even Martin Fowler has a "to read" booklist a mile long.  <li>Read code.&nbsp;&nbsp; There are a plethora of open source project just waiting to be read -- do so.&nbsp; This is the single best way to expand your coding repertoire, find things your language can do that you didn't even know about..&nbsp;&nbsp; Scott Hanselman has recently popularized this idea, and I thank him for it.  <li>Practice.&nbsp; This means writing small applications at home.&nbsp; You get an idea that you want to try out -- do it.&nbsp; I don't mean you have to write finished applications, just have some exploring time.&nbsp; I remember talking with an 80 year old master woodworker (he lives down the street from me), who was telling me how many time he would practice making dovetail joints before he felt competent.&nbsp; It was years worth.  <li>Pay Attention.&nbsp; Being good at anything really amounts to that.&nbsp; And don't just pay attention when reading, writing, or talking about code.&nbsp; Inspiration come from everywhere, any good artist will tell you that.&nbsp; Pay attention when you cook, when you work on the car, when you are wood working, playing an instrument, whatever it is that you do.&nbsp; This will help you in the end, event if it is just learning the amount of dedication it really takes to become good at something.  <li>Beware of preferences.&nbsp; Any time I hear someone start a statement with "I prefer code to ..." you know things are going downhill.&nbsp; If you find someone who cares more about how your code is formatted then how it is written you have found yourself in a mess.&nbsp; More importantly, beware of them in yourself.&nbsp;&nbsp; Having code style standard is important, but it isn't worth loosing sleep over.&nbsp; This also pertains to inheritance, patterns, use of particular classes (e.g. always using the generic list class in C# when a Dictionary would be better).&nbsp; <li>No Sacred Cows. Believe no single source of information.&nbsp; This means not thinking that Microsoft, Sun, IBM, Richard Stallman, or anyone else will have all of the correct answers.&nbsp; Apply the scientific method and do some research, experiment, and question the authority.&nbsp;&nbsp; There should be no sacred cows in programming.  <li>Talk with others.&nbsp; Join a user group, show up every now and then, heckle the presenter, and -maybe- speak.&nbsp; You want a broad range of people who you can talk to and bounce ideas off of.&nbsp; This will help you from becoming that crazy guy in the corner who vehemently states that all your code should be in one file.&nbsp; Having a mentor (mentioned earlier) is great, but having people around to push you from multiple directions is also good.  <li>Learn languages.&nbsp; This gets back to the "read code" idea. Make that multiple types of languages as well.&nbsp; If you know C# or Java and SQL, learn Python or Ruby, get really good at JavaScript.&nbsp; If you want something really out there, learn MDX.&nbsp; This is also a repertoire thing.&nbsp; Seeing how other languages work will make you rethink your ideas about what your current code, in whatever languages you are working in, should look like.  <li>Keep Thinking!!!&nbsp; That is possibly the most important point.&nbsp; Keep thinking.&nbsp; Don't just evaluate something once and never return, go back and reevaluate. Did the technique work?&nbsp; How could it have been better?&nbsp;&nbsp; The idea is continual improvement.  <li>Switch jobs every now and then.&nbsp; When I announced I was leaving my first programming job out of college, the VP of R&amp;D had me sit down with him and talk.&nbsp; He wasn't trying to keep me (he knew I was moving to be closer to family), he wanted to give me some advice.&nbsp; He told me to change jobs every three years.&nbsp; After you have been with a place for three years you have probably learned everything you are going to learn and it time to move on.&nbsp; This doesn't mean changing companies either.&nbsp; I've been with the same company now for four years, but I've had three different jobs.&nbsp; If you have been writing commercial applications, become a consultant - or vise-versa, become a test engineer, expand your boundaries.&nbsp; Again, this is about pushing yourself to be better.  <li>Have a Hobby. But don't ask about me, I have too many.&nbsp;&nbsp; Creating software is about creating things.&nbsp; So I suggest picking a hobby that involves creating something.&nbsp; Popular hobbies amongst programmers I know include: cooking, music, woodworking, beer making, and BBQ (which is different than cooking).&nbsp; But don't discount sports (golf is always popular), computer games, and board games.&nbsp; Anything that promotes the development of skill levels can't be a bad thing. </li></ol> <p>And I'm stopping there.&nbsp; This is my beginner's guide to how to become the writer elegant code.&nbsp; If you have others you think I missed, add them too the comments.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>LINQ Framework Design Guidelines</title>
		<link>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=linq-framework-design-guidelines-2</link>
		<comments>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/#comments</comments>
		<pubDate>Wed, 19 Mar 2008 01:53:22 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[Microsoft]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/</guid>
		<description><![CDATA[Excellent article on design considerations for LINQ. Looks like these concepts will be incorporated into the 2nd Edition of Framework Design Guidelines: Conventions, Idioms, and Patterns for Reuseable .NET Libraries. Cant wait to get my hands on a copy of that!]]></description>
			<content:encoded><![CDATA[<p>Excellent article on <a href="http://blogs.msdn.com/mirceat/archive/2008/03/13/linq-framework-design-guidelines.aspx" target="_blank">design considerations for LINQ</a>. Looks like these concepts will be incorporated into the 2nd Edition of <a href="http://www.amazon.com/Framework-Design-Guidelines-Conventions-Development/dp/0321545613/ref=pd_bbs_sr_2?ie=UTF8&amp;s=books&amp;qid=1200508590&amp;sr=1-2" target="_blank">Framework Design Guidelines: Conventions, Idioms, and Patterns for Reuseable .NET Libraries</a>. Cant wait to get my hands on a copy of that!</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Boise Code Camp 2008</title>
		<link>http://elegantcode.com/2008/03/09/boise-code-camp-2008/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=boise-code-camp-2008</link>
		<comments>http://elegantcode.com/2008/03/09/boise-code-camp-2008/#comments</comments>
		<pubDate>Mon, 10 Mar 2008 02:25:35 +0000</pubDate>
		<dc:creator>Alex Mueller</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Code Camp]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Patterns and Practices]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/09/boise-code-camp-2008/</guid>
		<description><![CDATA[Boise Code Camp 2008 was awesome. Thank you to David Starr and his wife, Eleanor, for taking ownership of this event and dedicating many months of their time to make this a reality. Thank you to the presenters for having the passion and desire to present technologies and practices, both new and not-as-new. Thank you [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Boise Code Camp 2008" href="http://www.boisecodecamp.org/" target="_blank">Boise Code Camp 2008</a> was awesome. Thank you to <a title="David Starr" href="http://elegantcode.com/about/david-starr/" target="_blank">David Starr</a> and his wife, <a title="Domestic Oblivion" href="http://domesticoblivion.com/" target="_blank">Eleanor</a>, for taking ownership of this event and dedicating many months of their time to make this a reality. Thank you to the presenters for having the passion and desire to present technologies and practices, both new and not-as-new. Thank you to the many volunteers who put in countless hours to help coordinate the weekend's events. Thank you to the campers for attending and making this year's code camp a success.</p>  <p>I am sure everyone will be blogging about all the great sessions they attended at code camp. I enjoyed all of the presentations I attended, and I am excited to explore some new material. </p>  <p>I want to share a few thoughts on my presentation dealing with Model-View-Presenter in ASP.NET after listening to <a title="Scott Hanselman" href="http://www.hanselman.com" target="_blank">Scott Hanselman's</a> session on the <a title="ASP. NET MVC Project" href="http://www.asp.net/mvc" target="_blank">ASP.NET MVC Project</a>. I may have the percentage incorrect, but Scott mentioned something like it is predicted that only 10% of the ASP.NET community currently using Web Forms will adopt and use the MVC framework. The MVC framework is an addition to ASP.NET, it is not a replacement. What this means is that Model-View-Presenter will still be a viable pattern to implement with your ASP.NET applications, and it is not going away. ASP.NET Web Forms will not be going away either.</p>  <p>Use MVP to get your third party controls under test. Use MVP to provide that separation of concerns in your legacy applications. Use it entirely or in conjunction with the MVC framework. It is all about testability. <a title="Glenn Block" href="http://blogs.msdn.com/gblock/" target="_blank">Glenn Block</a> presented on the Web Client Software Factory, and what pattern does this implement? Model-View-Presenter.</p>  <p>I am providing my <a title="Presentation and Source Code" href="http://muellerdesigns.net/DasBlog/content/binary/ModeViewPresenter.AlexMueller.BoiseCodeCamp2008.rar" target="_blank">presentation and source code (3.67 MB)</a> from my talk on MVP. It will be available via the <a title="Boise Code Camp" href="http://www.boisecodecamp.org/" target="_blank">Boise Code Camp</a> site as well. </p>  <p>Again, thank the many individuals and their families who sacrificed their time to bring to the local community this years code camp.</p>  <p>Around the office's water cooler on Monday, I will be able to say, &quot;and this one time, at code camp...&quot;</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/09/boise-code-camp-2008/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Microsoft releases Unity, an IoC library</title>
		<link>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=where-to-start-a-new-program</link>
		<comments>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/#comments</comments>
		<pubDate>Sun, 08 Jun 2008 19:50:39 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/06/08/where-to-start-a-new-program/</guid>
		<description><![CDATA[Here as an interesting question that popped up at my user group meeting last night, when you are starting a new program (green field development), where do you get started?&#160; I find these questions interesting because there really is no correct answer, but a persons answer will tell you something about how they problem solve. [...]]]></description>
			<content:encoded><![CDATA[<p>Here as an interesting question that popped up at my user group meeting last night, when you are starting a new program (green field development), where do you get started?&nbsp; I find these questions interesting because there really is no correct answer, but a persons answer will tell you something about how they problem solve.</p> <p>Depending on who you ask you will likely get one of two answers.&nbsp; If you are talking with a data guy, he will say to start with the database.&nbsp; If you are dealing with a visual guy, he will say to start with the UI.&nbsp; Historically, I did a conglomeration of those two ideas, creating both the UI and the database at the same time, letting the two drive each other.&nbsp; But I'm weird, I'm a visual/data guy.</p> <p>Lately though, I've started to thing about a third approach.&nbsp; Starting in the middle.&nbsp; No UI, no database, just the data classes.&nbsp; This approach has been spurred on buy a couple of things.&nbsp; Namely, test driven development (TDD), design patterns, and domain driven design.</p> <p>TDD really is the glue that allows all of this to happen.&nbsp; I can start in the middle because I can run the code in the middle any time I want.&nbsp; I just have to write a test.&nbsp; No having to wade through multiple layers of UI to test out some small chunk ok UI is a great productivity gain for me.</p> <p>Next is design patterns.&nbsp; This is what gives you a reference for how to approach your code.&nbsp; That said, this isn't about making everything absolutely perfect from the get go, but it is nice to know where you are going to made your comprises early on.</p> <p>But all of that does not explain where I begin.&nbsp; That is where Domain Driven Design (DDD) kicks in.&nbsp; For those not familiar with the concept, you can read up on <a href="http://en.wikipedia.org/wiki/Domain-driven_design">wikipedia</a>, buy <a href="http://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1212806974&amp;sr=8-1">The Book</a>, or buy one of the <a href="http://www.amazon.com/s?ie=UTF8&amp;tag=mozilla-20&amp;index=blended&amp;link%5Fcode=qs&amp;field-keywords=domain%20driven%20design&amp;sourceid=Mozilla-search">various other references</a>.&nbsp;&nbsp; One of the concepts in DDD (I'm still reading the book) is setting up something called a <a href="http://www.domaindrivendesign.org/discussion/messageboardarchive/UbiquitousLanguage.html">ubiquitous language</a>.</p> <p>The ubiquitous language is actually trying to solve a problem that I have seen happen on many projects I've been apart of (but not always my fault).&nbsp; At some point there is a linguistic difference between the nouns that the customer uses and the nouns that the programmers use.&nbsp; By focusing on the language first, you are attempting to head that off at the pass.</p> <p>These nouns consist of your domain classes and your tables and columns, they move everywhere...but not always consistently.&nbsp; Consistency does matter.&nbsp; Inconsistency is what leads to rather confusing conversations with people and everyone scratching their heads about "what just happened".&nbsp; So, this is the middle that I start with.</p> <p>But before you start...</p> <p>One of the other guys in the group was an actual data guy.&nbsp; The systems he has written worked with more data than yours does.&nbsp; Trust me on that.&nbsp; Historically, his biggest consideration was data input speed.&nbsp; I don't think I would start in the middle if that was my biggest concern.&nbsp; In that case you do start with the database, you tune for speed.&nbsp; You also don't use as many domain classes, and you tend to throw away your ORM solution and just hit the database layer directly via Ado.Net.</p> <p>But lets face it, that doesn't happen very often.&nbsp; Most business applications aren't going to touch what the architecture can take.&nbsp; It is amazing how many horribly written applications with very inefficient code run just fine.&nbsp; Most of my speed issues could be solved by indexing tables and changing generic lists to generic dictionaries (more on that later).&nbsp; Architect your applications for how they are going to be used.&nbsp; Most of my web applications have a max usage of 100 simultaneous users.&nbsp; If the data access is slightly slower I'll take that hit for programmer efficiency.&nbsp; If you are designing Amazon.com, you code for through-put.</p> <p>OK, someone is going to disagree with this...I can already feel it.&nbsp; Please take a moment to register your displeasure in the comments.&nbsp; I would love to hear it.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; Design Patterns</title>
	<atom:link href="http://elegantcode.com/tag/design-patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Where to start a new program</title>
		<link>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=where-to-start-a-new-program</link>
		<comments>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/#comments</comments>
		<pubDate>Sun, 08 Jun 2008 19:50:39 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/06/08/where-to-start-a-new-program/</guid>
		<description><![CDATA[Here as an interesting question that popped up at my user group meeting last night, when you are starting a new program (green field development), where do you get started?&#160; I find these questions interesting because there really is no correct answer, but a persons answer will tell you something about how they problem solve. [...]]]></description>
			<content:encoded><![CDATA[<p>Here as an interesting question that popped up at my user group meeting last night, when you are starting a new program (green field development), where do you get started?&nbsp; I find these questions interesting because there really is no correct answer, but a persons answer will tell you something about how they problem solve.</p> <p>Depending on who you ask you will likely get one of two answers.&nbsp; If you are talking with a data guy, he will say to start with the database.&nbsp; If you are dealing with a visual guy, he will say to start with the UI.&nbsp; Historically, I did a conglomeration of those two ideas, creating both the UI and the database at the same time, letting the two drive each other.&nbsp; But I'm weird, I'm a visual/data guy.</p> <p>Lately though, I've started to thing about a third approach.&nbsp; Starting in the middle.&nbsp; No UI, no database, just the data classes.&nbsp; This approach has been spurred on buy a couple of things.&nbsp; Namely, test driven development (TDD), design patterns, and domain driven design.</p> <p>TDD really is the glue that allows all of this to happen.&nbsp; I can start in the middle because I can run the code in the middle any time I want.&nbsp; I just have to write a test.&nbsp; No having to wade through multiple layers of UI to test out some small chunk ok UI is a great productivity gain for me.</p> <p>Next is design patterns.&nbsp; This is what gives you a reference for how to approach your code.&nbsp; That said, this isn't about making everything absolutely perfect from the get go, but it is nice to know where you are going to made your comprises early on.</p> <p>But all of that does not explain where I begin.&nbsp; That is where Domain Driven Design (DDD) kicks in.&nbsp; For those not familiar with the concept, you can read up on <a href="http://en.wikipedia.org/wiki/Domain-driven_design">wikipedia</a>, buy <a href="http://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1212806974&amp;sr=8-1">The Book</a>, or buy one of the <a href="http://www.amazon.com/s?ie=UTF8&amp;tag=mozilla-20&amp;index=blended&amp;link%5Fcode=qs&amp;field-keywords=domain%20driven%20design&amp;sourceid=Mozilla-search">various other references</a>.&nbsp;&nbsp; One of the concepts in DDD (I'm still reading the book) is setting up something called a <a href="http://www.domaindrivendesign.org/discussion/messageboardarchive/UbiquitousLanguage.html">ubiquitous language</a>.</p> <p>The ubiquitous language is actually trying to solve a problem that I have seen happen on many projects I've been apart of (but not always my fault).&nbsp; At some point there is a linguistic difference between the nouns that the customer uses and the nouns that the programmers use.&nbsp; By focusing on the language first, you are attempting to head that off at the pass.</p> <p>These nouns consist of your domain classes and your tables and columns, they move everywhere...but not always consistently.&nbsp; Consistency does matter.&nbsp; Inconsistency is what leads to rather confusing conversations with people and everyone scratching their heads about "what just happened".&nbsp; So, this is the middle that I start with.</p> <p>But before you start...</p> <p>One of the other guys in the group was an actual data guy.&nbsp; The systems he has written worked with more data than yours does.&nbsp; Trust me on that.&nbsp; Historically, his biggest consideration was data input speed.&nbsp; I don't think I would start in the middle if that was my biggest concern.&nbsp; In that case you do start with the database, you tune for speed.&nbsp; You also don't use as many domain classes, and you tend to throw away your ORM solution and just hit the database layer directly via Ado.Net.</p> <p>But lets face it, that doesn't happen very often.&nbsp; Most business applications aren't going to touch what the architecture can take.&nbsp; It is amazing how many horribly written applications with very inefficient code run just fine.&nbsp; Most of my speed issues could be solved by indexing tables and changing generic lists to generic dictionaries (more on that later).&nbsp; Architect your applications for how they are going to be used.&nbsp; Most of my web applications have a max usage of 100 simultaneous users.&nbsp; If the data access is slightly slower I'll take that hit for programmer efficiency.&nbsp; If you are designing Amazon.com, you code for through-put.</p> <p>OK, someone is going to disagree with this...I can already feel it.&nbsp; Please take a moment to register your displeasure in the comments.&nbsp; I would love to hear it.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Learning Design Patterns</title>
		<link>http://elegantcode.com/2008/04/28/learning-design-patterns/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=learning-design-patterns</link>
		<comments>http://elegantcode.com/2008/04/28/learning-design-patterns/#comments</comments>
		<pubDate>Mon, 28 Apr 2008 19:01:17 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/28/learning-design-patterns/</guid>
		<description><![CDATA[To me this seems like an interesting way to learn about design patterns (watch the video). I wonder if it also works when I hit an ivory-tower architect with POEAA or DDD? I'll keep you posted. PS: I'm really looking forward to the next episode on code reviews and retrospectives.]]></description>
			<content:encoded><![CDATA[To me <a href="http://codebetter.com/blogs/glenn.block/archive/2008/04/28/extreme-patterns-with-oren.aspx" target="_blank">this</a> seems like an interesting way to learn about design patterns (watch the video). I wonder if it also works when I hit an ivory-tower architect with <a href="http://www.bookpool.com/sm/0321127420" target="_blank">POEAA</a> or <a href="http://www.bookpool.com/sm/0321125215" target="_blank">DDD</a>? I'll keep you posted.

PS: I'm really looking forward to the next episode on code reviews and retrospectives.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/28/learning-design-patterns/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>DTO&#8217;s or Serialized Domain Entities?</title>
		<link>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=dtos-or-serialized-domain-entities</link>
		<comments>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/#comments</comments>
		<pubDate>Sun, 27 Apr 2008 08:09:02 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/</guid>
		<description><![CDATA[I hear this question come up time and time again. It is something that I have been dealing with for a while now, and have some experience with doing both ways. I started writing this in response to a DDD thread, but it got rather large and so I turned it into a post. So [...]]]></description>
			<content:encoded><![CDATA[<p>I hear this question come up time and time again. It is something that I have been dealing with for a while now, and have some experience with doing both ways. I started writing this in response to a <a href="http://tech.groups.yahoo.com/group/domaindrivendesign/message/7286" target="_blank">DDD thread</a>, but it got rather large and so I turned it into a post. So here ya go!  <p>I think you have to write DTO's for any sort of long term Enterprise 'system of systems' solution. I fought this for some time, but since moving to an Assembler/DTO I much enjoy the freedom of being able to easily tweak my DTO "snapshot" of the aggregate root I am working with. Maintaining them is very little overhead.  <p>I tend to think of the DTO as a message, a value object. This is necessary because once an entity leaves the boundary it can no longer function within its original meaning. Its methods may be intended to only run within a given context, which is not the same once on another tier. (Things like lazy loading are a good example) Once the DTO/Message arrives to the caller, it very well may (and probably should) be translated/assembled into another model which can represent its new semantics.  <p>Some other advantages of DTO w/ Assembler or Builder:<br>-Can explicitly control the depth of the graph/tree you are serializing.<br>-Can flatten out the data where needed<br>-Can return significantly less data (maybe you only need 3 fields instead of 15)<br>-Not tightly coupled between remote boundaries (this could be a mess when working with a large team)<br>-Interoperable  <p>I wrote a post a while back showing a way to '<a href="http://elegantcode.com/2007/12/05/fun-with-generics-from-repository-to-dto/" target="_blank">templatize</a>' the process. My current implementations are much different with LINQ now, but maybe it could stimulate some ideas for whomever.  <p><em>The next thing I would like to do is remove the assembler altogether. By flagging fields with attributes or using config to specify a mapping, I could then feed <a href="http://blogs.msdn.com/meek/archive/2008/04/25/using-linq-expressions-to-generate-dynamic-methods.aspx" target="_blank">dynamic methods</a> and run a translation/builder strategy.</em>  <p>I know there are concerns about writing and maintaining assembler and DTO, but to me this is the least of the worry, a more challenging scenario would be the UnitOfWork (How to you intended to track remote state, concurrency etc) How about versioning? performance? scala.. well ilities in general?  <p>These kinds of problems lead one from Request/Response, Patterns of Enterprise Architecture &amp; DDD to a Pub/Sub Message Bus - Enterprise Integration Patterns &amp; DDDD.  <p>And to think all you want to do is send some frickin entities to a remote client&nbsp; :)</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Test Data Builders Refined</title>
		<link>http://elegantcode.com/2008/04/26/test-data-builders-refined/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=test-data-builders-refined</link>
		<comments>http://elegantcode.com/2008/04/26/test-data-builders-refined/#comments</comments>
		<pubDate>Sat, 26 Apr 2008 23:08:27 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/26/test-data-builders-refined/</guid>
		<description><![CDATA[Last year, I blogged about Test Data Builders here and here. I still use them heavily in my unit tests for creating objects with test data. Heck, I also use this pattern for fluent interfaces in production code. Here is a simple example of this approach: &#160; 1: public class CustomerBuilder 2: { 3: public [...]]]></description>
			<content:encoded><![CDATA[<p>Last year, I blogged about Test Data Builders <a href="http://vanryswyckjan.blogspot.com/2007/09/test-data-builder-versus-object-mother.html" target="_blank">here</a> and <a href="http://vanryswyckjan.blogspot.com/2007/12/test-data-builder.html" target="_blank">here</a>. I still use them heavily in my unit tests for creating objects with test data. Heck, I also use this pattern for fluent interfaces in production code. Here is a simple example of this approach:</p>  <p>&#160;</p>  <div>   <div class="csharpcode">     <pre class="alteven"><span class="lnum">   1:</span> <span class="kwrd">public</span> <span class="kwrd">class</span> CustomerBuilder</pre>

    <pre class="alteven"><span class="lnum">   2:</span> {</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     <span class="kwrd">public</span> String _firstName = <span class="str">&quot;Homer&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     <span class="kwrd">public</span> String _lastName = <span class="str">&quot;Simpson&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   5:</span>         </pre>

    <pre class="alteven"><span class="lnum">   6:</span>     <span class="kwrd">public</span> CustomerBuilder WithFirstName(String firstName)</pre>

    <pre class="alteven"><span class="lnum">   7:</span>     {</pre>

    <pre class="alteven"><span class="lnum">   8:</span>         _firstName = firstName;</pre>

    <pre class="alteven"><span class="lnum">   9:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;    </pre>

    <pre class="alteven"><span class="lnum">  10:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  11:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  12:</span>     <span class="kwrd">public</span> CustomerBuilder WithLastName(String lastName)</pre>

    <pre class="alteven"><span class="lnum">  13:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  14:</span>         _lastName = lastName;</pre>

    <pre class="alteven"><span class="lnum">  15:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven"><span class="lnum">  16:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  17:</span>         </pre>

    <pre class="alteven"><span class="lnum">  18:</span>     <span class="kwrd">public</span> Customer Build()</pre>

    <pre class="alteven"><span class="lnum">  19:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  20:</span>         <span class="kwrd">return</span> <span class="kwrd">new</span> Customer(_firstName, _lastName);    </pre>

    <pre class="alteven"><span class="lnum">  21:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  22:</span> }</pre>
  </div>
</div>

<pre class="alteven">This fluent builder class can then be used this way:</pre>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> Customer customer = <span class="kwrd">new</span> CustomerBuilder()</pre>

    <pre class="alteven"><span class="lnum">   2:</span>     .WithFirstName(<span class="str">&quot;Homer&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     .WithLastName(<span class="str">&quot;Simpson&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     .Build();</pre>
  </div>
</div>

<p>A while ago, <a href="http://codebetter.com/blogs/gregyoung/default.aspx" target="_blank">Greg Young</a> started a series of blog posts on DDDD (Distributed Domain-Driven Design), which I can highly recommend. Make sure to catch up now you still can because I think that he has a lot of stuff coming up, which I'm really looking forward to.</p>

<p>Anyhow, Greg had a couple of posts on fluent builders, which you can read <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/15/dddd-5-messages-have-fluent-builders.aspx" target="_blank">here</a>, <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/16/dddd-6-fluent-builders-alternate-ending.aspx" target="_blank">here</a> and <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/22/dddd-8-fluent-builders-and-tests.aspx" target="_blank">here</a>. I noticed an interesting approach in the way that the target object is built. Here is an example of this approach:</p>

<p>&#160;</p>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> <span class="kwrd">public</span> <span class="kwrd">class</span> CustomerBuilder</pre>

    <pre class="alteven"><span class="lnum">   2:</span> {</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     <span class="kwrd">public</span> String _firstName = <span class="str">&quot;Homer&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     <span class="kwrd">public</span> String _lastName = <span class="str">&quot;Simpson&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   5:</span>         </pre>

    <pre class="alteven"><span class="lnum">   6:</span>     <span class="kwrd">public</span> CustomerBuilder WithFirstName(String firstName)</pre>

    <pre class="alteven"><span class="lnum">   7:</span>     {</pre>

    <pre class="alteven"><span class="lnum">   8:</span>         _firstName = firstName;</pre>

    <pre class="alteven"><span class="lnum">   9:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;    </pre>

    <pre class="alteven"><span class="lnum">  10:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  11:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  12:</span>     <span class="kwrd">public</span> CustomerBuilder WithLastName(String lastName)</pre>

    <pre class="alteven"><span class="lnum">  13:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  14:</span>         _lastName = lastName;</pre>

    <pre class="alteven"><span class="lnum">  15:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven"><span class="lnum">  16:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  17:</span>         </pre>

    <pre class="alteven"><span class="lnum">  18:</span>     <span class="kwrd">public</span> Customer Build()</pre>

    <pre class="alteven"><span class="lnum">  19:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  20:</span>         <span class="kwrd">return</span> <span class="kwrd">new</span> Customer(_firstName, _lastName);    </pre>

    <pre class="alteven"><span class="lnum">  21:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  22:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  23:</span>     <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">implicit</span> <span class="kwrd">operator</span> Customer(</pre>

    <pre class="alteven"><span class="lnum">  24:</span>         CustomerBuilder builder) </pre>

    <pre class="alteven"><span class="lnum">  25:</span>     {  </pre>

    <pre class="alteven"><span class="lnum">  26:</span>         <span class="kwrd">return</span> builder.Build()    </pre>

    <pre class="alteven"><span class="lnum">  27:</span>     } </pre>

    <pre class="alteven"><span class="lnum">  28:</span> }</pre>
  </div>
</div>

<p>which results in the following usage:</p>

<p>&#160;</p>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> Customer customer = <span class="kwrd">new</span> CustomerBuilder()</pre>

    <pre class="alteven"><span class="lnum">   2:</span>     .WithFirstName(<span class="str">&quot;Homer&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     .WithLastName(<span class="str">&quot;Simpson&quot;</span>);</pre>
  </div>

  <br />Adding an implicit cast operator to the builder class makes that its no longer required to explicitly call the <em>Build</em> method. I keep the <em>Build</em> method around for backwards-compatibility reasons or in case I ever need it again (violating YAGNI in the process, I know, I know). I find that adding the implicit cast operator adds to the readability of the fluent interface, don't you agree?</div>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/26/test-data-builders-refined/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Name Those Patterns</title>
		<link>http://elegantcode.com/2008/04/24/name-those-patterns/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=name-those-patterns</link>
		<comments>http://elegantcode.com/2008/04/24/name-those-patterns/#comments</comments>
		<pubDate>Thu, 24 Apr 2008 16:16:13 +0000</pubDate>
		<dc:creator>Tony Rasa</dc:creator>
				<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/24/name-those-patterns/</guid>
		<description><![CDATA[In yesterday's discussion of Gang of Four Design Patterns, I said &#34;Never name your classes after the pattern itself.&#34;&#160; But then someone pointed out &#34;What about Factory classes?&#34;&#160; &#34;Oh, ok, ALWAYS name classes after the pattern.&#34;&#160; Except for Singleton or Fa&#231;ade, and maybe a few others.&#160; Ok then, NEVER name after the pattern, except when [...]]]></description>
			<content:encoded><![CDATA[<p>In yesterday's discussion of Gang of Four Design Patterns, I said &quot;Never name your classes after the pattern itself.&quot;&#160; But then someone pointed out &quot;What about Factory classes?&quot;&#160; &quot;Oh, ok, ALWAYS name classes after the pattern.&quot;&#160; Except for Singleton or Fa&#231;ade, and maybe a few others.&#160; Ok then, NEVER name after the pattern, except when you do, sometimes, and then ALWAYS name those, unless otherwise noted.</p>  <p>So, here's a list of the 23 Gang of Four patterns, along with when I'd use the name of the pattern in the implementation.&#160; Unless I didn't want to, and then I wouldn't.&#160; Feel free to agree with this list, unless you don't care to.</p>  <h3>Creational Patterns</h3>  <p><strong>Abstract Factory</strong> - After consideration, I have to say that I call my factories Factory, unless they're Repositories, Data Access Layers, DAOs, or something else.&#160; But most of the time, Factory.&#160; Because this would just be wrong:</p>  <pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> FooCreatorMakerThingy { ... }</pre>

<p><strong>Builder</strong> - Hmm, another noun. Guess I'd use this one too, for the same reason as Abstract Factory. Most of the time.&#160; </p>

<p><strong>Factory Method</strong> - I probably would not call my Factory Methods &quot;GetFooFactoryMethod()&quot; because that just looks weird.&#160;&#160; Maybe if we verbify the noun:</p>

<pre class="csharpcode"><span class="kwrd">public</span> Foo FactorialMethodizeFoo() {...}</pre>

<p>Nope, that's still dumb.</p>

<p><strong>Prototype</strong> - &quot;Specify the kinds of objects to create using a prototypical instance...&quot; So we don't care that the class is a &quot;Prototype,&quot; but we do care about the &quot;kind of object&quot; part.&#160; So don't name your prototypical classes &quot;FooPrototype.&quot;&#160; (Unless you just want to.)</p>

<p><strong>Singleton</strong> - Somebody have a case where it makes sense to name your Singleton &quot;Singleton&quot;?&#160; I can't come up with one.&#160; Anyway, you shouldn't be using singletons anymore anyway.&#160; :)</p>

<h3>Structural Patterns</h3>

<p><strong>Adapter</strong> - Adapters are pretty much called Adapters.&#160; Except when they're called Wrappers.&#160; So you can use this name too.</p>

<p><strong>Bridge</strong> - I wouldn't label a participant in a bridge pattern &quot;FooBridge,&quot; unless I was trying to impress on an interview test or something.&#160; And even then, that's a bad idea.&#160; &quot;Ohh, look, I know a complicated design pattern!&#160; Lookitme!&quot;&#160; So don't call your bridges Bridges.&#160; </p>

<p><strong>Composite</strong> - &quot;One of the goals of the Composite&#160; pattern is to make clients unaware of the specific Leaf or Composite classes they're using.&quot;&#160; So you don't want to point out that Composites are in play, that breaks the suspension of disbelief for your audience.&#160; Or something.&#160; Anyway Composites get used all over the place, if we had to label them all then it'd be FooComposite, BarComposite, TreeComposite, EverythingComposite, and what's the fun in that.</p>

<p><strong>Decorator </strong>- I think in general people don't label their Decorators Decorators.&#160; I'm sure there's a counter-example to this, but all of the Stream- and Xml-handling classes I can think of don't point out that they are Decorators.&#160; You'd probably want to stick to more descriptive names, like ASCIIStreamReader instead of cluttering things up with ASCIIStreamDecorator.&#160; We're not decorating the stream (in a literal sense), we're reading it.&#160; </p>

<p><strong>Fa&#231;ade</strong> - Don't call your Fa&#231;ade &quot;FooFa&#231;ade,&quot; that's just silly.&#160; Besides, its too much work to type that cedille into the editor if you're using an ignorant American keyboard.</p>

<p><strong>Flyweight </strong>- This is another pattern where we're going to disguise what's going on inside, in order to simplify the client's usage and make better use of resources.&#160; No reason to flaunt to the client that we're low on resources, because that's a personal and private matter - times are tough all over, and there's no shame in struggling a little bit, and hey who could have predicted that the sub-prime loan market was going to ... anyway, back to patterns.&#160;&#160; Don't call your Flyweights Flyweights, unless what you're modeling is actually a Flyweight, and then thats OK.</p>

<p><strong>Proxy </strong>- This one is standard terminology.&#160; So Proxy's ok.</p>

<h3>Behavioral Patterns (or &quot;<em>MAN this book has a lot of pages in it..</em>&quot;)</h3>

<p><strong>Chain of Responsibility </strong>- Don't even think of naming a class ChainOfResponsibility.&#160; Even with ReSharper, who'd want to have to type that?</p>

<p><strong>Command</strong> - Another convenient noun.&#160; Ok, you can create Commands.&#160; </p>

<p><strong>Interpreter </strong>- This is another pattern that doesn't have one identifying class that you could label &quot;The Interpreter.&quot;&#160; But you might create a Fa&#231;ade, and call that class Interpreter.&#160; I'm not sure if naming one pattern after another is OK or not.&#160; Makes my head hurt.&#160; </p>

<p>Iterator - Another convenient noun.&#160; Iterate away!&#160; Not that you'll have to worry too much about naming classes with this pattern if you use C#, Java, C++ or pretty much any other modern language, since this pattern is built-in.</p>

<p><strong>Mediator </strong>- This pattern promotes loose coupling, most of the time I'd call what it does a &quot;Controller,&quot; or what's being Mediated is more on a service level, so perhaps the word &quot;Service&quot; would end up in the name.&#160; Can't say I've ever called a class specifically &quot;FooMediator&quot;, but I suppose I don't have a problem with it if you want to do that.</p>

<p><strong>Memento </strong>- This pattern captures and externalizes an object's state.&#160; So I think I'd call something like it &quot;State&quot; before &quot;Memento,&quot; since it's fewer letters to type.&#160; Besides, I had to see that movie twice before I really appreciated it.&#160; </p>

<p>Ok, that was just lame, sorry.</p>

<p><strong>Observer </strong>- Oh, you mean like an Event?&#160; Or more like a delegate?&#160; :)</p>

<p><strong>State </strong>- I'd call my implementation of this pattern &quot;Memento,&quot; just to confuse you.&#160; That's just how I roll.</p>

<p><strong>Strategy </strong>- This is another pattern that it seems like we use all the time, without having to give it a fancy Ivory Tower name.&#160; As someone pointed out (sorry I lost the reference to who said that) Microsoft calls all their Strategy implementations Providers now anyway.&#160; Rather than label something a &quot;Strategy&quot; I'd probably just say what the thing does: &quot;CryptographicProvider.&quot;</p>

<p><strong>Template Method</strong> - Yet another pattern where there's no obvious place to hang the name off of, so don't try to force it in there and impress your coworkers with how smart you are.&#160; We know, you're a Design Pattern God, no need to brag about it.</p>

<p><strong>Visitor </strong>- Hmm, not sure where I stand on this one.&#160; I have used this pattern where I've named a visitor &quot;SqlGeneratingVisitor.&quot;&#160; But maybe that was a mistake?&#160; So how about we just declare this one &quot;Free Parking,&quot; and you can do what you want.</p>

<p>&#160;</p>

<p>So, there you go.&#160; A bunch of design patterns, summarized into one neat page.&#160; Now you're ready to ace that interview!&#160; </p>

<p>&#160;</p>

<p><em>disclaimer: I make no guarantees that any or all of the information will not harm you in a technical interview, and if repeating this to an interview board gets your removed from the building by security, its not my fault.&#160; This information comes without warantees or conditions of any kind, and you probably shouldn't have bothered reading it..</em></p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/24/name-those-patterns/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.Net HierarchicalDataSource&lt;T&gt;</title>
		<link>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=aspnet-hierarchicaldatasourcet</link>
		<comments>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/#comments</comments>
		<pubDate>Sun, 06 Apr 2008 06:45:44 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Extension Methods]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/</guid>
		<description><![CDATA[The Asp.net 2.0 tree control, as well as many 3rd party controls binds to hierarchical data via an IHierarchicalDataSource interface. Asp.net provides an implementation OOTB with the HierarchicalDataSourceControl! Easy, let’s hook that up to our… wait this control only takes data in xml format or from a sitemap? Hmm, we don’t want to convert our [...]]]></description>
			<content:encoded><![CDATA[<p>The Asp.net 2.0 tree control, as well as many 3rd party controls binds to hierarchical data via an IHierarchicalDataSource interface. Asp.net provides an implementation OOTB with the HierarchicalDataSourceControl! Easy, let’s hook that up to our… wait this control only takes data in xml format or from a sitemap? Hmm, we don’t want to convert our model to xml, and a sitemap seems inappropriate. We spend a lot time doing great patterns such as MVP &amp; MVC, we should just be able to bind up a list of model objects we already have.  <p>Good thing MS has exposed a set of interfaces and abstract classes in System.Web.UI for us to implement our own IHierarchicalDataSource and HierarchicalDataSourceView and IHierarchicalEnumerable and then IHierarchyData... geesh.  <p>Data? So let's see here, in order to bind our model to the tree control it needs to implement an interface in System.Web.UI? Well that's just not going to happen, so its time for some Elegant Code.  <p><strong>Story:</strong>  <p>Dude, as a software developer, needs to create a custom implementation of IHierarchicalDataSource, and the rest of its parts, so that he can bind a list of objects to a tree control in asp.net  <ul> <li>Use existing model to bind to tree control  <li>Must be able to reuse data source for future models  <li>Model must not contain a reference to System.Web.UI </li></ul> <p>In order to setup our model to address the acceptance criteria we are going to use an interface with a self-referencing generic declaration, where the type will specify itself as the concrete type. This will let us create a nice generic hierarchical model to pass into our datasource without resorting to any sort of base class.  <div><pre class="csharpcode"><span class="kwrd">namespace</span> POC.Common
{
    <span class="kwrd">public</span> <span class="kwrd">interface</span> IModelWithHierarchy&lt;T&gt;
    {
        <span class="kwrd">string</span> Name { get; set; }
        T Parent { get; set; }
        List&lt;T&gt; Children { get; set; }
    }
}</pre><em>Lets note that we have placed the interface in a Common namespace which represents a shared.dll between Web &amp; Model</em> </div>
<p>The UI part of criteria is now a bit more tricky because the HierarchicalDataSourceView expects a IHierarchicalEnumerable datasource which contains IHierarchyData. Said IHierarchyData != IModelWithHierarchy&lt;T&gt;.&nbsp; Luckily someone else has already figured this one out for us. We need an <a href="http://www.dofactory.com/Patterns/PatternAdapter.aspx" target="_blank">adapter</a>. 
<p><a href="http://elegantcode.com/wp-content/uploads/2008/04/image7.png" target="_blank"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="123" alt="image" src="http://elegantcode.com/wp-content/uploads/2008/04/image-thumb7.png" width="244" border="0"></a> 
<p>To instantiate our HierarchyData adapter objects we will use an extension method and linq 'Select' to convert our datasource IEnumerable&lt;T&gt; of IModelWithHierarchy&lt;T&gt; 's to an IHierarchicalEnumerable of IHierarchyData. 
<div><pre class="csharpcode"><span class="kwrd">namespace</span> POC.Web.HierarchyExtensions
{
    <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">class</span> HierarchyConverter
    {
        <span class="kwrd">public</span> <span class="kwrd">static</span> HierarchicalModelList
            ToHierarchicalModelList&lt;T&gt;(<span class="kwrd">this</span> IEnumerable&lt;T&gt; modelWithHierarchy)
            <span class="kwrd">where</span> T : IModelWithHierarchy&lt;T&gt;
        {
            <span class="kwrd">return</span> <span class="kwrd">new</span> HierarchicalModelList(
                modelWithHierarchy.Select(m =&gt; <span class="kwrd">new</span> HierarchyData&lt;T&gt;(m) <span class="kwrd">as</span> IHierarchyData));
        }
    }
}</pre></div>
<p>I am not going to bore you with the implementation details of the HierarachyData&lt;T&gt; adapter and the .net abstractions, but have dropped the <a href="http://elegantcode.com/wp-content/uploads/2008/04/hierarchydatasourcepoc.zip" target="_blank">source code here</a> if your interested. The end result: 
<div><pre class="csharpcode">TreeView1.DataSource = <span class="kwrd">new</span> HierarchicalModelDataSource&lt;Category&gt; { DataSource = Categories };
TreeView1.DataBind();
TreeView1.CollapseAll();</pre></div>
<p>Story complete!&nbsp; </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is Elegant Code to me?</title>
		<link>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-is-elegant-code-to-me</link>
		<comments>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/#comments</comments>
		<pubDate>Mon, 31 Mar 2008 03:47:45 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Patterns and Practices]]></category>
		<category><![CDATA[Widgets of Wisdom]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/</guid>
		<description><![CDATA[This question keeps popping up around here ("around here" being the loose conglomeration that makes up the Elegant Code group).&#160; It isn't easy to describe.&#160; And really, the notion of what constitutes elegance in code changes over time.&#160; There is no static "this is good code" test, and I doubt there ever will be.&#160; Plus, [...]]]></description>
			<content:encoded><![CDATA[<p>This question keeps popping up around here ("around here" being the loose conglomeration that makes up the Elegant Code group).&nbsp; It isn't easy to describe.&nbsp; And really, the notion of what constitutes elegance in code changes over time.&nbsp; There is no static "this is good code" test, and I doubt there ever will be.&nbsp; Plus, what makes good code in one language, may not apply to the next.</p> <p>So let me state for the record: today's elegant code is tomorrow's drivel.&nbsp; Don't feel bad, many writers have the same problems.&nbsp; What was super amazing back in the day is now rubbish or near unreadable.&nbsp; I am thinking of the Victorian writing that Hemingway usurped, and now Hemingway himself is almost unreadable (to me anyway).&nbsp; Tastes change with the times.&nbsp; That is a simple fact.</p> <p>So what can you do about this?&nbsp; As I say that old writing sucks to read (read Washington Irvine lately?), great works of literature still abound (for instance, Hemingway is still a great writer -- even if I prefer Tolkien) .&nbsp; So take some notes from them, and from other crafts in looking for the answer.&nbsp; If you want a cliff notes version of what this is going to tell you, it is this: you must always push yourself to get better.</p> <ol> <li>Find a good teacher.&nbsp; Nothing is better than sitting at the feet of a master who can nudge you along in the right direction.&nbsp; While early mistakes can often be corrected easily with a little bit of guidance, after they have had some time to fester all bets are off.&nbsp; I believe a fare number of "Anit-patterns" were created by self taught developers (find <a href="http://elegantcode.com/2008/03/21/sql-ejaculation/">SQL Ejaculation</a> on this site).&nbsp; <li>Read.&nbsp;&nbsp; Good writer are first good readers.&nbsp; Start with Code Complete, move into a good Patterns book, get MSDN Magazine, find some bloggers your like, but keep moving.&nbsp; You will NEVER be done reading.&nbsp; I imagine that even Martin Fowler has a "to read" booklist a mile long.  <li>Read code.&nbsp;&nbsp; There are a plethora of open source project just waiting to be read -- do so.&nbsp; This is the single best way to expand your coding repertoire, find things your language can do that you didn't even know about..&nbsp;&nbsp; Scott Hanselman has recently popularized this idea, and I thank him for it.  <li>Practice.&nbsp; This means writing small applications at home.&nbsp; You get an idea that you want to try out -- do it.&nbsp; I don't mean you have to write finished applications, just have some exploring time.&nbsp; I remember talking with an 80 year old master woodworker (he lives down the street from me), who was telling me how many time he would practice making dovetail joints before he felt competent.&nbsp; It was years worth.  <li>Pay Attention.&nbsp; Being good at anything really amounts to that.&nbsp; And don't just pay attention when reading, writing, or talking about code.&nbsp; Inspiration come from everywhere, any good artist will tell you that.&nbsp; Pay attention when you cook, when you work on the car, when you are wood working, playing an instrument, whatever it is that you do.&nbsp; This will help you in the end, event if it is just learning the amount of dedication it really takes to become good at something.  <li>Beware of preferences.&nbsp; Any time I hear someone start a statement with "I prefer code to ..." you know things are going downhill.&nbsp; If you find someone who cares more about how your code is formatted then how it is written you have found yourself in a mess.&nbsp; More importantly, beware of them in yourself.&nbsp;&nbsp; Having code style standard is important, but it isn't worth loosing sleep over.&nbsp; This also pertains to inheritance, patterns, use of particular classes (e.g. always using the generic list class in C# when a Dictionary would be better).&nbsp; <li>No Sacred Cows. Believe no single source of information.&nbsp; This means not thinking that Microsoft, Sun, IBM, Richard Stallman, or anyone else will have all of the correct answers.&nbsp; Apply the scientific method and do some research, experiment, and question the authority.&nbsp;&nbsp; There should be no sacred cows in programming.  <li>Talk with others.&nbsp; Join a user group, show up every now and then, heckle the presenter, and -maybe- speak.&nbsp; You want a broad range of people who you can talk to and bounce ideas off of.&nbsp; This will help you from becoming that crazy guy in the corner who vehemently states that all your code should be in one file.&nbsp; Having a mentor (mentioned earlier) is great, but having people around to push you from multiple directions is also good.  <li>Learn languages.&nbsp; This gets back to the "read code" idea. Make that multiple types of languages as well.&nbsp; If you know C# or Java and SQL, learn Python or Ruby, get really good at JavaScript.&nbsp; If you want something really out there, learn MDX.&nbsp; This is also a repertoire thing.&nbsp; Seeing how other languages work will make you rethink your ideas about what your current code, in whatever languages you are working in, should look like.  <li>Keep Thinking!!!&nbsp; That is possibly the most important point.&nbsp; Keep thinking.&nbsp; Don't just evaluate something once and never return, go back and reevaluate. Did the technique work?&nbsp; How could it have been better?&nbsp;&nbsp; The idea is continual improvement.  <li>Switch jobs every now and then.&nbsp; When I announced I was leaving my first programming job out of college, the VP of R&amp;D had me sit down with him and talk.&nbsp; He wasn't trying to keep me (he knew I was moving to be closer to family), he wanted to give me some advice.&nbsp; He told me to change jobs every three years.&nbsp; After you have been with a place for three years you have probably learned everything you are going to learn and it time to move on.&nbsp; This doesn't mean changing companies either.&nbsp; I've been with the same company now for four years, but I've had three different jobs.&nbsp; If you have been writing commercial applications, become a consultant - or vise-versa, become a test engineer, expand your boundaries.&nbsp; Again, this is about pushing yourself to be better.  <li>Have a Hobby. But don't ask about me, I have too many.&nbsp;&nbsp; Creating software is about creating things.&nbsp; So I suggest picking a hobby that involves creating something.&nbsp; Popular hobbies amongst programmers I know include: cooking, music, woodworking, beer making, and BBQ (which is different than cooking).&nbsp; But don't discount sports (golf is always popular), computer games, and board games.&nbsp; Anything that promotes the development of skill levels can't be a bad thing. </li></ol> <p>And I'm stopping there.&nbsp; This is my beginner's guide to how to become the writer elegant code.&nbsp; If you have others you think I missed, add them too the comments.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>LINQ Framework Design Guidelines</title>
		<link>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=linq-framework-design-guidelines-2</link>
		<comments>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/#comments</comments>
		<pubDate>Wed, 19 Mar 2008 01:53:22 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[Microsoft]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/</guid>
		<description><![CDATA[Excellent article on design considerations for LINQ. Looks like these concepts will be incorporated into the 2nd Edition of Framework Design Guidelines: Conventions, Idioms, and Patterns for Reuseable .NET Libraries. Cant wait to get my hands on a copy of that!]]></description>
			<content:encoded><![CDATA[<p>Excellent article on <a href="http://blogs.msdn.com/mirceat/archive/2008/03/13/linq-framework-design-guidelines.aspx" target="_blank">design considerations for LINQ</a>. Looks like these concepts will be incorporated into the 2nd Edition of <a href="http://www.amazon.com/Framework-Design-Guidelines-Conventions-Development/dp/0321545613/ref=pd_bbs_sr_2?ie=UTF8&amp;s=books&amp;qid=1200508590&amp;sr=1-2" target="_blank">Framework Design Guidelines: Conventions, Idioms, and Patterns for Reuseable .NET Libraries</a>. Cant wait to get my hands on a copy of that!</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Boise Code Camp 2008</title>
		<link>http://elegantcode.com/2008/03/09/boise-code-camp-2008/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=boise-code-camp-2008</link>
		<comments>http://elegantcode.com/2008/03/09/boise-code-camp-2008/#comments</comments>
		<pubDate>Mon, 10 Mar 2008 02:25:35 +0000</pubDate>
		<dc:creator>Alex Mueller</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Code Camp]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Patterns and Practices]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/09/boise-code-camp-2008/</guid>
		<description><![CDATA[Boise Code Camp 2008 was awesome. Thank you to David Starr and his wife, Eleanor, for taking ownership of this event and dedicating many months of their time to make this a reality. Thank you to the presenters for having the passion and desire to present technologies and practices, both new and not-as-new. Thank you [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Boise Code Camp 2008" href="http://www.boisecodecamp.org/" target="_blank">Boise Code Camp 2008</a> was awesome. Thank you to <a title="David Starr" href="http://elegantcode.com/about/david-starr/" target="_blank">David Starr</a> and his wife, <a title="Domestic Oblivion" href="http://domesticoblivion.com/" target="_blank">Eleanor</a>, for taking ownership of this event and dedicating many months of their time to make this a reality. Thank you to the presenters for having the passion and desire to present technologies and practices, both new and not-as-new. Thank you to the many volunteers who put in countless hours to help coordinate the weekend's events. Thank you to the campers for attending and making this year's code camp a success.</p>  <p>I am sure everyone will be blogging about all the great sessions they attended at code camp. I enjoyed all of the presentations I attended, and I am excited to explore some new material. </p>  <p>I want to share a few thoughts on my presentation dealing with Model-View-Presenter in ASP.NET after listening to <a title="Scott Hanselman" href="http://www.hanselman.com" target="_blank">Scott Hanselman's</a> session on the <a title="ASP. NET MVC Project" href="http://www.asp.net/mvc" target="_blank">ASP.NET MVC Project</a>. I may have the percentage incorrect, but Scott mentioned something like it is predicted that only 10% of the ASP.NET community currently using Web Forms will adopt and use the MVC framework. The MVC framework is an addition to ASP.NET, it is not a replacement. What this means is that Model-View-Presenter will still be a viable pattern to implement with your ASP.NET applications, and it is not going away. ASP.NET Web Forms will not be going away either.</p>  <p>Use MVP to get your third party controls under test. Use MVP to provide that separation of concerns in your legacy applications. Use it entirely or in conjunction with the MVC framework. It is all about testability. <a title="Glenn Block" href="http://blogs.msdn.com/gblock/" target="_blank">Glenn Block</a> presented on the Web Client Software Factory, and what pattern does this implement? Model-View-Presenter.</p>  <p>I am providing my <a title="Presentation and Source Code" href="http://muellerdesigns.net/DasBlog/content/binary/ModeViewPresenter.AlexMueller.BoiseCodeCamp2008.rar" target="_blank">presentation and source code (3.67 MB)</a> from my talk on MVP. It will be available via the <a title="Boise Code Camp" href="http://www.boisecodecamp.org/" target="_blank">Boise Code Camp</a> site as well. </p>  <p>Again, thank the many individuals and their families who sacrificed their time to bring to the local community this years code camp.</p>  <p>Around the office's water cooler on Monday, I will be able to say, &quot;and this one time, at code camp...&quot;</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/09/boise-code-camp-2008/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Microsoft releases Unity, an IoC library</title>
		<link>http://elegantcode.com/2008/04/28/learning-design-patterns/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=learning-design-patterns</link>
		<comments>http://elegantcode.com/2008/04/28/learning-design-patterns/#comments</comments>
		<pubDate>Mon, 28 Apr 2008 19:01:17 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/28/learning-design-patterns/</guid>
		<description><![CDATA[To me this seems like an interesting way to learn about design patterns (watch the video). I wonder if it also works when I hit an ivory-tower architect with POEAA or DDD? I'll keep you posted. PS: I'm really looking forward to the next episode on code reviews and retrospectives.]]></description>
			<content:encoded><![CDATA[To me <a href="http://codebetter.com/blogs/glenn.block/archive/2008/04/28/extreme-patterns-with-oren.aspx" target="_blank">this</a> seems like an interesting way to learn about design patterns (watch the video). I wonder if it also works when I hit an ivory-tower architect with <a href="http://www.bookpool.com/sm/0321127420" target="_blank">POEAA</a> or <a href="http://www.bookpool.com/sm/0321125215" target="_blank">DDD</a>? I'll keep you posted.

PS: I'm really looking forward to the next episode on code reviews and retrospectives.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/28/learning-design-patterns/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; Design Patterns</title>
	<atom:link href="http://elegantcode.com/tag/design-patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Where to start a new program</title>
		<link>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=where-to-start-a-new-program</link>
		<comments>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/#comments</comments>
		<pubDate>Sun, 08 Jun 2008 19:50:39 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/06/08/where-to-start-a-new-program/</guid>
		<description><![CDATA[Here as an interesting question that popped up at my user group meeting last night, when you are starting a new program (green field development), where do you get started?&#160; I find these questions interesting because there really is no correct answer, but a persons answer will tell you something about how they problem solve. [...]]]></description>
			<content:encoded><![CDATA[<p>Here as an interesting question that popped up at my user group meeting last night, when you are starting a new program (green field development), where do you get started?&nbsp; I find these questions interesting because there really is no correct answer, but a persons answer will tell you something about how they problem solve.</p> <p>Depending on who you ask you will likely get one of two answers.&nbsp; If you are talking with a data guy, he will say to start with the database.&nbsp; If you are dealing with a visual guy, he will say to start with the UI.&nbsp; Historically, I did a conglomeration of those two ideas, creating both the UI and the database at the same time, letting the two drive each other.&nbsp; But I'm weird, I'm a visual/data guy.</p> <p>Lately though, I've started to thing about a third approach.&nbsp; Starting in the middle.&nbsp; No UI, no database, just the data classes.&nbsp; This approach has been spurred on buy a couple of things.&nbsp; Namely, test driven development (TDD), design patterns, and domain driven design.</p> <p>TDD really is the glue that allows all of this to happen.&nbsp; I can start in the middle because I can run the code in the middle any time I want.&nbsp; I just have to write a test.&nbsp; No having to wade through multiple layers of UI to test out some small chunk ok UI is a great productivity gain for me.</p> <p>Next is design patterns.&nbsp; This is what gives you a reference for how to approach your code.&nbsp; That said, this isn't about making everything absolutely perfect from the get go, but it is nice to know where you are going to made your comprises early on.</p> <p>But all of that does not explain where I begin.&nbsp; That is where Domain Driven Design (DDD) kicks in.&nbsp; For those not familiar with the concept, you can read up on <a href="http://en.wikipedia.org/wiki/Domain-driven_design">wikipedia</a>, buy <a href="http://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1212806974&amp;sr=8-1">The Book</a>, or buy one of the <a href="http://www.amazon.com/s?ie=UTF8&amp;tag=mozilla-20&amp;index=blended&amp;link%5Fcode=qs&amp;field-keywords=domain%20driven%20design&amp;sourceid=Mozilla-search">various other references</a>.&nbsp;&nbsp; One of the concepts in DDD (I'm still reading the book) is setting up something called a <a href="http://www.domaindrivendesign.org/discussion/messageboardarchive/UbiquitousLanguage.html">ubiquitous language</a>.</p> <p>The ubiquitous language is actually trying to solve a problem that I have seen happen on many projects I've been apart of (but not always my fault).&nbsp; At some point there is a linguistic difference between the nouns that the customer uses and the nouns that the programmers use.&nbsp; By focusing on the language first, you are attempting to head that off at the pass.</p> <p>These nouns consist of your domain classes and your tables and columns, they move everywhere...but not always consistently.&nbsp; Consistency does matter.&nbsp; Inconsistency is what leads to rather confusing conversations with people and everyone scratching their heads about "what just happened".&nbsp; So, this is the middle that I start with.</p> <p>But before you start...</p> <p>One of the other guys in the group was an actual data guy.&nbsp; The systems he has written worked with more data than yours does.&nbsp; Trust me on that.&nbsp; Historically, his biggest consideration was data input speed.&nbsp; I don't think I would start in the middle if that was my biggest concern.&nbsp; In that case you do start with the database, you tune for speed.&nbsp; You also don't use as many domain classes, and you tend to throw away your ORM solution and just hit the database layer directly via Ado.Net.</p> <p>But lets face it, that doesn't happen very often.&nbsp; Most business applications aren't going to touch what the architecture can take.&nbsp; It is amazing how many horribly written applications with very inefficient code run just fine.&nbsp; Most of my speed issues could be solved by indexing tables and changing generic lists to generic dictionaries (more on that later).&nbsp; Architect your applications for how they are going to be used.&nbsp; Most of my web applications have a max usage of 100 simultaneous users.&nbsp; If the data access is slightly slower I'll take that hit for programmer efficiency.&nbsp; If you are designing Amazon.com, you code for through-put.</p> <p>OK, someone is going to disagree with this...I can already feel it.&nbsp; Please take a moment to register your displeasure in the comments.&nbsp; I would love to hear it.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Learning Design Patterns</title>
		<link>http://elegantcode.com/2008/04/28/learning-design-patterns/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=learning-design-patterns</link>
		<comments>http://elegantcode.com/2008/04/28/learning-design-patterns/#comments</comments>
		<pubDate>Mon, 28 Apr 2008 19:01:17 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/28/learning-design-patterns/</guid>
		<description><![CDATA[To me this seems like an interesting way to learn about design patterns (watch the video). I wonder if it also works when I hit an ivory-tower architect with POEAA or DDD? I'll keep you posted. PS: I'm really looking forward to the next episode on code reviews and retrospectives.]]></description>
			<content:encoded><![CDATA[To me <a href="http://codebetter.com/blogs/glenn.block/archive/2008/04/28/extreme-patterns-with-oren.aspx" target="_blank">this</a> seems like an interesting way to learn about design patterns (watch the video). I wonder if it also works when I hit an ivory-tower architect with <a href="http://www.bookpool.com/sm/0321127420" target="_blank">POEAA</a> or <a href="http://www.bookpool.com/sm/0321125215" target="_blank">DDD</a>? I'll keep you posted.

PS: I'm really looking forward to the next episode on code reviews and retrospectives.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/28/learning-design-patterns/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>DTO&#8217;s or Serialized Domain Entities?</title>
		<link>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=dtos-or-serialized-domain-entities</link>
		<comments>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/#comments</comments>
		<pubDate>Sun, 27 Apr 2008 08:09:02 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/</guid>
		<description><![CDATA[I hear this question come up time and time again. It is something that I have been dealing with for a while now, and have some experience with doing both ways. I started writing this in response to a DDD thread, but it got rather large and so I turned it into a post. So [...]]]></description>
			<content:encoded><![CDATA[<p>I hear this question come up time and time again. It is something that I have been dealing with for a while now, and have some experience with doing both ways. I started writing this in response to a <a href="http://tech.groups.yahoo.com/group/domaindrivendesign/message/7286" target="_blank">DDD thread</a>, but it got rather large and so I turned it into a post. So here ya go!  <p>I think you have to write DTO's for any sort of long term Enterprise 'system of systems' solution. I fought this for some time, but since moving to an Assembler/DTO I much enjoy the freedom of being able to easily tweak my DTO "snapshot" of the aggregate root I am working with. Maintaining them is very little overhead.  <p>I tend to think of the DTO as a message, a value object. This is necessary because once an entity leaves the boundary it can no longer function within its original meaning. Its methods may be intended to only run within a given context, which is not the same once on another tier. (Things like lazy loading are a good example) Once the DTO/Message arrives to the caller, it very well may (and probably should) be translated/assembled into another model which can represent its new semantics.  <p>Some other advantages of DTO w/ Assembler or Builder:<br>-Can explicitly control the depth of the graph/tree you are serializing.<br>-Can flatten out the data where needed<br>-Can return significantly less data (maybe you only need 3 fields instead of 15)<br>-Not tightly coupled between remote boundaries (this could be a mess when working with a large team)<br>-Interoperable  <p>I wrote a post a while back showing a way to '<a href="http://elegantcode.com/2007/12/05/fun-with-generics-from-repository-to-dto/" target="_blank">templatize</a>' the process. My current implementations are much different with LINQ now, but maybe it could stimulate some ideas for whomever.  <p><em>The next thing I would like to do is remove the assembler altogether. By flagging fields with attributes or using config to specify a mapping, I could then feed <a href="http://blogs.msdn.com/meek/archive/2008/04/25/using-linq-expressions-to-generate-dynamic-methods.aspx" target="_blank">dynamic methods</a> and run a translation/builder strategy.</em>  <p>I know there are concerns about writing and maintaining assembler and DTO, but to me this is the least of the worry, a more challenging scenario would be the UnitOfWork (How to you intended to track remote state, concurrency etc) How about versioning? performance? scala.. well ilities in general?  <p>These kinds of problems lead one from Request/Response, Patterns of Enterprise Architecture &amp; DDD to a Pub/Sub Message Bus - Enterprise Integration Patterns &amp; DDDD.  <p>And to think all you want to do is send some frickin entities to a remote client&nbsp; :)</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Test Data Builders Refined</title>
		<link>http://elegantcode.com/2008/04/26/test-data-builders-refined/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=test-data-builders-refined</link>
		<comments>http://elegantcode.com/2008/04/26/test-data-builders-refined/#comments</comments>
		<pubDate>Sat, 26 Apr 2008 23:08:27 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/26/test-data-builders-refined/</guid>
		<description><![CDATA[Last year, I blogged about Test Data Builders here and here. I still use them heavily in my unit tests for creating objects with test data. Heck, I also use this pattern for fluent interfaces in production code. Here is a simple example of this approach: &#160; 1: public class CustomerBuilder 2: { 3: public [...]]]></description>
			<content:encoded><![CDATA[<p>Last year, I blogged about Test Data Builders <a href="http://vanryswyckjan.blogspot.com/2007/09/test-data-builder-versus-object-mother.html" target="_blank">here</a> and <a href="http://vanryswyckjan.blogspot.com/2007/12/test-data-builder.html" target="_blank">here</a>. I still use them heavily in my unit tests for creating objects with test data. Heck, I also use this pattern for fluent interfaces in production code. Here is a simple example of this approach:</p>  <p>&#160;</p>  <div>   <div class="csharpcode">     <pre class="alteven"><span class="lnum">   1:</span> <span class="kwrd">public</span> <span class="kwrd">class</span> CustomerBuilder</pre>

    <pre class="alteven"><span class="lnum">   2:</span> {</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     <span class="kwrd">public</span> String _firstName = <span class="str">&quot;Homer&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     <span class="kwrd">public</span> String _lastName = <span class="str">&quot;Simpson&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   5:</span>         </pre>

    <pre class="alteven"><span class="lnum">   6:</span>     <span class="kwrd">public</span> CustomerBuilder WithFirstName(String firstName)</pre>

    <pre class="alteven"><span class="lnum">   7:</span>     {</pre>

    <pre class="alteven"><span class="lnum">   8:</span>         _firstName = firstName;</pre>

    <pre class="alteven"><span class="lnum">   9:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;    </pre>

    <pre class="alteven"><span class="lnum">  10:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  11:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  12:</span>     <span class="kwrd">public</span> CustomerBuilder WithLastName(String lastName)</pre>

    <pre class="alteven"><span class="lnum">  13:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  14:</span>         _lastName = lastName;</pre>

    <pre class="alteven"><span class="lnum">  15:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven"><span class="lnum">  16:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  17:</span>         </pre>

    <pre class="alteven"><span class="lnum">  18:</span>     <span class="kwrd">public</span> Customer Build()</pre>

    <pre class="alteven"><span class="lnum">  19:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  20:</span>         <span class="kwrd">return</span> <span class="kwrd">new</span> Customer(_firstName, _lastName);    </pre>

    <pre class="alteven"><span class="lnum">  21:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  22:</span> }</pre>
  </div>
</div>

<pre class="alteven">This fluent builder class can then be used this way:</pre>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> Customer customer = <span class="kwrd">new</span> CustomerBuilder()</pre>

    <pre class="alteven"><span class="lnum">   2:</span>     .WithFirstName(<span class="str">&quot;Homer&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     .WithLastName(<span class="str">&quot;Simpson&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     .Build();</pre>
  </div>
</div>

<p>A while ago, <a href="http://codebetter.com/blogs/gregyoung/default.aspx" target="_blank">Greg Young</a> started a series of blog posts on DDDD (Distributed Domain-Driven Design), which I can highly recommend. Make sure to catch up now you still can because I think that he has a lot of stuff coming up, which I'm really looking forward to.</p>

<p>Anyhow, Greg had a couple of posts on fluent builders, which you can read <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/15/dddd-5-messages-have-fluent-builders.aspx" target="_blank">here</a>, <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/16/dddd-6-fluent-builders-alternate-ending.aspx" target="_blank">here</a> and <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/22/dddd-8-fluent-builders-and-tests.aspx" target="_blank">here</a>. I noticed an interesting approach in the way that the target object is built. Here is an example of this approach:</p>

<p>&#160;</p>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> <span class="kwrd">public</span> <span class="kwrd">class</span> CustomerBuilder</pre>

    <pre class="alteven"><span class="lnum">   2:</span> {</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     <span class="kwrd">public</span> String _firstName = <span class="str">&quot;Homer&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     <span class="kwrd">public</span> String _lastName = <span class="str">&quot;Simpson&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   5:</span>         </pre>

    <pre class="alteven"><span class="lnum">   6:</span>     <span class="kwrd">public</span> CustomerBuilder WithFirstName(String firstName)</pre>

    <pre class="alteven"><span class="lnum">   7:</span>     {</pre>

    <pre class="alteven"><span class="lnum">   8:</span>         _firstName = firstName;</pre>

    <pre class="alteven"><span class="lnum">   9:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;    </pre>

    <pre class="alteven"><span class="lnum">  10:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  11:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  12:</span>     <span class="kwrd">public</span> CustomerBuilder WithLastName(String lastName)</pre>

    <pre class="alteven"><span class="lnum">  13:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  14:</span>         _lastName = lastName;</pre>

    <pre class="alteven"><span class="lnum">  15:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven"><span class="lnum">  16:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  17:</span>         </pre>

    <pre class="alteven"><span class="lnum">  18:</span>     <span class="kwrd">public</span> Customer Build()</pre>

    <pre class="alteven"><span class="lnum">  19:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  20:</span>         <span class="kwrd">return</span> <span class="kwrd">new</span> Customer(_firstName, _lastName);    </pre>

    <pre class="alteven"><span class="lnum">  21:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  22:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  23:</span>     <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">implicit</span> <span class="kwrd">operator</span> Customer(</pre>

    <pre class="alteven"><span class="lnum">  24:</span>         CustomerBuilder builder) </pre>

    <pre class="alteven"><span class="lnum">  25:</span>     {  </pre>

    <pre class="alteven"><span class="lnum">  26:</span>         <span class="kwrd">return</span> builder.Build()    </pre>

    <pre class="alteven"><span class="lnum">  27:</span>     } </pre>

    <pre class="alteven"><span class="lnum">  28:</span> }</pre>
  </div>
</div>

<p>which results in the following usage:</p>

<p>&#160;</p>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> Customer customer = <span class="kwrd">new</span> CustomerBuilder()</pre>

    <pre class="alteven"><span class="lnum">   2:</span>     .WithFirstName(<span class="str">&quot;Homer&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     .WithLastName(<span class="str">&quot;Simpson&quot;</span>);</pre>
  </div>

  <br />Adding an implicit cast operator to the builder class makes that its no longer required to explicitly call the <em>Build</em> method. I keep the <em>Build</em> method around for backwards-compatibility reasons or in case I ever need it again (violating YAGNI in the process, I know, I know). I find that adding the implicit cast operator adds to the readability of the fluent interface, don't you agree?</div>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/26/test-data-builders-refined/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Name Those Patterns</title>
		<link>http://elegantcode.com/2008/04/24/name-those-patterns/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=name-those-patterns</link>
		<comments>http://elegantcode.com/2008/04/24/name-those-patterns/#comments</comments>
		<pubDate>Thu, 24 Apr 2008 16:16:13 +0000</pubDate>
		<dc:creator>Tony Rasa</dc:creator>
				<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/24/name-those-patterns/</guid>
		<description><![CDATA[In yesterday's discussion of Gang of Four Design Patterns, I said &#34;Never name your classes after the pattern itself.&#34;&#160; But then someone pointed out &#34;What about Factory classes?&#34;&#160; &#34;Oh, ok, ALWAYS name classes after the pattern.&#34;&#160; Except for Singleton or Fa&#231;ade, and maybe a few others.&#160; Ok then, NEVER name after the pattern, except when [...]]]></description>
			<content:encoded><![CDATA[<p>In yesterday's discussion of Gang of Four Design Patterns, I said &quot;Never name your classes after the pattern itself.&quot;&#160; But then someone pointed out &quot;What about Factory classes?&quot;&#160; &quot;Oh, ok, ALWAYS name classes after the pattern.&quot;&#160; Except for Singleton or Fa&#231;ade, and maybe a few others.&#160; Ok then, NEVER name after the pattern, except when you do, sometimes, and then ALWAYS name those, unless otherwise noted.</p>  <p>So, here's a list of the 23 Gang of Four patterns, along with when I'd use the name of the pattern in the implementation.&#160; Unless I didn't want to, and then I wouldn't.&#160; Feel free to agree with this list, unless you don't care to.</p>  <h3>Creational Patterns</h3>  <p><strong>Abstract Factory</strong> - After consideration, I have to say that I call my factories Factory, unless they're Repositories, Data Access Layers, DAOs, or something else.&#160; But most of the time, Factory.&#160; Because this would just be wrong:</p>  <pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> FooCreatorMakerThingy { ... }</pre>

<p><strong>Builder</strong> - Hmm, another noun. Guess I'd use this one too, for the same reason as Abstract Factory. Most of the time.&#160; </p>

<p><strong>Factory Method</strong> - I probably would not call my Factory Methods &quot;GetFooFactoryMethod()&quot; because that just looks weird.&#160;&#160; Maybe if we verbify the noun:</p>

<pre class="csharpcode"><span class="kwrd">public</span> Foo FactorialMethodizeFoo() {...}</pre>

<p>Nope, that's still dumb.</p>

<p><strong>Prototype</strong> - &quot;Specify the kinds of objects to create using a prototypical instance...&quot; So we don't care that the class is a &quot;Prototype,&quot; but we do care about the &quot;kind of object&quot; part.&#160; So don't name your prototypical classes &quot;FooPrototype.&quot;&#160; (Unless you just want to.)</p>

<p><strong>Singleton</strong> - Somebody have a case where it makes sense to name your Singleton &quot;Singleton&quot;?&#160; I can't come up with one.&#160; Anyway, you shouldn't be using singletons anymore anyway.&#160; :)</p>

<h3>Structural Patterns</h3>

<p><strong>Adapter</strong> - Adapters are pretty much called Adapters.&#160; Except when they're called Wrappers.&#160; So you can use this name too.</p>

<p><strong>Bridge</strong> - I wouldn't label a participant in a bridge pattern &quot;FooBridge,&quot; unless I was trying to impress on an interview test or something.&#160; And even then, that's a bad idea.&#160; &quot;Ohh, look, I know a complicated design pattern!&#160; Lookitme!&quot;&#160; So don't call your bridges Bridges.&#160; </p>

<p><strong>Composite</strong> - &quot;One of the goals of the Composite&#160; pattern is to make clients unaware of the specific Leaf or Composite classes they're using.&quot;&#160; So you don't want to point out that Composites are in play, that breaks the suspension of disbelief for your audience.&#160; Or something.&#160; Anyway Composites get used all over the place, if we had to label them all then it'd be FooComposite, BarComposite, TreeComposite, EverythingComposite, and what's the fun in that.</p>

<p><strong>Decorator </strong>- I think in general people don't label their Decorators Decorators.&#160; I'm sure there's a counter-example to this, but all of the Stream- and Xml-handling classes I can think of don't point out that they are Decorators.&#160; You'd probably want to stick to more descriptive names, like ASCIIStreamReader instead of cluttering things up with ASCIIStreamDecorator.&#160; We're not decorating the stream (in a literal sense), we're reading it.&#160; </p>

<p><strong>Fa&#231;ade</strong> - Don't call your Fa&#231;ade &quot;FooFa&#231;ade,&quot; that's just silly.&#160; Besides, its too much work to type that cedille into the editor if you're using an ignorant American keyboard.</p>

<p><strong>Flyweight </strong>- This is another pattern where we're going to disguise what's going on inside, in order to simplify the client's usage and make better use of resources.&#160; No reason to flaunt to the client that we're low on resources, because that's a personal and private matter - times are tough all over, and there's no shame in struggling a little bit, and hey who could have predicted that the sub-prime loan market was going to ... anyway, back to patterns.&#160;&#160; Don't call your Flyweights Flyweights, unless what you're modeling is actually a Flyweight, and then thats OK.</p>

<p><strong>Proxy </strong>- This one is standard terminology.&#160; So Proxy's ok.</p>

<h3>Behavioral Patterns (or &quot;<em>MAN this book has a lot of pages in it..</em>&quot;)</h3>

<p><strong>Chain of Responsibility </strong>- Don't even think of naming a class ChainOfResponsibility.&#160; Even with ReSharper, who'd want to have to type that?</p>

<p><strong>Command</strong> - Another convenient noun.&#160; Ok, you can create Commands.&#160; </p>

<p><strong>Interpreter </strong>- This is another pattern that doesn't have one identifying class that you could label &quot;The Interpreter.&quot;&#160; But you might create a Fa&#231;ade, and call that class Interpreter.&#160; I'm not sure if naming one pattern after another is OK or not.&#160; Makes my head hurt.&#160; </p>

<p>Iterator - Another convenient noun.&#160; Iterate away!&#160; Not that you'll have to worry too much about naming classes with this pattern if you use C#, Java, C++ or pretty much any other modern language, since this pattern is built-in.</p>

<p><strong>Mediator </strong>- This pattern promotes loose coupling, most of the time I'd call what it does a &quot;Controller,&quot; or what's being Mediated is more on a service level, so perhaps the word &quot;Service&quot; would end up in the name.&#160; Can't say I've ever called a class specifically &quot;FooMediator&quot;, but I suppose I don't have a problem with it if you want to do that.</p>

<p><strong>Memento </strong>- This pattern captures and externalizes an object's state.&#160; So I think I'd call something like it &quot;State&quot; before &quot;Memento,&quot; since it's fewer letters to type.&#160; Besides, I had to see that movie twice before I really appreciated it.&#160; </p>

<p>Ok, that was just lame, sorry.</p>

<p><strong>Observer </strong>- Oh, you mean like an Event?&#160; Or more like a delegate?&#160; :)</p>

<p><strong>State </strong>- I'd call my implementation of this pattern &quot;Memento,&quot; just to confuse you.&#160; That's just how I roll.</p>

<p><strong>Strategy </strong>- This is another pattern that it seems like we use all the time, without having to give it a fancy Ivory Tower name.&#160; As someone pointed out (sorry I lost the reference to who said that) Microsoft calls all their Strategy implementations Providers now anyway.&#160; Rather than label something a &quot;Strategy&quot; I'd probably just say what the thing does: &quot;CryptographicProvider.&quot;</p>

<p><strong>Template Method</strong> - Yet another pattern where there's no obvious place to hang the name off of, so don't try to force it in there and impress your coworkers with how smart you are.&#160; We know, you're a Design Pattern God, no need to brag about it.</p>

<p><strong>Visitor </strong>- Hmm, not sure where I stand on this one.&#160; I have used this pattern where I've named a visitor &quot;SqlGeneratingVisitor.&quot;&#160; But maybe that was a mistake?&#160; So how about we just declare this one &quot;Free Parking,&quot; and you can do what you want.</p>

<p>&#160;</p>

<p>So, there you go.&#160; A bunch of design patterns, summarized into one neat page.&#160; Now you're ready to ace that interview!&#160; </p>

<p>&#160;</p>

<p><em>disclaimer: I make no guarantees that any or all of the information will not harm you in a technical interview, and if repeating this to an interview board gets your removed from the building by security, its not my fault.&#160; This information comes without warantees or conditions of any kind, and you probably shouldn't have bothered reading it..</em></p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/24/name-those-patterns/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.Net HierarchicalDataSource&lt;T&gt;</title>
		<link>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=aspnet-hierarchicaldatasourcet</link>
		<comments>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/#comments</comments>
		<pubDate>Sun, 06 Apr 2008 06:45:44 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Extension Methods]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/</guid>
		<description><![CDATA[The Asp.net 2.0 tree control, as well as many 3rd party controls binds to hierarchical data via an IHierarchicalDataSource interface. Asp.net provides an implementation OOTB with the HierarchicalDataSourceControl! Easy, let’s hook that up to our… wait this control only takes data in xml format or from a sitemap? Hmm, we don’t want to convert our [...]]]></description>
			<content:encoded><![CDATA[<p>The Asp.net 2.0 tree control, as well as many 3rd party controls binds to hierarchical data via an IHierarchicalDataSource interface. Asp.net provides an implementation OOTB with the HierarchicalDataSourceControl! Easy, let’s hook that up to our… wait this control only takes data in xml format or from a sitemap? Hmm, we don’t want to convert our model to xml, and a sitemap seems inappropriate. We spend a lot time doing great patterns such as MVP &amp; MVC, we should just be able to bind up a list of model objects we already have.  <p>Good thing MS has exposed a set of interfaces and abstract classes in System.Web.UI for us to implement our own IHierarchicalDataSource and HierarchicalDataSourceView and IHierarchicalEnumerable and then IHierarchyData... geesh.  <p>Data? So let's see here, in order to bind our model to the tree control it needs to implement an interface in System.Web.UI? Well that's just not going to happen, so its time for some Elegant Code.  <p><strong>Story:</strong>  <p>Dude, as a software developer, needs to create a custom implementation of IHierarchicalDataSource, and the rest of its parts, so that he can bind a list of objects to a tree control in asp.net  <ul> <li>Use existing model to bind to tree control  <li>Must be able to reuse data source for future models  <li>Model must not contain a reference to System.Web.UI </li></ul> <p>In order to setup our model to address the acceptance criteria we are going to use an interface with a self-referencing generic declaration, where the type will specify itself as the concrete type. This will let us create a nice generic hierarchical model to pass into our datasource without resorting to any sort of base class.  <div><pre class="csharpcode"><span class="kwrd">namespace</span> POC.Common
{
    <span class="kwrd">public</span> <span class="kwrd">interface</span> IModelWithHierarchy&lt;T&gt;
    {
        <span class="kwrd">string</span> Name { get; set; }
        T Parent { get; set; }
        List&lt;T&gt; Children { get; set; }
    }
}</pre><em>Lets note that we have placed the interface in a Common namespace which represents a shared.dll between Web &amp; Model</em> </div>
<p>The UI part of criteria is now a bit more tricky because the HierarchicalDataSourceView expects a IHierarchicalEnumerable datasource which contains IHierarchyData. Said IHierarchyData != IModelWithHierarchy&lt;T&gt;.&nbsp; Luckily someone else has already figured this one out for us. We need an <a href="http://www.dofactory.com/Patterns/PatternAdapter.aspx" target="_blank">adapter</a>. 
<p><a href="http://elegantcode.com/wp-content/uploads/2008/04/image7.png" target="_blank"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="123" alt="image" src="http://elegantcode.com/wp-content/uploads/2008/04/image-thumb7.png" width="244" border="0"></a> 
<p>To instantiate our HierarchyData adapter objects we will use an extension method and linq 'Select' to convert our datasource IEnumerable&lt;T&gt; of IModelWithHierarchy&lt;T&gt; 's to an IHierarchicalEnumerable of IHierarchyData. 
<div><pre class="csharpcode"><span class="kwrd">namespace</span> POC.Web.HierarchyExtensions
{
    <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">class</span> HierarchyConverter
    {
        <span class="kwrd">public</span> <span class="kwrd">static</span> HierarchicalModelList
            ToHierarchicalModelList&lt;T&gt;(<span class="kwrd">this</span> IEnumerable&lt;T&gt; modelWithHierarchy)
            <span class="kwrd">where</span> T : IModelWithHierarchy&lt;T&gt;
        {
            <span class="kwrd">return</span> <span class="kwrd">new</span> HierarchicalModelList(
                modelWithHierarchy.Select(m =&gt; <span class="kwrd">new</span> HierarchyData&lt;T&gt;(m) <span class="kwrd">as</span> IHierarchyData));
        }
    }
}</pre></div>
<p>I am not going to bore you with the implementation details of the HierarachyData&lt;T&gt; adapter and the .net abstractions, but have dropped the <a href="http://elegantcode.com/wp-content/uploads/2008/04/hierarchydatasourcepoc.zip" target="_blank">source code here</a> if your interested. The end result: 
<div><pre class="csharpcode">TreeView1.DataSource = <span class="kwrd">new</span> HierarchicalModelDataSource&lt;Category&gt; { DataSource = Categories };
TreeView1.DataBind();
TreeView1.CollapseAll();</pre></div>
<p>Story complete!&nbsp; </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is Elegant Code to me?</title>
		<link>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-is-elegant-code-to-me</link>
		<comments>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/#comments</comments>
		<pubDate>Mon, 31 Mar 2008 03:47:45 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Patterns and Practices]]></category>
		<category><![CDATA[Widgets of Wisdom]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/</guid>
		<description><![CDATA[This question keeps popping up around here ("around here" being the loose conglomeration that makes up the Elegant Code group).&#160; It isn't easy to describe.&#160; And really, the notion of what constitutes elegance in code changes over time.&#160; There is no static "this is good code" test, and I doubt there ever will be.&#160; Plus, [...]]]></description>
			<content:encoded><![CDATA[<p>This question keeps popping up around here ("around here" being the loose conglomeration that makes up the Elegant Code group).&nbsp; It isn't easy to describe.&nbsp; And really, the notion of what constitutes elegance in code changes over time.&nbsp; There is no static "this is good code" test, and I doubt there ever will be.&nbsp; Plus, what makes good code in one language, may not apply to the next.</p> <p>So let me state for the record: today's elegant code is tomorrow's drivel.&nbsp; Don't feel bad, many writers have the same problems.&nbsp; What was super amazing back in the day is now rubbish or near unreadable.&nbsp; I am thinking of the Victorian writing that Hemingway usurped, and now Hemingway himself is almost unreadable (to me anyway).&nbsp; Tastes change with the times.&nbsp; That is a simple fact.</p> <p>So what can you do about this?&nbsp; As I say that old writing sucks to read (read Washington Irvine lately?), great works of literature still abound (for instance, Hemingway is still a great writer -- even if I prefer Tolkien) .&nbsp; So take some notes from them, and from other crafts in looking for the answer.&nbsp; If you want a cliff notes version of what this is going to tell you, it is this: you must always push yourself to get better.</p> <ol> <li>Find a good teacher.&nbsp; Nothing is better than sitting at the feet of a master who can nudge you along in the right direction.&nbsp; While early mistakes can often be corrected easily with a little bit of guidance, after they have had some time to fester all bets are off.&nbsp; I believe a fare number of "Anit-patterns" were created by self taught developers (find <a href="http://elegantcode.com/2008/03/21/sql-ejaculation/">SQL Ejaculation</a> on this site).&nbsp; <li>Read.&nbsp;&nbsp; Good writer are first good readers.&nbsp; Start with Code Complete, move into a good Patterns book, get MSDN Magazine, find some bloggers your like, but keep moving.&nbsp; You will NEVER be done reading.&nbsp; I imagine that even Martin Fowler has a "to read" booklist a mile long.  <li>Read code.&nbsp;&nbsp; There are a plethora of open source project just waiting to be read -- do so.&nbsp; This is the single best way to expand your coding repertoire, find things your language can do that you didn't even know about..&nbsp;&nbsp; Scott Hanselman has recently popularized this idea, and I thank him for it.  <li>Practice.&nbsp; This means writing small applications at home.&nbsp; You get an idea that you want to try out -- do it.&nbsp; I don't mean you have to write finished applications, just have some exploring time.&nbsp; I remember talking with an 80 year old master woodworker (he lives down the street from me), who was telling me how many time he would practice making dovetail joints before he felt competent.&nbsp; It was years worth.  <li>Pay Attention.&nbsp; Being good at anything really amounts to that.&nbsp; And don't just pay attention when reading, writing, or talking about code.&nbsp; Inspiration come from everywhere, any good artist will tell you that.&nbsp; Pay attention when you cook, when you work on the car, when you are wood working, playing an instrument, whatever it is that you do.&nbsp; This will help you in the end, event if it is just learning the amount of dedication it really takes to become good at something.  <li>Beware of preferences.&nbsp; Any time I hear someone start a statement with "I prefer code to ..." you know things are going downhill.&nbsp; If you find someone who cares more about how your code is formatted then how it is written you have found yourself in a mess.&nbsp; More importantly, beware of them in yourself.&nbsp;&nbsp; Having code style standard is important, but it isn't worth loosing sleep over.&nbsp; This also pertains to inheritance, patterns, use of particular classes (e.g. always using the generic list class in C# when a Dictionary would be better).&nbsp; <li>No Sacred Cows. Believe no single source of information.&nbsp; This means not thinking that Microsoft, Sun, IBM, Richard Stallman, or anyone else will have all of the correct answers.&nbsp; Apply the scientific method and do some research, experiment, and question the authority.&nbsp;&nbsp; There should be no sacred cows in programming.  <li>Talk with others.&nbsp; Join a user group, show up every now and then, heckle the presenter, and -maybe- speak.&nbsp; You want a broad range of people who you can talk to and bounce ideas off of.&nbsp; This will help you from becoming that crazy guy in the corner who vehemently states that all your code should be in one file.&nbsp; Having a mentor (mentioned earlier) is great, but having people around to push you from multiple directions is also good.  <li>Learn languages.&nbsp; This gets back to the "read code" idea. Make that multiple types of languages as well.&nbsp; If you know C# or Java and SQL, learn Python or Ruby, get really good at JavaScript.&nbsp; If you want something really out there, learn MDX.&nbsp; This is also a repertoire thing.&nbsp; Seeing how other languages work will make you rethink your ideas about what your current code, in whatever languages you are working in, should look like.  <li>Keep Thinking!!!&nbsp; That is possibly the most important point.&nbsp; Keep thinking.&nbsp; Don't just evaluate something once and never return, go back and reevaluate. Did the technique work?&nbsp; How could it have been better?&nbsp;&nbsp; The idea is continual improvement.  <li>Switch jobs every now and then.&nbsp; When I announced I was leaving my first programming job out of college, the VP of R&amp;D had me sit down with him and talk.&nbsp; He wasn't trying to keep me (he knew I was moving to be closer to family), he wanted to give me some advice.&nbsp; He told me to change jobs every three years.&nbsp; After you have been with a place for three years you have probably learned everything you are going to learn and it time to move on.&nbsp; This doesn't mean changing companies either.&nbsp; I've been with the same company now for four years, but I've had three different jobs.&nbsp; If you have been writing commercial applications, become a consultant - or vise-versa, become a test engineer, expand your boundaries.&nbsp; Again, this is about pushing yourself to be better.  <li>Have a Hobby. But don't ask about me, I have too many.&nbsp;&nbsp; Creating software is about creating things.&nbsp; So I suggest picking a hobby that involves creating something.&nbsp; Popular hobbies amongst programmers I know include: cooking, music, woodworking, beer making, and BBQ (which is different than cooking).&nbsp; But don't discount sports (golf is always popular), computer games, and board games.&nbsp; Anything that promotes the development of skill levels can't be a bad thing. </li></ol> <p>And I'm stopping there.&nbsp; This is my beginner's guide to how to become the writer elegant code.&nbsp; If you have others you think I missed, add them too the comments.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>LINQ Framework Design Guidelines</title>
		<link>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=linq-framework-design-guidelines-2</link>
		<comments>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/#comments</comments>
		<pubDate>Wed, 19 Mar 2008 01:53:22 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[Microsoft]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/</guid>
		<description><![CDATA[Excellent article on design considerations for LINQ. Looks like these concepts will be incorporated into the 2nd Edition of Framework Design Guidelines: Conventions, Idioms, and Patterns for Reuseable .NET Libraries. Cant wait to get my hands on a copy of that!]]></description>
			<content:encoded><![CDATA[<p>Excellent article on <a href="http://blogs.msdn.com/mirceat/archive/2008/03/13/linq-framework-design-guidelines.aspx" target="_blank">design considerations for LINQ</a>. Looks like these concepts will be incorporated into the 2nd Edition of <a href="http://www.amazon.com/Framework-Design-Guidelines-Conventions-Development/dp/0321545613/ref=pd_bbs_sr_2?ie=UTF8&amp;s=books&amp;qid=1200508590&amp;sr=1-2" target="_blank">Framework Design Guidelines: Conventions, Idioms, and Patterns for Reuseable .NET Libraries</a>. Cant wait to get my hands on a copy of that!</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Boise Code Camp 2008</title>
		<link>http://elegantcode.com/2008/03/09/boise-code-camp-2008/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=boise-code-camp-2008</link>
		<comments>http://elegantcode.com/2008/03/09/boise-code-camp-2008/#comments</comments>
		<pubDate>Mon, 10 Mar 2008 02:25:35 +0000</pubDate>
		<dc:creator>Alex Mueller</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Code Camp]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Patterns and Practices]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/09/boise-code-camp-2008/</guid>
		<description><![CDATA[Boise Code Camp 2008 was awesome. Thank you to David Starr and his wife, Eleanor, for taking ownership of this event and dedicating many months of their time to make this a reality. Thank you to the presenters for having the passion and desire to present technologies and practices, both new and not-as-new. Thank you [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Boise Code Camp 2008" href="http://www.boisecodecamp.org/" target="_blank">Boise Code Camp 2008</a> was awesome. Thank you to <a title="David Starr" href="http://elegantcode.com/about/david-starr/" target="_blank">David Starr</a> and his wife, <a title="Domestic Oblivion" href="http://domesticoblivion.com/" target="_blank">Eleanor</a>, for taking ownership of this event and dedicating many months of their time to make this a reality. Thank you to the presenters for having the passion and desire to present technologies and practices, both new and not-as-new. Thank you to the many volunteers who put in countless hours to help coordinate the weekend's events. Thank you to the campers for attending and making this year's code camp a success.</p>  <p>I am sure everyone will be blogging about all the great sessions they attended at code camp. I enjoyed all of the presentations I attended, and I am excited to explore some new material. </p>  <p>I want to share a few thoughts on my presentation dealing with Model-View-Presenter in ASP.NET after listening to <a title="Scott Hanselman" href="http://www.hanselman.com" target="_blank">Scott Hanselman's</a> session on the <a title="ASP. NET MVC Project" href="http://www.asp.net/mvc" target="_blank">ASP.NET MVC Project</a>. I may have the percentage incorrect, but Scott mentioned something like it is predicted that only 10% of the ASP.NET community currently using Web Forms will adopt and use the MVC framework. The MVC framework is an addition to ASP.NET, it is not a replacement. What this means is that Model-View-Presenter will still be a viable pattern to implement with your ASP.NET applications, and it is not going away. ASP.NET Web Forms will not be going away either.</p>  <p>Use MVP to get your third party controls under test. Use MVP to provide that separation of concerns in your legacy applications. Use it entirely or in conjunction with the MVC framework. It is all about testability. <a title="Glenn Block" href="http://blogs.msdn.com/gblock/" target="_blank">Glenn Block</a> presented on the Web Client Software Factory, and what pattern does this implement? Model-View-Presenter.</p>  <p>I am providing my <a title="Presentation and Source Code" href="http://muellerdesigns.net/DasBlog/content/binary/ModeViewPresenter.AlexMueller.BoiseCodeCamp2008.rar" target="_blank">presentation and source code (3.67 MB)</a> from my talk on MVP. It will be available via the <a title="Boise Code Camp" href="http://www.boisecodecamp.org/" target="_blank">Boise Code Camp</a> site as well. </p>  <p>Again, thank the many individuals and their families who sacrificed their time to bring to the local community this years code camp.</p>  <p>Around the office's water cooler on Monday, I will be able to say, &quot;and this one time, at code camp...&quot;</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/09/boise-code-camp-2008/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Microsoft releases Unity, an IoC library</title>
		<link>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=dtos-or-serialized-domain-entities</link>
		<comments>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/#comments</comments>
		<pubDate>Sun, 27 Apr 2008 08:09:02 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/</guid>
		<description><![CDATA[I hear this question come up time and time again. It is something that I have been dealing with for a while now, and have some experience with doing both ways. I started writing this in response to a DDD thread, but it got rather large and so I turned it into a post. So [...]]]></description>
			<content:encoded><![CDATA[<p>I hear this question come up time and time again. It is something that I have been dealing with for a while now, and have some experience with doing both ways. I started writing this in response to a <a href="http://tech.groups.yahoo.com/group/domaindrivendesign/message/7286" target="_blank">DDD thread</a>, but it got rather large and so I turned it into a post. So here ya go!  <p>I think you have to write DTO's for any sort of long term Enterprise 'system of systems' solution. I fought this for some time, but since moving to an Assembler/DTO I much enjoy the freedom of being able to easily tweak my DTO "snapshot" of the aggregate root I am working with. Maintaining them is very little overhead.  <p>I tend to think of the DTO as a message, a value object. This is necessary because once an entity leaves the boundary it can no longer function within its original meaning. Its methods may be intended to only run within a given context, which is not the same once on another tier. (Things like lazy loading are a good example) Once the DTO/Message arrives to the caller, it very well may (and probably should) be translated/assembled into another model which can represent its new semantics.  <p>Some other advantages of DTO w/ Assembler or Builder:<br>-Can explicitly control the depth of the graph/tree you are serializing.<br>-Can flatten out the data where needed<br>-Can return significantly less data (maybe you only need 3 fields instead of 15)<br>-Not tightly coupled between remote boundaries (this could be a mess when working with a large team)<br>-Interoperable  <p>I wrote a post a while back showing a way to '<a href="http://elegantcode.com/2007/12/05/fun-with-generics-from-repository-to-dto/" target="_blank">templatize</a>' the process. My current implementations are much different with LINQ now, but maybe it could stimulate some ideas for whomever.  <p><em>The next thing I would like to do is remove the assembler altogether. By flagging fields with attributes or using config to specify a mapping, I could then feed <a href="http://blogs.msdn.com/meek/archive/2008/04/25/using-linq-expressions-to-generate-dynamic-methods.aspx" target="_blank">dynamic methods</a> and run a translation/builder strategy.</em>  <p>I know there are concerns about writing and maintaining assembler and DTO, but to me this is the least of the worry, a more challenging scenario would be the UnitOfWork (How to you intended to track remote state, concurrency etc) How about versioning? performance? scala.. well ilities in general?  <p>These kinds of problems lead one from Request/Response, Patterns of Enterprise Architecture &amp; DDD to a Pub/Sub Message Bus - Enterprise Integration Patterns &amp; DDDD.  <p>And to think all you want to do is send some frickin entities to a remote client&nbsp; :)</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; Design Patterns</title>
	<atom:link href="http://elegantcode.com/tag/design-patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Where to start a new program</title>
		<link>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=where-to-start-a-new-program</link>
		<comments>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/#comments</comments>
		<pubDate>Sun, 08 Jun 2008 19:50:39 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/06/08/where-to-start-a-new-program/</guid>
		<description><![CDATA[Here as an interesting question that popped up at my user group meeting last night, when you are starting a new program (green field development), where do you get started?&#160; I find these questions interesting because there really is no correct answer, but a persons answer will tell you something about how they problem solve. [...]]]></description>
			<content:encoded><![CDATA[<p>Here as an interesting question that popped up at my user group meeting last night, when you are starting a new program (green field development), where do you get started?&nbsp; I find these questions interesting because there really is no correct answer, but a persons answer will tell you something about how they problem solve.</p> <p>Depending on who you ask you will likely get one of two answers.&nbsp; If you are talking with a data guy, he will say to start with the database.&nbsp; If you are dealing with a visual guy, he will say to start with the UI.&nbsp; Historically, I did a conglomeration of those two ideas, creating both the UI and the database at the same time, letting the two drive each other.&nbsp; But I'm weird, I'm a visual/data guy.</p> <p>Lately though, I've started to thing about a third approach.&nbsp; Starting in the middle.&nbsp; No UI, no database, just the data classes.&nbsp; This approach has been spurred on buy a couple of things.&nbsp; Namely, test driven development (TDD), design patterns, and domain driven design.</p> <p>TDD really is the glue that allows all of this to happen.&nbsp; I can start in the middle because I can run the code in the middle any time I want.&nbsp; I just have to write a test.&nbsp; No having to wade through multiple layers of UI to test out some small chunk ok UI is a great productivity gain for me.</p> <p>Next is design patterns.&nbsp; This is what gives you a reference for how to approach your code.&nbsp; That said, this isn't about making everything absolutely perfect from the get go, but it is nice to know where you are going to made your comprises early on.</p> <p>But all of that does not explain where I begin.&nbsp; That is where Domain Driven Design (DDD) kicks in.&nbsp; For those not familiar with the concept, you can read up on <a href="http://en.wikipedia.org/wiki/Domain-driven_design">wikipedia</a>, buy <a href="http://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1212806974&amp;sr=8-1">The Book</a>, or buy one of the <a href="http://www.amazon.com/s?ie=UTF8&amp;tag=mozilla-20&amp;index=blended&amp;link%5Fcode=qs&amp;field-keywords=domain%20driven%20design&amp;sourceid=Mozilla-search">various other references</a>.&nbsp;&nbsp; One of the concepts in DDD (I'm still reading the book) is setting up something called a <a href="http://www.domaindrivendesign.org/discussion/messageboardarchive/UbiquitousLanguage.html">ubiquitous language</a>.</p> <p>The ubiquitous language is actually trying to solve a problem that I have seen happen on many projects I've been apart of (but not always my fault).&nbsp; At some point there is a linguistic difference between the nouns that the customer uses and the nouns that the programmers use.&nbsp; By focusing on the language first, you are attempting to head that off at the pass.</p> <p>These nouns consist of your domain classes and your tables and columns, they move everywhere...but not always consistently.&nbsp; Consistency does matter.&nbsp; Inconsistency is what leads to rather confusing conversations with people and everyone scratching their heads about "what just happened".&nbsp; So, this is the middle that I start with.</p> <p>But before you start...</p> <p>One of the other guys in the group was an actual data guy.&nbsp; The systems he has written worked with more data than yours does.&nbsp; Trust me on that.&nbsp; Historically, his biggest consideration was data input speed.&nbsp; I don't think I would start in the middle if that was my biggest concern.&nbsp; In that case you do start with the database, you tune for speed.&nbsp; You also don't use as many domain classes, and you tend to throw away your ORM solution and just hit the database layer directly via Ado.Net.</p> <p>But lets face it, that doesn't happen very often.&nbsp; Most business applications aren't going to touch what the architecture can take.&nbsp; It is amazing how many horribly written applications with very inefficient code run just fine.&nbsp; Most of my speed issues could be solved by indexing tables and changing generic lists to generic dictionaries (more on that later).&nbsp; Architect your applications for how they are going to be used.&nbsp; Most of my web applications have a max usage of 100 simultaneous users.&nbsp; If the data access is slightly slower I'll take that hit for programmer efficiency.&nbsp; If you are designing Amazon.com, you code for through-put.</p> <p>OK, someone is going to disagree with this...I can already feel it.&nbsp; Please take a moment to register your displeasure in the comments.&nbsp; I would love to hear it.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Learning Design Patterns</title>
		<link>http://elegantcode.com/2008/04/28/learning-design-patterns/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=learning-design-patterns</link>
		<comments>http://elegantcode.com/2008/04/28/learning-design-patterns/#comments</comments>
		<pubDate>Mon, 28 Apr 2008 19:01:17 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/28/learning-design-patterns/</guid>
		<description><![CDATA[To me this seems like an interesting way to learn about design patterns (watch the video). I wonder if it also works when I hit an ivory-tower architect with POEAA or DDD? I'll keep you posted. PS: I'm really looking forward to the next episode on code reviews and retrospectives.]]></description>
			<content:encoded><![CDATA[To me <a href="http://codebetter.com/blogs/glenn.block/archive/2008/04/28/extreme-patterns-with-oren.aspx" target="_blank">this</a> seems like an interesting way to learn about design patterns (watch the video). I wonder if it also works when I hit an ivory-tower architect with <a href="http://www.bookpool.com/sm/0321127420" target="_blank">POEAA</a> or <a href="http://www.bookpool.com/sm/0321125215" target="_blank">DDD</a>? I'll keep you posted.

PS: I'm really looking forward to the next episode on code reviews and retrospectives.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/28/learning-design-patterns/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>DTO&#8217;s or Serialized Domain Entities?</title>
		<link>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=dtos-or-serialized-domain-entities</link>
		<comments>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/#comments</comments>
		<pubDate>Sun, 27 Apr 2008 08:09:02 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/</guid>
		<description><![CDATA[I hear this question come up time and time again. It is something that I have been dealing with for a while now, and have some experience with doing both ways. I started writing this in response to a DDD thread, but it got rather large and so I turned it into a post. So [...]]]></description>
			<content:encoded><![CDATA[<p>I hear this question come up time and time again. It is something that I have been dealing with for a while now, and have some experience with doing both ways. I started writing this in response to a <a href="http://tech.groups.yahoo.com/group/domaindrivendesign/message/7286" target="_blank">DDD thread</a>, but it got rather large and so I turned it into a post. So here ya go!  <p>I think you have to write DTO's for any sort of long term Enterprise 'system of systems' solution. I fought this for some time, but since moving to an Assembler/DTO I much enjoy the freedom of being able to easily tweak my DTO "snapshot" of the aggregate root I am working with. Maintaining them is very little overhead.  <p>I tend to think of the DTO as a message, a value object. This is necessary because once an entity leaves the boundary it can no longer function within its original meaning. Its methods may be intended to only run within a given context, which is not the same once on another tier. (Things like lazy loading are a good example) Once the DTO/Message arrives to the caller, it very well may (and probably should) be translated/assembled into another model which can represent its new semantics.  <p>Some other advantages of DTO w/ Assembler or Builder:<br>-Can explicitly control the depth of the graph/tree you are serializing.<br>-Can flatten out the data where needed<br>-Can return significantly less data (maybe you only need 3 fields instead of 15)<br>-Not tightly coupled between remote boundaries (this could be a mess when working with a large team)<br>-Interoperable  <p>I wrote a post a while back showing a way to '<a href="http://elegantcode.com/2007/12/05/fun-with-generics-from-repository-to-dto/" target="_blank">templatize</a>' the process. My current implementations are much different with LINQ now, but maybe it could stimulate some ideas for whomever.  <p><em>The next thing I would like to do is remove the assembler altogether. By flagging fields with attributes or using config to specify a mapping, I could then feed <a href="http://blogs.msdn.com/meek/archive/2008/04/25/using-linq-expressions-to-generate-dynamic-methods.aspx" target="_blank">dynamic methods</a> and run a translation/builder strategy.</em>  <p>I know there are concerns about writing and maintaining assembler and DTO, but to me this is the least of the worry, a more challenging scenario would be the UnitOfWork (How to you intended to track remote state, concurrency etc) How about versioning? performance? scala.. well ilities in general?  <p>These kinds of problems lead one from Request/Response, Patterns of Enterprise Architecture &amp; DDD to a Pub/Sub Message Bus - Enterprise Integration Patterns &amp; DDDD.  <p>And to think all you want to do is send some frickin entities to a remote client&nbsp; :)</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Test Data Builders Refined</title>
		<link>http://elegantcode.com/2008/04/26/test-data-builders-refined/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=test-data-builders-refined</link>
		<comments>http://elegantcode.com/2008/04/26/test-data-builders-refined/#comments</comments>
		<pubDate>Sat, 26 Apr 2008 23:08:27 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/26/test-data-builders-refined/</guid>
		<description><![CDATA[Last year, I blogged about Test Data Builders here and here. I still use them heavily in my unit tests for creating objects with test data. Heck, I also use this pattern for fluent interfaces in production code. Here is a simple example of this approach: &#160; 1: public class CustomerBuilder 2: { 3: public [...]]]></description>
			<content:encoded><![CDATA[<p>Last year, I blogged about Test Data Builders <a href="http://vanryswyckjan.blogspot.com/2007/09/test-data-builder-versus-object-mother.html" target="_blank">here</a> and <a href="http://vanryswyckjan.blogspot.com/2007/12/test-data-builder.html" target="_blank">here</a>. I still use them heavily in my unit tests for creating objects with test data. Heck, I also use this pattern for fluent interfaces in production code. Here is a simple example of this approach:</p>  <p>&#160;</p>  <div>   <div class="csharpcode">     <pre class="alteven"><span class="lnum">   1:</span> <span class="kwrd">public</span> <span class="kwrd">class</span> CustomerBuilder</pre>

    <pre class="alteven"><span class="lnum">   2:</span> {</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     <span class="kwrd">public</span> String _firstName = <span class="str">&quot;Homer&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     <span class="kwrd">public</span> String _lastName = <span class="str">&quot;Simpson&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   5:</span>         </pre>

    <pre class="alteven"><span class="lnum">   6:</span>     <span class="kwrd">public</span> CustomerBuilder WithFirstName(String firstName)</pre>

    <pre class="alteven"><span class="lnum">   7:</span>     {</pre>

    <pre class="alteven"><span class="lnum">   8:</span>         _firstName = firstName;</pre>

    <pre class="alteven"><span class="lnum">   9:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;    </pre>

    <pre class="alteven"><span class="lnum">  10:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  11:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  12:</span>     <span class="kwrd">public</span> CustomerBuilder WithLastName(String lastName)</pre>

    <pre class="alteven"><span class="lnum">  13:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  14:</span>         _lastName = lastName;</pre>

    <pre class="alteven"><span class="lnum">  15:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven"><span class="lnum">  16:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  17:</span>         </pre>

    <pre class="alteven"><span class="lnum">  18:</span>     <span class="kwrd">public</span> Customer Build()</pre>

    <pre class="alteven"><span class="lnum">  19:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  20:</span>         <span class="kwrd">return</span> <span class="kwrd">new</span> Customer(_firstName, _lastName);    </pre>

    <pre class="alteven"><span class="lnum">  21:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  22:</span> }</pre>
  </div>
</div>

<pre class="alteven">This fluent builder class can then be used this way:</pre>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> Customer customer = <span class="kwrd">new</span> CustomerBuilder()</pre>

    <pre class="alteven"><span class="lnum">   2:</span>     .WithFirstName(<span class="str">&quot;Homer&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     .WithLastName(<span class="str">&quot;Simpson&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     .Build();</pre>
  </div>
</div>

<p>A while ago, <a href="http://codebetter.com/blogs/gregyoung/default.aspx" target="_blank">Greg Young</a> started a series of blog posts on DDDD (Distributed Domain-Driven Design), which I can highly recommend. Make sure to catch up now you still can because I think that he has a lot of stuff coming up, which I'm really looking forward to.</p>

<p>Anyhow, Greg had a couple of posts on fluent builders, which you can read <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/15/dddd-5-messages-have-fluent-builders.aspx" target="_blank">here</a>, <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/16/dddd-6-fluent-builders-alternate-ending.aspx" target="_blank">here</a> and <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/22/dddd-8-fluent-builders-and-tests.aspx" target="_blank">here</a>. I noticed an interesting approach in the way that the target object is built. Here is an example of this approach:</p>

<p>&#160;</p>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> <span class="kwrd">public</span> <span class="kwrd">class</span> CustomerBuilder</pre>

    <pre class="alteven"><span class="lnum">   2:</span> {</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     <span class="kwrd">public</span> String _firstName = <span class="str">&quot;Homer&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     <span class="kwrd">public</span> String _lastName = <span class="str">&quot;Simpson&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   5:</span>         </pre>

    <pre class="alteven"><span class="lnum">   6:</span>     <span class="kwrd">public</span> CustomerBuilder WithFirstName(String firstName)</pre>

    <pre class="alteven"><span class="lnum">   7:</span>     {</pre>

    <pre class="alteven"><span class="lnum">   8:</span>         _firstName = firstName;</pre>

    <pre class="alteven"><span class="lnum">   9:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;    </pre>

    <pre class="alteven"><span class="lnum">  10:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  11:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  12:</span>     <span class="kwrd">public</span> CustomerBuilder WithLastName(String lastName)</pre>

    <pre class="alteven"><span class="lnum">  13:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  14:</span>         _lastName = lastName;</pre>

    <pre class="alteven"><span class="lnum">  15:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven"><span class="lnum">  16:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  17:</span>         </pre>

    <pre class="alteven"><span class="lnum">  18:</span>     <span class="kwrd">public</span> Customer Build()</pre>

    <pre class="alteven"><span class="lnum">  19:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  20:</span>         <span class="kwrd">return</span> <span class="kwrd">new</span> Customer(_firstName, _lastName);    </pre>

    <pre class="alteven"><span class="lnum">  21:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  22:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  23:</span>     <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">implicit</span> <span class="kwrd">operator</span> Customer(</pre>

    <pre class="alteven"><span class="lnum">  24:</span>         CustomerBuilder builder) </pre>

    <pre class="alteven"><span class="lnum">  25:</span>     {  </pre>

    <pre class="alteven"><span class="lnum">  26:</span>         <span class="kwrd">return</span> builder.Build()    </pre>

    <pre class="alteven"><span class="lnum">  27:</span>     } </pre>

    <pre class="alteven"><span class="lnum">  28:</span> }</pre>
  </div>
</div>

<p>which results in the following usage:</p>

<p>&#160;</p>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> Customer customer = <span class="kwrd">new</span> CustomerBuilder()</pre>

    <pre class="alteven"><span class="lnum">   2:</span>     .WithFirstName(<span class="str">&quot;Homer&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     .WithLastName(<span class="str">&quot;Simpson&quot;</span>);</pre>
  </div>

  <br />Adding an implicit cast operator to the builder class makes that its no longer required to explicitly call the <em>Build</em> method. I keep the <em>Build</em> method around for backwards-compatibility reasons or in case I ever need it again (violating YAGNI in the process, I know, I know). I find that adding the implicit cast operator adds to the readability of the fluent interface, don't you agree?</div>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/26/test-data-builders-refined/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Name Those Patterns</title>
		<link>http://elegantcode.com/2008/04/24/name-those-patterns/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=name-those-patterns</link>
		<comments>http://elegantcode.com/2008/04/24/name-those-patterns/#comments</comments>
		<pubDate>Thu, 24 Apr 2008 16:16:13 +0000</pubDate>
		<dc:creator>Tony Rasa</dc:creator>
				<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/24/name-those-patterns/</guid>
		<description><![CDATA[In yesterday's discussion of Gang of Four Design Patterns, I said &#34;Never name your classes after the pattern itself.&#34;&#160; But then someone pointed out &#34;What about Factory classes?&#34;&#160; &#34;Oh, ok, ALWAYS name classes after the pattern.&#34;&#160; Except for Singleton or Fa&#231;ade, and maybe a few others.&#160; Ok then, NEVER name after the pattern, except when [...]]]></description>
			<content:encoded><![CDATA[<p>In yesterday's discussion of Gang of Four Design Patterns, I said &quot;Never name your classes after the pattern itself.&quot;&#160; But then someone pointed out &quot;What about Factory classes?&quot;&#160; &quot;Oh, ok, ALWAYS name classes after the pattern.&quot;&#160; Except for Singleton or Fa&#231;ade, and maybe a few others.&#160; Ok then, NEVER name after the pattern, except when you do, sometimes, and then ALWAYS name those, unless otherwise noted.</p>  <p>So, here's a list of the 23 Gang of Four patterns, along with when I'd use the name of the pattern in the implementation.&#160; Unless I didn't want to, and then I wouldn't.&#160; Feel free to agree with this list, unless you don't care to.</p>  <h3>Creational Patterns</h3>  <p><strong>Abstract Factory</strong> - After consideration, I have to say that I call my factories Factory, unless they're Repositories, Data Access Layers, DAOs, or something else.&#160; But most of the time, Factory.&#160; Because this would just be wrong:</p>  <pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> FooCreatorMakerThingy { ... }</pre>

<p><strong>Builder</strong> - Hmm, another noun. Guess I'd use this one too, for the same reason as Abstract Factory. Most of the time.&#160; </p>

<p><strong>Factory Method</strong> - I probably would not call my Factory Methods &quot;GetFooFactoryMethod()&quot; because that just looks weird.&#160;&#160; Maybe if we verbify the noun:</p>

<pre class="csharpcode"><span class="kwrd">public</span> Foo FactorialMethodizeFoo() {...}</pre>

<p>Nope, that's still dumb.</p>

<p><strong>Prototype</strong> - &quot;Specify the kinds of objects to create using a prototypical instance...&quot; So we don't care that the class is a &quot;Prototype,&quot; but we do care about the &quot;kind of object&quot; part.&#160; So don't name your prototypical classes &quot;FooPrototype.&quot;&#160; (Unless you just want to.)</p>

<p><strong>Singleton</strong> - Somebody have a case where it makes sense to name your Singleton &quot;Singleton&quot;?&#160; I can't come up with one.&#160; Anyway, you shouldn't be using singletons anymore anyway.&#160; :)</p>

<h3>Structural Patterns</h3>

<p><strong>Adapter</strong> - Adapters are pretty much called Adapters.&#160; Except when they're called Wrappers.&#160; So you can use this name too.</p>

<p><strong>Bridge</strong> - I wouldn't label a participant in a bridge pattern &quot;FooBridge,&quot; unless I was trying to impress on an interview test or something.&#160; And even then, that's a bad idea.&#160; &quot;Ohh, look, I know a complicated design pattern!&#160; Lookitme!&quot;&#160; So don't call your bridges Bridges.&#160; </p>

<p><strong>Composite</strong> - &quot;One of the goals of the Composite&#160; pattern is to make clients unaware of the specific Leaf or Composite classes they're using.&quot;&#160; So you don't want to point out that Composites are in play, that breaks the suspension of disbelief for your audience.&#160; Or something.&#160; Anyway Composites get used all over the place, if we had to label them all then it'd be FooComposite, BarComposite, TreeComposite, EverythingComposite, and what's the fun in that.</p>

<p><strong>Decorator </strong>- I think in general people don't label their Decorators Decorators.&#160; I'm sure there's a counter-example to this, but all of the Stream- and Xml-handling classes I can think of don't point out that they are Decorators.&#160; You'd probably want to stick to more descriptive names, like ASCIIStreamReader instead of cluttering things up with ASCIIStreamDecorator.&#160; We're not decorating the stream (in a literal sense), we're reading it.&#160; </p>

<p><strong>Fa&#231;ade</strong> - Don't call your Fa&#231;ade &quot;FooFa&#231;ade,&quot; that's just silly.&#160; Besides, its too much work to type that cedille into the editor if you're using an ignorant American keyboard.</p>

<p><strong>Flyweight </strong>- This is another pattern where we're going to disguise what's going on inside, in order to simplify the client's usage and make better use of resources.&#160; No reason to flaunt to the client that we're low on resources, because that's a personal and private matter - times are tough all over, and there's no shame in struggling a little bit, and hey who could have predicted that the sub-prime loan market was going to ... anyway, back to patterns.&#160;&#160; Don't call your Flyweights Flyweights, unless what you're modeling is actually a Flyweight, and then thats OK.</p>

<p><strong>Proxy </strong>- This one is standard terminology.&#160; So Proxy's ok.</p>

<h3>Behavioral Patterns (or &quot;<em>MAN this book has a lot of pages in it..</em>&quot;)</h3>

<p><strong>Chain of Responsibility </strong>- Don't even think of naming a class ChainOfResponsibility.&#160; Even with ReSharper, who'd want to have to type that?</p>

<p><strong>Command</strong> - Another convenient noun.&#160; Ok, you can create Commands.&#160; </p>

<p><strong>Interpreter </strong>- This is another pattern that doesn't have one identifying class that you could label &quot;The Interpreter.&quot;&#160; But you might create a Fa&#231;ade, and call that class Interpreter.&#160; I'm not sure if naming one pattern after another is OK or not.&#160; Makes my head hurt.&#160; </p>

<p>Iterator - Another convenient noun.&#160; Iterate away!&#160; Not that you'll have to worry too much about naming classes with this pattern if you use C#, Java, C++ or pretty much any other modern language, since this pattern is built-in.</p>

<p><strong>Mediator </strong>- This pattern promotes loose coupling, most of the time I'd call what it does a &quot;Controller,&quot; or what's being Mediated is more on a service level, so perhaps the word &quot;Service&quot; would end up in the name.&#160; Can't say I've ever called a class specifically &quot;FooMediator&quot;, but I suppose I don't have a problem with it if you want to do that.</p>

<p><strong>Memento </strong>- This pattern captures and externalizes an object's state.&#160; So I think I'd call something like it &quot;State&quot; before &quot;Memento,&quot; since it's fewer letters to type.&#160; Besides, I had to see that movie twice before I really appreciated it.&#160; </p>

<p>Ok, that was just lame, sorry.</p>

<p><strong>Observer </strong>- Oh, you mean like an Event?&#160; Or more like a delegate?&#160; :)</p>

<p><strong>State </strong>- I'd call my implementation of this pattern &quot;Memento,&quot; just to confuse you.&#160; That's just how I roll.</p>

<p><strong>Strategy </strong>- This is another pattern that it seems like we use all the time, without having to give it a fancy Ivory Tower name.&#160; As someone pointed out (sorry I lost the reference to who said that) Microsoft calls all their Strategy implementations Providers now anyway.&#160; Rather than label something a &quot;Strategy&quot; I'd probably just say what the thing does: &quot;CryptographicProvider.&quot;</p>

<p><strong>Template Method</strong> - Yet another pattern where there's no obvious place to hang the name off of, so don't try to force it in there and impress your coworkers with how smart you are.&#160; We know, you're a Design Pattern God, no need to brag about it.</p>

<p><strong>Visitor </strong>- Hmm, not sure where I stand on this one.&#160; I have used this pattern where I've named a visitor &quot;SqlGeneratingVisitor.&quot;&#160; But maybe that was a mistake?&#160; So how about we just declare this one &quot;Free Parking,&quot; and you can do what you want.</p>

<p>&#160;</p>

<p>So, there you go.&#160; A bunch of design patterns, summarized into one neat page.&#160; Now you're ready to ace that interview!&#160; </p>

<p>&#160;</p>

<p><em>disclaimer: I make no guarantees that any or all of the information will not harm you in a technical interview, and if repeating this to an interview board gets your removed from the building by security, its not my fault.&#160; This information comes without warantees or conditions of any kind, and you probably shouldn't have bothered reading it..</em></p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/24/name-those-patterns/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.Net HierarchicalDataSource&lt;T&gt;</title>
		<link>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=aspnet-hierarchicaldatasourcet</link>
		<comments>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/#comments</comments>
		<pubDate>Sun, 06 Apr 2008 06:45:44 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Extension Methods]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/</guid>
		<description><![CDATA[The Asp.net 2.0 tree control, as well as many 3rd party controls binds to hierarchical data via an IHierarchicalDataSource interface. Asp.net provides an implementation OOTB with the HierarchicalDataSourceControl! Easy, let’s hook that up to our… wait this control only takes data in xml format or from a sitemap? Hmm, we don’t want to convert our [...]]]></description>
			<content:encoded><![CDATA[<p>The Asp.net 2.0 tree control, as well as many 3rd party controls binds to hierarchical data via an IHierarchicalDataSource interface. Asp.net provides an implementation OOTB with the HierarchicalDataSourceControl! Easy, let’s hook that up to our… wait this control only takes data in xml format or from a sitemap? Hmm, we don’t want to convert our model to xml, and a sitemap seems inappropriate. We spend a lot time doing great patterns such as MVP &amp; MVC, we should just be able to bind up a list of model objects we already have.  <p>Good thing MS has exposed a set of interfaces and abstract classes in System.Web.UI for us to implement our own IHierarchicalDataSource and HierarchicalDataSourceView and IHierarchicalEnumerable and then IHierarchyData... geesh.  <p>Data? So let's see here, in order to bind our model to the tree control it needs to implement an interface in System.Web.UI? Well that's just not going to happen, so its time for some Elegant Code.  <p><strong>Story:</strong>  <p>Dude, as a software developer, needs to create a custom implementation of IHierarchicalDataSource, and the rest of its parts, so that he can bind a list of objects to a tree control in asp.net  <ul> <li>Use existing model to bind to tree control  <li>Must be able to reuse data source for future models  <li>Model must not contain a reference to System.Web.UI </li></ul> <p>In order to setup our model to address the acceptance criteria we are going to use an interface with a self-referencing generic declaration, where the type will specify itself as the concrete type. This will let us create a nice generic hierarchical model to pass into our datasource without resorting to any sort of base class.  <div><pre class="csharpcode"><span class="kwrd">namespace</span> POC.Common
{
    <span class="kwrd">public</span> <span class="kwrd">interface</span> IModelWithHierarchy&lt;T&gt;
    {
        <span class="kwrd">string</span> Name { get; set; }
        T Parent { get; set; }
        List&lt;T&gt; Children { get; set; }
    }
}</pre><em>Lets note that we have placed the interface in a Common namespace which represents a shared.dll between Web &amp; Model</em> </div>
<p>The UI part of criteria is now a bit more tricky because the HierarchicalDataSourceView expects a IHierarchicalEnumerable datasource which contains IHierarchyData. Said IHierarchyData != IModelWithHierarchy&lt;T&gt;.&nbsp; Luckily someone else has already figured this one out for us. We need an <a href="http://www.dofactory.com/Patterns/PatternAdapter.aspx" target="_blank">adapter</a>. 
<p><a href="http://elegantcode.com/wp-content/uploads/2008/04/image7.png" target="_blank"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="123" alt="image" src="http://elegantcode.com/wp-content/uploads/2008/04/image-thumb7.png" width="244" border="0"></a> 
<p>To instantiate our HierarchyData adapter objects we will use an extension method and linq 'Select' to convert our datasource IEnumerable&lt;T&gt; of IModelWithHierarchy&lt;T&gt; 's to an IHierarchicalEnumerable of IHierarchyData. 
<div><pre class="csharpcode"><span class="kwrd">namespace</span> POC.Web.HierarchyExtensions
{
    <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">class</span> HierarchyConverter
    {
        <span class="kwrd">public</span> <span class="kwrd">static</span> HierarchicalModelList
            ToHierarchicalModelList&lt;T&gt;(<span class="kwrd">this</span> IEnumerable&lt;T&gt; modelWithHierarchy)
            <span class="kwrd">where</span> T : IModelWithHierarchy&lt;T&gt;
        {
            <span class="kwrd">return</span> <span class="kwrd">new</span> HierarchicalModelList(
                modelWithHierarchy.Select(m =&gt; <span class="kwrd">new</span> HierarchyData&lt;T&gt;(m) <span class="kwrd">as</span> IHierarchyData));
        }
    }
}</pre></div>
<p>I am not going to bore you with the implementation details of the HierarachyData&lt;T&gt; adapter and the .net abstractions, but have dropped the <a href="http://elegantcode.com/wp-content/uploads/2008/04/hierarchydatasourcepoc.zip" target="_blank">source code here</a> if your interested. The end result: 
<div><pre class="csharpcode">TreeView1.DataSource = <span class="kwrd">new</span> HierarchicalModelDataSource&lt;Category&gt; { DataSource = Categories };
TreeView1.DataBind();
TreeView1.CollapseAll();</pre></div>
<p>Story complete!&nbsp; </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is Elegant Code to me?</title>
		<link>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-is-elegant-code-to-me</link>
		<comments>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/#comments</comments>
		<pubDate>Mon, 31 Mar 2008 03:47:45 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Patterns and Practices]]></category>
		<category><![CDATA[Widgets of Wisdom]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/</guid>
		<description><![CDATA[This question keeps popping up around here ("around here" being the loose conglomeration that makes up the Elegant Code group).&#160; It isn't easy to describe.&#160; And really, the notion of what constitutes elegance in code changes over time.&#160; There is no static "this is good code" test, and I doubt there ever will be.&#160; Plus, [...]]]></description>
			<content:encoded><![CDATA[<p>This question keeps popping up around here ("around here" being the loose conglomeration that makes up the Elegant Code group).&nbsp; It isn't easy to describe.&nbsp; And really, the notion of what constitutes elegance in code changes over time.&nbsp; There is no static "this is good code" test, and I doubt there ever will be.&nbsp; Plus, what makes good code in one language, may not apply to the next.</p> <p>So let me state for the record: today's elegant code is tomorrow's drivel.&nbsp; Don't feel bad, many writers have the same problems.&nbsp; What was super amazing back in the day is now rubbish or near unreadable.&nbsp; I am thinking of the Victorian writing that Hemingway usurped, and now Hemingway himself is almost unreadable (to me anyway).&nbsp; Tastes change with the times.&nbsp; That is a simple fact.</p> <p>So what can you do about this?&nbsp; As I say that old writing sucks to read (read Washington Irvine lately?), great works of literature still abound (for instance, Hemingway is still a great writer -- even if I prefer Tolkien) .&nbsp; So take some notes from them, and from other crafts in looking for the answer.&nbsp; If you want a cliff notes version of what this is going to tell you, it is this: you must always push yourself to get better.</p> <ol> <li>Find a good teacher.&nbsp; Nothing is better than sitting at the feet of a master who can nudge you along in the right direction.&nbsp; While early mistakes can often be corrected easily with a little bit of guidance, after they have had some time to fester all bets are off.&nbsp; I believe a fare number of "Anit-patterns" were created by self taught developers (find <a href="http://elegantcode.com/2008/03/21/sql-ejaculation/">SQL Ejaculation</a> on this site).&nbsp; <li>Read.&nbsp;&nbsp; Good writer are first good readers.&nbsp; Start with Code Complete, move into a good Patterns book, get MSDN Magazine, find some bloggers your like, but keep moving.&nbsp; You will NEVER be done reading.&nbsp; I imagine that even Martin Fowler has a "to read" booklist a mile long.  <li>Read code.&nbsp;&nbsp; There are a plethora of open source project just waiting to be read -- do so.&nbsp; This is the single best way to expand your coding repertoire, find things your language can do that you didn't even know about..&nbsp;&nbsp; Scott Hanselman has recently popularized this idea, and I thank him for it.  <li>Practice.&nbsp; This means writing small applications at home.&nbsp; You get an idea that you want to try out -- do it.&nbsp; I don't mean you have to write finished applications, just have some exploring time.&nbsp; I remember talking with an 80 year old master woodworker (he lives down the street from me), who was telling me how many time he would practice making dovetail joints before he felt competent.&nbsp; It was years worth.  <li>Pay Attention.&nbsp; Being good at anything really amounts to that.&nbsp; And don't just pay attention when reading, writing, or talking about code.&nbsp; Inspiration come from everywhere, any good artist will tell you that.&nbsp; Pay attention when you cook, when you work on the car, when you are wood working, playing an instrument, whatever it is that you do.&nbsp; This will help you in the end, event if it is just learning the amount of dedication it really takes to become good at something.  <li>Beware of preferences.&nbsp; Any time I hear someone start a statement with "I prefer code to ..." you know things are going downhill.&nbsp; If you find someone who cares more about how your code is formatted then how it is written you have found yourself in a mess.&nbsp; More importantly, beware of them in yourself.&nbsp;&nbsp; Having code style standard is important, but it isn't worth loosing sleep over.&nbsp; This also pertains to inheritance, patterns, use of particular classes (e.g. always using the generic list class in C# when a Dictionary would be better).&nbsp; <li>No Sacred Cows. Believe no single source of information.&nbsp; This means not thinking that Microsoft, Sun, IBM, Richard Stallman, or anyone else will have all of the correct answers.&nbsp; Apply the scientific method and do some research, experiment, and question the authority.&nbsp;&nbsp; There should be no sacred cows in programming.  <li>Talk with others.&nbsp; Join a user group, show up every now and then, heckle the presenter, and -maybe- speak.&nbsp; You want a broad range of people who you can talk to and bounce ideas off of.&nbsp; This will help you from becoming that crazy guy in the corner who vehemently states that all your code should be in one file.&nbsp; Having a mentor (mentioned earlier) is great, but having people around to push you from multiple directions is also good.  <li>Learn languages.&nbsp; This gets back to the "read code" idea. Make that multiple types of languages as well.&nbsp; If you know C# or Java and SQL, learn Python or Ruby, get really good at JavaScript.&nbsp; If you want something really out there, learn MDX.&nbsp; This is also a repertoire thing.&nbsp; Seeing how other languages work will make you rethink your ideas about what your current code, in whatever languages you are working in, should look like.  <li>Keep Thinking!!!&nbsp; That is possibly the most important point.&nbsp; Keep thinking.&nbsp; Don't just evaluate something once and never return, go back and reevaluate. Did the technique work?&nbsp; How could it have been better?&nbsp;&nbsp; The idea is continual improvement.  <li>Switch jobs every now and then.&nbsp; When I announced I was leaving my first programming job out of college, the VP of R&amp;D had me sit down with him and talk.&nbsp; He wasn't trying to keep me (he knew I was moving to be closer to family), he wanted to give me some advice.&nbsp; He told me to change jobs every three years.&nbsp; After you have been with a place for three years you have probably learned everything you are going to learn and it time to move on.&nbsp; This doesn't mean changing companies either.&nbsp; I've been with the same company now for four years, but I've had three different jobs.&nbsp; If you have been writing commercial applications, become a consultant - or vise-versa, become a test engineer, expand your boundaries.&nbsp; Again, this is about pushing yourself to be better.  <li>Have a Hobby. But don't ask about me, I have too many.&nbsp;&nbsp; Creating software is about creating things.&nbsp; So I suggest picking a hobby that involves creating something.&nbsp; Popular hobbies amongst programmers I know include: cooking, music, woodworking, beer making, and BBQ (which is different than cooking).&nbsp; But don't discount sports (golf is always popular), computer games, and board games.&nbsp; Anything that promotes the development of skill levels can't be a bad thing. </li></ol> <p>And I'm stopping there.&nbsp; This is my beginner's guide to how to become the writer elegant code.&nbsp; If you have others you think I missed, add them too the comments.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>LINQ Framework Design Guidelines</title>
		<link>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=linq-framework-design-guidelines-2</link>
		<comments>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/#comments</comments>
		<pubDate>Wed, 19 Mar 2008 01:53:22 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[Microsoft]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/</guid>
		<description><![CDATA[Excellent article on design considerations for LINQ. Looks like these concepts will be incorporated into the 2nd Edition of Framework Design Guidelines: Conventions, Idioms, and Patterns for Reuseable .NET Libraries. Cant wait to get my hands on a copy of that!]]></description>
			<content:encoded><![CDATA[<p>Excellent article on <a href="http://blogs.msdn.com/mirceat/archive/2008/03/13/linq-framework-design-guidelines.aspx" target="_blank">design considerations for LINQ</a>. Looks like these concepts will be incorporated into the 2nd Edition of <a href="http://www.amazon.com/Framework-Design-Guidelines-Conventions-Development/dp/0321545613/ref=pd_bbs_sr_2?ie=UTF8&amp;s=books&amp;qid=1200508590&amp;sr=1-2" target="_blank">Framework Design Guidelines: Conventions, Idioms, and Patterns for Reuseable .NET Libraries</a>. Cant wait to get my hands on a copy of that!</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Boise Code Camp 2008</title>
		<link>http://elegantcode.com/2008/03/09/boise-code-camp-2008/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=boise-code-camp-2008</link>
		<comments>http://elegantcode.com/2008/03/09/boise-code-camp-2008/#comments</comments>
		<pubDate>Mon, 10 Mar 2008 02:25:35 +0000</pubDate>
		<dc:creator>Alex Mueller</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Code Camp]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Patterns and Practices]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/09/boise-code-camp-2008/</guid>
		<description><![CDATA[Boise Code Camp 2008 was awesome. Thank you to David Starr and his wife, Eleanor, for taking ownership of this event and dedicating many months of their time to make this a reality. Thank you to the presenters for having the passion and desire to present technologies and practices, both new and not-as-new. Thank you [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Boise Code Camp 2008" href="http://www.boisecodecamp.org/" target="_blank">Boise Code Camp 2008</a> was awesome. Thank you to <a title="David Starr" href="http://elegantcode.com/about/david-starr/" target="_blank">David Starr</a> and his wife, <a title="Domestic Oblivion" href="http://domesticoblivion.com/" target="_blank">Eleanor</a>, for taking ownership of this event and dedicating many months of their time to make this a reality. Thank you to the presenters for having the passion and desire to present technologies and practices, both new and not-as-new. Thank you to the many volunteers who put in countless hours to help coordinate the weekend's events. Thank you to the campers for attending and making this year's code camp a success.</p>  <p>I am sure everyone will be blogging about all the great sessions they attended at code camp. I enjoyed all of the presentations I attended, and I am excited to explore some new material. </p>  <p>I want to share a few thoughts on my presentation dealing with Model-View-Presenter in ASP.NET after listening to <a title="Scott Hanselman" href="http://www.hanselman.com" target="_blank">Scott Hanselman's</a> session on the <a title="ASP. NET MVC Project" href="http://www.asp.net/mvc" target="_blank">ASP.NET MVC Project</a>. I may have the percentage incorrect, but Scott mentioned something like it is predicted that only 10% of the ASP.NET community currently using Web Forms will adopt and use the MVC framework. The MVC framework is an addition to ASP.NET, it is not a replacement. What this means is that Model-View-Presenter will still be a viable pattern to implement with your ASP.NET applications, and it is not going away. ASP.NET Web Forms will not be going away either.</p>  <p>Use MVP to get your third party controls under test. Use MVP to provide that separation of concerns in your legacy applications. Use it entirely or in conjunction with the MVC framework. It is all about testability. <a title="Glenn Block" href="http://blogs.msdn.com/gblock/" target="_blank">Glenn Block</a> presented on the Web Client Software Factory, and what pattern does this implement? Model-View-Presenter.</p>  <p>I am providing my <a title="Presentation and Source Code" href="http://muellerdesigns.net/DasBlog/content/binary/ModeViewPresenter.AlexMueller.BoiseCodeCamp2008.rar" target="_blank">presentation and source code (3.67 MB)</a> from my talk on MVP. It will be available via the <a title="Boise Code Camp" href="http://www.boisecodecamp.org/" target="_blank">Boise Code Camp</a> site as well. </p>  <p>Again, thank the many individuals and their families who sacrificed their time to bring to the local community this years code camp.</p>  <p>Around the office's water cooler on Monday, I will be able to say, &quot;and this one time, at code camp...&quot;</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/09/boise-code-camp-2008/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Microsoft releases Unity, an IoC library</title>
		<link>http://elegantcode.com/2008/04/26/test-data-builders-refined/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=test-data-builders-refined</link>
		<comments>http://elegantcode.com/2008/04/26/test-data-builders-refined/#comments</comments>
		<pubDate>Sat, 26 Apr 2008 23:08:27 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/26/test-data-builders-refined/</guid>
		<description><![CDATA[Last year, I blogged about Test Data Builders here and here. I still use them heavily in my unit tests for creating objects with test data. Heck, I also use this pattern for fluent interfaces in production code. Here is a simple example of this approach: &#160; 1: public class CustomerBuilder 2: { 3: public [...]]]></description>
			<content:encoded><![CDATA[<p>Last year, I blogged about Test Data Builders <a href="http://vanryswyckjan.blogspot.com/2007/09/test-data-builder-versus-object-mother.html" target="_blank">here</a> and <a href="http://vanryswyckjan.blogspot.com/2007/12/test-data-builder.html" target="_blank">here</a>. I still use them heavily in my unit tests for creating objects with test data. Heck, I also use this pattern for fluent interfaces in production code. Here is a simple example of this approach:</p>  <p>&#160;</p>  <div>   <div class="csharpcode">     <pre class="alteven"><span class="lnum">   1:</span> <span class="kwrd">public</span> <span class="kwrd">class</span> CustomerBuilder</pre>

    <pre class="alteven"><span class="lnum">   2:</span> {</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     <span class="kwrd">public</span> String _firstName = <span class="str">&quot;Homer&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     <span class="kwrd">public</span> String _lastName = <span class="str">&quot;Simpson&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   5:</span>         </pre>

    <pre class="alteven"><span class="lnum">   6:</span>     <span class="kwrd">public</span> CustomerBuilder WithFirstName(String firstName)</pre>

    <pre class="alteven"><span class="lnum">   7:</span>     {</pre>

    <pre class="alteven"><span class="lnum">   8:</span>         _firstName = firstName;</pre>

    <pre class="alteven"><span class="lnum">   9:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;    </pre>

    <pre class="alteven"><span class="lnum">  10:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  11:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  12:</span>     <span class="kwrd">public</span> CustomerBuilder WithLastName(String lastName)</pre>

    <pre class="alteven"><span class="lnum">  13:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  14:</span>         _lastName = lastName;</pre>

    <pre class="alteven"><span class="lnum">  15:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven"><span class="lnum">  16:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  17:</span>         </pre>

    <pre class="alteven"><span class="lnum">  18:</span>     <span class="kwrd">public</span> Customer Build()</pre>

    <pre class="alteven"><span class="lnum">  19:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  20:</span>         <span class="kwrd">return</span> <span class="kwrd">new</span> Customer(_firstName, _lastName);    </pre>

    <pre class="alteven"><span class="lnum">  21:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  22:</span> }</pre>
  </div>
</div>

<pre class="alteven">This fluent builder class can then be used this way:</pre>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> Customer customer = <span class="kwrd">new</span> CustomerBuilder()</pre>

    <pre class="alteven"><span class="lnum">   2:</span>     .WithFirstName(<span class="str">&quot;Homer&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     .WithLastName(<span class="str">&quot;Simpson&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     .Build();</pre>
  </div>
</div>

<p>A while ago, <a href="http://codebetter.com/blogs/gregyoung/default.aspx" target="_blank">Greg Young</a> started a series of blog posts on DDDD (Distributed Domain-Driven Design), which I can highly recommend. Make sure to catch up now you still can because I think that he has a lot of stuff coming up, which I'm really looking forward to.</p>

<p>Anyhow, Greg had a couple of posts on fluent builders, which you can read <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/15/dddd-5-messages-have-fluent-builders.aspx" target="_blank">here</a>, <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/16/dddd-6-fluent-builders-alternate-ending.aspx" target="_blank">here</a> and <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/22/dddd-8-fluent-builders-and-tests.aspx" target="_blank">here</a>. I noticed an interesting approach in the way that the target object is built. Here is an example of this approach:</p>

<p>&#160;</p>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> <span class="kwrd">public</span> <span class="kwrd">class</span> CustomerBuilder</pre>

    <pre class="alteven"><span class="lnum">   2:</span> {</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     <span class="kwrd">public</span> String _firstName = <span class="str">&quot;Homer&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     <span class="kwrd">public</span> String _lastName = <span class="str">&quot;Simpson&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   5:</span>         </pre>

    <pre class="alteven"><span class="lnum">   6:</span>     <span class="kwrd">public</span> CustomerBuilder WithFirstName(String firstName)</pre>

    <pre class="alteven"><span class="lnum">   7:</span>     {</pre>

    <pre class="alteven"><span class="lnum">   8:</span>         _firstName = firstName;</pre>

    <pre class="alteven"><span class="lnum">   9:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;    </pre>

    <pre class="alteven"><span class="lnum">  10:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  11:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  12:</span>     <span class="kwrd">public</span> CustomerBuilder WithLastName(String lastName)</pre>

    <pre class="alteven"><span class="lnum">  13:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  14:</span>         _lastName = lastName;</pre>

    <pre class="alteven"><span class="lnum">  15:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven"><span class="lnum">  16:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  17:</span>         </pre>

    <pre class="alteven"><span class="lnum">  18:</span>     <span class="kwrd">public</span> Customer Build()</pre>

    <pre class="alteven"><span class="lnum">  19:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  20:</span>         <span class="kwrd">return</span> <span class="kwrd">new</span> Customer(_firstName, _lastName);    </pre>

    <pre class="alteven"><span class="lnum">  21:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  22:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  23:</span>     <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">implicit</span> <span class="kwrd">operator</span> Customer(</pre>

    <pre class="alteven"><span class="lnum">  24:</span>         CustomerBuilder builder) </pre>

    <pre class="alteven"><span class="lnum">  25:</span>     {  </pre>

    <pre class="alteven"><span class="lnum">  26:</span>         <span class="kwrd">return</span> builder.Build()    </pre>

    <pre class="alteven"><span class="lnum">  27:</span>     } </pre>

    <pre class="alteven"><span class="lnum">  28:</span> }</pre>
  </div>
</div>

<p>which results in the following usage:</p>

<p>&#160;</p>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> Customer customer = <span class="kwrd">new</span> CustomerBuilder()</pre>

    <pre class="alteven"><span class="lnum">   2:</span>     .WithFirstName(<span class="str">&quot;Homer&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     .WithLastName(<span class="str">&quot;Simpson&quot;</span>);</pre>
  </div>

  <br />Adding an implicit cast operator to the builder class makes that its no longer required to explicitly call the <em>Build</em> method. I keep the <em>Build</em> method around for backwards-compatibility reasons or in case I ever need it again (violating YAGNI in the process, I know, I know). I find that adding the implicit cast operator adds to the readability of the fluent interface, don't you agree?</div>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/26/test-data-builders-refined/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; Design Patterns</title>
	<atom:link href="http://elegantcode.com/tag/design-patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Where to start a new program</title>
		<link>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=where-to-start-a-new-program</link>
		<comments>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/#comments</comments>
		<pubDate>Sun, 08 Jun 2008 19:50:39 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/06/08/where-to-start-a-new-program/</guid>
		<description><![CDATA[Here as an interesting question that popped up at my user group meeting last night, when you are starting a new program (green field development), where do you get started?&#160; I find these questions interesting because there really is no correct answer, but a persons answer will tell you something about how they problem solve. [...]]]></description>
			<content:encoded><![CDATA[<p>Here as an interesting question that popped up at my user group meeting last night, when you are starting a new program (green field development), where do you get started?&nbsp; I find these questions interesting because there really is no correct answer, but a persons answer will tell you something about how they problem solve.</p> <p>Depending on who you ask you will likely get one of two answers.&nbsp; If you are talking with a data guy, he will say to start with the database.&nbsp; If you are dealing with a visual guy, he will say to start with the UI.&nbsp; Historically, I did a conglomeration of those two ideas, creating both the UI and the database at the same time, letting the two drive each other.&nbsp; But I'm weird, I'm a visual/data guy.</p> <p>Lately though, I've started to thing about a third approach.&nbsp; Starting in the middle.&nbsp; No UI, no database, just the data classes.&nbsp; This approach has been spurred on buy a couple of things.&nbsp; Namely, test driven development (TDD), design patterns, and domain driven design.</p> <p>TDD really is the glue that allows all of this to happen.&nbsp; I can start in the middle because I can run the code in the middle any time I want.&nbsp; I just have to write a test.&nbsp; No having to wade through multiple layers of UI to test out some small chunk ok UI is a great productivity gain for me.</p> <p>Next is design patterns.&nbsp; This is what gives you a reference for how to approach your code.&nbsp; That said, this isn't about making everything absolutely perfect from the get go, but it is nice to know where you are going to made your comprises early on.</p> <p>But all of that does not explain where I begin.&nbsp; That is where Domain Driven Design (DDD) kicks in.&nbsp; For those not familiar with the concept, you can read up on <a href="http://en.wikipedia.org/wiki/Domain-driven_design">wikipedia</a>, buy <a href="http://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1212806974&amp;sr=8-1">The Book</a>, or buy one of the <a href="http://www.amazon.com/s?ie=UTF8&amp;tag=mozilla-20&amp;index=blended&amp;link%5Fcode=qs&amp;field-keywords=domain%20driven%20design&amp;sourceid=Mozilla-search">various other references</a>.&nbsp;&nbsp; One of the concepts in DDD (I'm still reading the book) is setting up something called a <a href="http://www.domaindrivendesign.org/discussion/messageboardarchive/UbiquitousLanguage.html">ubiquitous language</a>.</p> <p>The ubiquitous language is actually trying to solve a problem that I have seen happen on many projects I've been apart of (but not always my fault).&nbsp; At some point there is a linguistic difference between the nouns that the customer uses and the nouns that the programmers use.&nbsp; By focusing on the language first, you are attempting to head that off at the pass.</p> <p>These nouns consist of your domain classes and your tables and columns, they move everywhere...but not always consistently.&nbsp; Consistency does matter.&nbsp; Inconsistency is what leads to rather confusing conversations with people and everyone scratching their heads about "what just happened".&nbsp; So, this is the middle that I start with.</p> <p>But before you start...</p> <p>One of the other guys in the group was an actual data guy.&nbsp; The systems he has written worked with more data than yours does.&nbsp; Trust me on that.&nbsp; Historically, his biggest consideration was data input speed.&nbsp; I don't think I would start in the middle if that was my biggest concern.&nbsp; In that case you do start with the database, you tune for speed.&nbsp; You also don't use as many domain classes, and you tend to throw away your ORM solution and just hit the database layer directly via Ado.Net.</p> <p>But lets face it, that doesn't happen very often.&nbsp; Most business applications aren't going to touch what the architecture can take.&nbsp; It is amazing how many horribly written applications with very inefficient code run just fine.&nbsp; Most of my speed issues could be solved by indexing tables and changing generic lists to generic dictionaries (more on that later).&nbsp; Architect your applications for how they are going to be used.&nbsp; Most of my web applications have a max usage of 100 simultaneous users.&nbsp; If the data access is slightly slower I'll take that hit for programmer efficiency.&nbsp; If you are designing Amazon.com, you code for through-put.</p> <p>OK, someone is going to disagree with this...I can already feel it.&nbsp; Please take a moment to register your displeasure in the comments.&nbsp; I would love to hear it.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Learning Design Patterns</title>
		<link>http://elegantcode.com/2008/04/28/learning-design-patterns/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=learning-design-patterns</link>
		<comments>http://elegantcode.com/2008/04/28/learning-design-patterns/#comments</comments>
		<pubDate>Mon, 28 Apr 2008 19:01:17 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/28/learning-design-patterns/</guid>
		<description><![CDATA[To me this seems like an interesting way to learn about design patterns (watch the video). I wonder if it also works when I hit an ivory-tower architect with POEAA or DDD? I'll keep you posted. PS: I'm really looking forward to the next episode on code reviews and retrospectives.]]></description>
			<content:encoded><![CDATA[To me <a href="http://codebetter.com/blogs/glenn.block/archive/2008/04/28/extreme-patterns-with-oren.aspx" target="_blank">this</a> seems like an interesting way to learn about design patterns (watch the video). I wonder if it also works when I hit an ivory-tower architect with <a href="http://www.bookpool.com/sm/0321127420" target="_blank">POEAA</a> or <a href="http://www.bookpool.com/sm/0321125215" target="_blank">DDD</a>? I'll keep you posted.

PS: I'm really looking forward to the next episode on code reviews and retrospectives.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/28/learning-design-patterns/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>DTO&#8217;s or Serialized Domain Entities?</title>
		<link>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=dtos-or-serialized-domain-entities</link>
		<comments>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/#comments</comments>
		<pubDate>Sun, 27 Apr 2008 08:09:02 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/</guid>
		<description><![CDATA[I hear this question come up time and time again. It is something that I have been dealing with for a while now, and have some experience with doing both ways. I started writing this in response to a DDD thread, but it got rather large and so I turned it into a post. So [...]]]></description>
			<content:encoded><![CDATA[<p>I hear this question come up time and time again. It is something that I have been dealing with for a while now, and have some experience with doing both ways. I started writing this in response to a <a href="http://tech.groups.yahoo.com/group/domaindrivendesign/message/7286" target="_blank">DDD thread</a>, but it got rather large and so I turned it into a post. So here ya go!  <p>I think you have to write DTO's for any sort of long term Enterprise 'system of systems' solution. I fought this for some time, but since moving to an Assembler/DTO I much enjoy the freedom of being able to easily tweak my DTO "snapshot" of the aggregate root I am working with. Maintaining them is very little overhead.  <p>I tend to think of the DTO as a message, a value object. This is necessary because once an entity leaves the boundary it can no longer function within its original meaning. Its methods may be intended to only run within a given context, which is not the same once on another tier. (Things like lazy loading are a good example) Once the DTO/Message arrives to the caller, it very well may (and probably should) be translated/assembled into another model which can represent its new semantics.  <p>Some other advantages of DTO w/ Assembler or Builder:<br>-Can explicitly control the depth of the graph/tree you are serializing.<br>-Can flatten out the data where needed<br>-Can return significantly less data (maybe you only need 3 fields instead of 15)<br>-Not tightly coupled between remote boundaries (this could be a mess when working with a large team)<br>-Interoperable  <p>I wrote a post a while back showing a way to '<a href="http://elegantcode.com/2007/12/05/fun-with-generics-from-repository-to-dto/" target="_blank">templatize</a>' the process. My current implementations are much different with LINQ now, but maybe it could stimulate some ideas for whomever.  <p><em>The next thing I would like to do is remove the assembler altogether. By flagging fields with attributes or using config to specify a mapping, I could then feed <a href="http://blogs.msdn.com/meek/archive/2008/04/25/using-linq-expressions-to-generate-dynamic-methods.aspx" target="_blank">dynamic methods</a> and run a translation/builder strategy.</em>  <p>I know there are concerns about writing and maintaining assembler and DTO, but to me this is the least of the worry, a more challenging scenario would be the UnitOfWork (How to you intended to track remote state, concurrency etc) How about versioning? performance? scala.. well ilities in general?  <p>These kinds of problems lead one from Request/Response, Patterns of Enterprise Architecture &amp; DDD to a Pub/Sub Message Bus - Enterprise Integration Patterns &amp; DDDD.  <p>And to think all you want to do is send some frickin entities to a remote client&nbsp; :)</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Test Data Builders Refined</title>
		<link>http://elegantcode.com/2008/04/26/test-data-builders-refined/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=test-data-builders-refined</link>
		<comments>http://elegantcode.com/2008/04/26/test-data-builders-refined/#comments</comments>
		<pubDate>Sat, 26 Apr 2008 23:08:27 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/26/test-data-builders-refined/</guid>
		<description><![CDATA[Last year, I blogged about Test Data Builders here and here. I still use them heavily in my unit tests for creating objects with test data. Heck, I also use this pattern for fluent interfaces in production code. Here is a simple example of this approach: &#160; 1: public class CustomerBuilder 2: { 3: public [...]]]></description>
			<content:encoded><![CDATA[<p>Last year, I blogged about Test Data Builders <a href="http://vanryswyckjan.blogspot.com/2007/09/test-data-builder-versus-object-mother.html" target="_blank">here</a> and <a href="http://vanryswyckjan.blogspot.com/2007/12/test-data-builder.html" target="_blank">here</a>. I still use them heavily in my unit tests for creating objects with test data. Heck, I also use this pattern for fluent interfaces in production code. Here is a simple example of this approach:</p>  <p>&#160;</p>  <div>   <div class="csharpcode">     <pre class="alteven"><span class="lnum">   1:</span> <span class="kwrd">public</span> <span class="kwrd">class</span> CustomerBuilder</pre>

    <pre class="alteven"><span class="lnum">   2:</span> {</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     <span class="kwrd">public</span> String _firstName = <span class="str">&quot;Homer&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     <span class="kwrd">public</span> String _lastName = <span class="str">&quot;Simpson&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   5:</span>         </pre>

    <pre class="alteven"><span class="lnum">   6:</span>     <span class="kwrd">public</span> CustomerBuilder WithFirstName(String firstName)</pre>

    <pre class="alteven"><span class="lnum">   7:</span>     {</pre>

    <pre class="alteven"><span class="lnum">   8:</span>         _firstName = firstName;</pre>

    <pre class="alteven"><span class="lnum">   9:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;    </pre>

    <pre class="alteven"><span class="lnum">  10:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  11:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  12:</span>     <span class="kwrd">public</span> CustomerBuilder WithLastName(String lastName)</pre>

    <pre class="alteven"><span class="lnum">  13:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  14:</span>         _lastName = lastName;</pre>

    <pre class="alteven"><span class="lnum">  15:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven"><span class="lnum">  16:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  17:</span>         </pre>

    <pre class="alteven"><span class="lnum">  18:</span>     <span class="kwrd">public</span> Customer Build()</pre>

    <pre class="alteven"><span class="lnum">  19:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  20:</span>         <span class="kwrd">return</span> <span class="kwrd">new</span> Customer(_firstName, _lastName);    </pre>

    <pre class="alteven"><span class="lnum">  21:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  22:</span> }</pre>
  </div>
</div>

<pre class="alteven">This fluent builder class can then be used this way:</pre>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> Customer customer = <span class="kwrd">new</span> CustomerBuilder()</pre>

    <pre class="alteven"><span class="lnum">   2:</span>     .WithFirstName(<span class="str">&quot;Homer&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     .WithLastName(<span class="str">&quot;Simpson&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     .Build();</pre>
  </div>
</div>

<p>A while ago, <a href="http://codebetter.com/blogs/gregyoung/default.aspx" target="_blank">Greg Young</a> started a series of blog posts on DDDD (Distributed Domain-Driven Design), which I can highly recommend. Make sure to catch up now you still can because I think that he has a lot of stuff coming up, which I'm really looking forward to.</p>

<p>Anyhow, Greg had a couple of posts on fluent builders, which you can read <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/15/dddd-5-messages-have-fluent-builders.aspx" target="_blank">here</a>, <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/16/dddd-6-fluent-builders-alternate-ending.aspx" target="_blank">here</a> and <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/22/dddd-8-fluent-builders-and-tests.aspx" target="_blank">here</a>. I noticed an interesting approach in the way that the target object is built. Here is an example of this approach:</p>

<p>&#160;</p>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> <span class="kwrd">public</span> <span class="kwrd">class</span> CustomerBuilder</pre>

    <pre class="alteven"><span class="lnum">   2:</span> {</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     <span class="kwrd">public</span> String _firstName = <span class="str">&quot;Homer&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     <span class="kwrd">public</span> String _lastName = <span class="str">&quot;Simpson&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   5:</span>         </pre>

    <pre class="alteven"><span class="lnum">   6:</span>     <span class="kwrd">public</span> CustomerBuilder WithFirstName(String firstName)</pre>

    <pre class="alteven"><span class="lnum">   7:</span>     {</pre>

    <pre class="alteven"><span class="lnum">   8:</span>         _firstName = firstName;</pre>

    <pre class="alteven"><span class="lnum">   9:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;    </pre>

    <pre class="alteven"><span class="lnum">  10:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  11:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  12:</span>     <span class="kwrd">public</span> CustomerBuilder WithLastName(String lastName)</pre>

    <pre class="alteven"><span class="lnum">  13:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  14:</span>         _lastName = lastName;</pre>

    <pre class="alteven"><span class="lnum">  15:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven"><span class="lnum">  16:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  17:</span>         </pre>

    <pre class="alteven"><span class="lnum">  18:</span>     <span class="kwrd">public</span> Customer Build()</pre>

    <pre class="alteven"><span class="lnum">  19:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  20:</span>         <span class="kwrd">return</span> <span class="kwrd">new</span> Customer(_firstName, _lastName);    </pre>

    <pre class="alteven"><span class="lnum">  21:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  22:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  23:</span>     <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">implicit</span> <span class="kwrd">operator</span> Customer(</pre>

    <pre class="alteven"><span class="lnum">  24:</span>         CustomerBuilder builder) </pre>

    <pre class="alteven"><span class="lnum">  25:</span>     {  </pre>

    <pre class="alteven"><span class="lnum">  26:</span>         <span class="kwrd">return</span> builder.Build()    </pre>

    <pre class="alteven"><span class="lnum">  27:</span>     } </pre>

    <pre class="alteven"><span class="lnum">  28:</span> }</pre>
  </div>
</div>

<p>which results in the following usage:</p>

<p>&#160;</p>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> Customer customer = <span class="kwrd">new</span> CustomerBuilder()</pre>

    <pre class="alteven"><span class="lnum">   2:</span>     .WithFirstName(<span class="str">&quot;Homer&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     .WithLastName(<span class="str">&quot;Simpson&quot;</span>);</pre>
  </div>

  <br />Adding an implicit cast operator to the builder class makes that its no longer required to explicitly call the <em>Build</em> method. I keep the <em>Build</em> method around for backwards-compatibility reasons or in case I ever need it again (violating YAGNI in the process, I know, I know). I find that adding the implicit cast operator adds to the readability of the fluent interface, don't you agree?</div>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/26/test-data-builders-refined/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Name Those Patterns</title>
		<link>http://elegantcode.com/2008/04/24/name-those-patterns/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=name-those-patterns</link>
		<comments>http://elegantcode.com/2008/04/24/name-those-patterns/#comments</comments>
		<pubDate>Thu, 24 Apr 2008 16:16:13 +0000</pubDate>
		<dc:creator>Tony Rasa</dc:creator>
				<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/24/name-those-patterns/</guid>
		<description><![CDATA[In yesterday's discussion of Gang of Four Design Patterns, I said &#34;Never name your classes after the pattern itself.&#34;&#160; But then someone pointed out &#34;What about Factory classes?&#34;&#160; &#34;Oh, ok, ALWAYS name classes after the pattern.&#34;&#160; Except for Singleton or Fa&#231;ade, and maybe a few others.&#160; Ok then, NEVER name after the pattern, except when [...]]]></description>
			<content:encoded><![CDATA[<p>In yesterday's discussion of Gang of Four Design Patterns, I said &quot;Never name your classes after the pattern itself.&quot;&#160; But then someone pointed out &quot;What about Factory classes?&quot;&#160; &quot;Oh, ok, ALWAYS name classes after the pattern.&quot;&#160; Except for Singleton or Fa&#231;ade, and maybe a few others.&#160; Ok then, NEVER name after the pattern, except when you do, sometimes, and then ALWAYS name those, unless otherwise noted.</p>  <p>So, here's a list of the 23 Gang of Four patterns, along with when I'd use the name of the pattern in the implementation.&#160; Unless I didn't want to, and then I wouldn't.&#160; Feel free to agree with this list, unless you don't care to.</p>  <h3>Creational Patterns</h3>  <p><strong>Abstract Factory</strong> - After consideration, I have to say that I call my factories Factory, unless they're Repositories, Data Access Layers, DAOs, or something else.&#160; But most of the time, Factory.&#160; Because this would just be wrong:</p>  <pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> FooCreatorMakerThingy { ... }</pre>

<p><strong>Builder</strong> - Hmm, another noun. Guess I'd use this one too, for the same reason as Abstract Factory. Most of the time.&#160; </p>

<p><strong>Factory Method</strong> - I probably would not call my Factory Methods &quot;GetFooFactoryMethod()&quot; because that just looks weird.&#160;&#160; Maybe if we verbify the noun:</p>

<pre class="csharpcode"><span class="kwrd">public</span> Foo FactorialMethodizeFoo() {...}</pre>

<p>Nope, that's still dumb.</p>

<p><strong>Prototype</strong> - &quot;Specify the kinds of objects to create using a prototypical instance...&quot; So we don't care that the class is a &quot;Prototype,&quot; but we do care about the &quot;kind of object&quot; part.&#160; So don't name your prototypical classes &quot;FooPrototype.&quot;&#160; (Unless you just want to.)</p>

<p><strong>Singleton</strong> - Somebody have a case where it makes sense to name your Singleton &quot;Singleton&quot;?&#160; I can't come up with one.&#160; Anyway, you shouldn't be using singletons anymore anyway.&#160; :)</p>

<h3>Structural Patterns</h3>

<p><strong>Adapter</strong> - Adapters are pretty much called Adapters.&#160; Except when they're called Wrappers.&#160; So you can use this name too.</p>

<p><strong>Bridge</strong> - I wouldn't label a participant in a bridge pattern &quot;FooBridge,&quot; unless I was trying to impress on an interview test or something.&#160; And even then, that's a bad idea.&#160; &quot;Ohh, look, I know a complicated design pattern!&#160; Lookitme!&quot;&#160; So don't call your bridges Bridges.&#160; </p>

<p><strong>Composite</strong> - &quot;One of the goals of the Composite&#160; pattern is to make clients unaware of the specific Leaf or Composite classes they're using.&quot;&#160; So you don't want to point out that Composites are in play, that breaks the suspension of disbelief for your audience.&#160; Or something.&#160; Anyway Composites get used all over the place, if we had to label them all then it'd be FooComposite, BarComposite, TreeComposite, EverythingComposite, and what's the fun in that.</p>

<p><strong>Decorator </strong>- I think in general people don't label their Decorators Decorators.&#160; I'm sure there's a counter-example to this, but all of the Stream- and Xml-handling classes I can think of don't point out that they are Decorators.&#160; You'd probably want to stick to more descriptive names, like ASCIIStreamReader instead of cluttering things up with ASCIIStreamDecorator.&#160; We're not decorating the stream (in a literal sense), we're reading it.&#160; </p>

<p><strong>Fa&#231;ade</strong> - Don't call your Fa&#231;ade &quot;FooFa&#231;ade,&quot; that's just silly.&#160; Besides, its too much work to type that cedille into the editor if you're using an ignorant American keyboard.</p>

<p><strong>Flyweight </strong>- This is another pattern where we're going to disguise what's going on inside, in order to simplify the client's usage and make better use of resources.&#160; No reason to flaunt to the client that we're low on resources, because that's a personal and private matter - times are tough all over, and there's no shame in struggling a little bit, and hey who could have predicted that the sub-prime loan market was going to ... anyway, back to patterns.&#160;&#160; Don't call your Flyweights Flyweights, unless what you're modeling is actually a Flyweight, and then thats OK.</p>

<p><strong>Proxy </strong>- This one is standard terminology.&#160; So Proxy's ok.</p>

<h3>Behavioral Patterns (or &quot;<em>MAN this book has a lot of pages in it..</em>&quot;)</h3>

<p><strong>Chain of Responsibility </strong>- Don't even think of naming a class ChainOfResponsibility.&#160; Even with ReSharper, who'd want to have to type that?</p>

<p><strong>Command</strong> - Another convenient noun.&#160; Ok, you can create Commands.&#160; </p>

<p><strong>Interpreter </strong>- This is another pattern that doesn't have one identifying class that you could label &quot;The Interpreter.&quot;&#160; But you might create a Fa&#231;ade, and call that class Interpreter.&#160; I'm not sure if naming one pattern after another is OK or not.&#160; Makes my head hurt.&#160; </p>

<p>Iterator - Another convenient noun.&#160; Iterate away!&#160; Not that you'll have to worry too much about naming classes with this pattern if you use C#, Java, C++ or pretty much any other modern language, since this pattern is built-in.</p>

<p><strong>Mediator </strong>- This pattern promotes loose coupling, most of the time I'd call what it does a &quot;Controller,&quot; or what's being Mediated is more on a service level, so perhaps the word &quot;Service&quot; would end up in the name.&#160; Can't say I've ever called a class specifically &quot;FooMediator&quot;, but I suppose I don't have a problem with it if you want to do that.</p>

<p><strong>Memento </strong>- This pattern captures and externalizes an object's state.&#160; So I think I'd call something like it &quot;State&quot; before &quot;Memento,&quot; since it's fewer letters to type.&#160; Besides, I had to see that movie twice before I really appreciated it.&#160; </p>

<p>Ok, that was just lame, sorry.</p>

<p><strong>Observer </strong>- Oh, you mean like an Event?&#160; Or more like a delegate?&#160; :)</p>

<p><strong>State </strong>- I'd call my implementation of this pattern &quot;Memento,&quot; just to confuse you.&#160; That's just how I roll.</p>

<p><strong>Strategy </strong>- This is another pattern that it seems like we use all the time, without having to give it a fancy Ivory Tower name.&#160; As someone pointed out (sorry I lost the reference to who said that) Microsoft calls all their Strategy implementations Providers now anyway.&#160; Rather than label something a &quot;Strategy&quot; I'd probably just say what the thing does: &quot;CryptographicProvider.&quot;</p>

<p><strong>Template Method</strong> - Yet another pattern where there's no obvious place to hang the name off of, so don't try to force it in there and impress your coworkers with how smart you are.&#160; We know, you're a Design Pattern God, no need to brag about it.</p>

<p><strong>Visitor </strong>- Hmm, not sure where I stand on this one.&#160; I have used this pattern where I've named a visitor &quot;SqlGeneratingVisitor.&quot;&#160; But maybe that was a mistake?&#160; So how about we just declare this one &quot;Free Parking,&quot; and you can do what you want.</p>

<p>&#160;</p>

<p>So, there you go.&#160; A bunch of design patterns, summarized into one neat page.&#160; Now you're ready to ace that interview!&#160; </p>

<p>&#160;</p>

<p><em>disclaimer: I make no guarantees that any or all of the information will not harm you in a technical interview, and if repeating this to an interview board gets your removed from the building by security, its not my fault.&#160; This information comes without warantees or conditions of any kind, and you probably shouldn't have bothered reading it..</em></p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/24/name-those-patterns/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.Net HierarchicalDataSource&lt;T&gt;</title>
		<link>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=aspnet-hierarchicaldatasourcet</link>
		<comments>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/#comments</comments>
		<pubDate>Sun, 06 Apr 2008 06:45:44 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Extension Methods]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/</guid>
		<description><![CDATA[The Asp.net 2.0 tree control, as well as many 3rd party controls binds to hierarchical data via an IHierarchicalDataSource interface. Asp.net provides an implementation OOTB with the HierarchicalDataSourceControl! Easy, let’s hook that up to our… wait this control only takes data in xml format or from a sitemap? Hmm, we don’t want to convert our [...]]]></description>
			<content:encoded><![CDATA[<p>The Asp.net 2.0 tree control, as well as many 3rd party controls binds to hierarchical data via an IHierarchicalDataSource interface. Asp.net provides an implementation OOTB with the HierarchicalDataSourceControl! Easy, let’s hook that up to our… wait this control only takes data in xml format or from a sitemap? Hmm, we don’t want to convert our model to xml, and a sitemap seems inappropriate. We spend a lot time doing great patterns such as MVP &amp; MVC, we should just be able to bind up a list of model objects we already have.  <p>Good thing MS has exposed a set of interfaces and abstract classes in System.Web.UI for us to implement our own IHierarchicalDataSource and HierarchicalDataSourceView and IHierarchicalEnumerable and then IHierarchyData... geesh.  <p>Data? So let's see here, in order to bind our model to the tree control it needs to implement an interface in System.Web.UI? Well that's just not going to happen, so its time for some Elegant Code.  <p><strong>Story:</strong>  <p>Dude, as a software developer, needs to create a custom implementation of IHierarchicalDataSource, and the rest of its parts, so that he can bind a list of objects to a tree control in asp.net  <ul> <li>Use existing model to bind to tree control  <li>Must be able to reuse data source for future models  <li>Model must not contain a reference to System.Web.UI </li></ul> <p>In order to setup our model to address the acceptance criteria we are going to use an interface with a self-referencing generic declaration, where the type will specify itself as the concrete type. This will let us create a nice generic hierarchical model to pass into our datasource without resorting to any sort of base class.  <div><pre class="csharpcode"><span class="kwrd">namespace</span> POC.Common
{
    <span class="kwrd">public</span> <span class="kwrd">interface</span> IModelWithHierarchy&lt;T&gt;
    {
        <span class="kwrd">string</span> Name { get; set; }
        T Parent { get; set; }
        List&lt;T&gt; Children { get; set; }
    }
}</pre><em>Lets note that we have placed the interface in a Common namespace which represents a shared.dll between Web &amp; Model</em> </div>
<p>The UI part of criteria is now a bit more tricky because the HierarchicalDataSourceView expects a IHierarchicalEnumerable datasource which contains IHierarchyData. Said IHierarchyData != IModelWithHierarchy&lt;T&gt;.&nbsp; Luckily someone else has already figured this one out for us. We need an <a href="http://www.dofactory.com/Patterns/PatternAdapter.aspx" target="_blank">adapter</a>. 
<p><a href="http://elegantcode.com/wp-content/uploads/2008/04/image7.png" target="_blank"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="123" alt="image" src="http://elegantcode.com/wp-content/uploads/2008/04/image-thumb7.png" width="244" border="0"></a> 
<p>To instantiate our HierarchyData adapter objects we will use an extension method and linq 'Select' to convert our datasource IEnumerable&lt;T&gt; of IModelWithHierarchy&lt;T&gt; 's to an IHierarchicalEnumerable of IHierarchyData. 
<div><pre class="csharpcode"><span class="kwrd">namespace</span> POC.Web.HierarchyExtensions
{
    <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">class</span> HierarchyConverter
    {
        <span class="kwrd">public</span> <span class="kwrd">static</span> HierarchicalModelList
            ToHierarchicalModelList&lt;T&gt;(<span class="kwrd">this</span> IEnumerable&lt;T&gt; modelWithHierarchy)
            <span class="kwrd">where</span> T : IModelWithHierarchy&lt;T&gt;
        {
            <span class="kwrd">return</span> <span class="kwrd">new</span> HierarchicalModelList(
                modelWithHierarchy.Select(m =&gt; <span class="kwrd">new</span> HierarchyData&lt;T&gt;(m) <span class="kwrd">as</span> IHierarchyData));
        }
    }
}</pre></div>
<p>I am not going to bore you with the implementation details of the HierarachyData&lt;T&gt; adapter and the .net abstractions, but have dropped the <a href="http://elegantcode.com/wp-content/uploads/2008/04/hierarchydatasourcepoc.zip" target="_blank">source code here</a> if your interested. The end result: 
<div><pre class="csharpcode">TreeView1.DataSource = <span class="kwrd">new</span> HierarchicalModelDataSource&lt;Category&gt; { DataSource = Categories };
TreeView1.DataBind();
TreeView1.CollapseAll();</pre></div>
<p>Story complete!&nbsp; </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is Elegant Code to me?</title>
		<link>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-is-elegant-code-to-me</link>
		<comments>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/#comments</comments>
		<pubDate>Mon, 31 Mar 2008 03:47:45 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Patterns and Practices]]></category>
		<category><![CDATA[Widgets of Wisdom]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/</guid>
		<description><![CDATA[This question keeps popping up around here ("around here" being the loose conglomeration that makes up the Elegant Code group).&#160; It isn't easy to describe.&#160; And really, the notion of what constitutes elegance in code changes over time.&#160; There is no static "this is good code" test, and I doubt there ever will be.&#160; Plus, [...]]]></description>
			<content:encoded><![CDATA[<p>This question keeps popping up around here ("around here" being the loose conglomeration that makes up the Elegant Code group).&nbsp; It isn't easy to describe.&nbsp; And really, the notion of what constitutes elegance in code changes over time.&nbsp; There is no static "this is good code" test, and I doubt there ever will be.&nbsp; Plus, what makes good code in one language, may not apply to the next.</p> <p>So let me state for the record: today's elegant code is tomorrow's drivel.&nbsp; Don't feel bad, many writers have the same problems.&nbsp; What was super amazing back in the day is now rubbish or near unreadable.&nbsp; I am thinking of the Victorian writing that Hemingway usurped, and now Hemingway himself is almost unreadable (to me anyway).&nbsp; Tastes change with the times.&nbsp; That is a simple fact.</p> <p>So what can you do about this?&nbsp; As I say that old writing sucks to read (read Washington Irvine lately?), great works of literature still abound (for instance, Hemingway is still a great writer -- even if I prefer Tolkien) .&nbsp; So take some notes from them, and from other crafts in looking for the answer.&nbsp; If you want a cliff notes version of what this is going to tell you, it is this: you must always push yourself to get better.</p> <ol> <li>Find a good teacher.&nbsp; Nothing is better than sitting at the feet of a master who can nudge you along in the right direction.&nbsp; While early mistakes can often be corrected easily with a little bit of guidance, after they have had some time to fester all bets are off.&nbsp; I believe a fare number of "Anit-patterns" were created by self taught developers (find <a href="http://elegantcode.com/2008/03/21/sql-ejaculation/">SQL Ejaculation</a> on this site).&nbsp; <li>Read.&nbsp;&nbsp; Good writer are first good readers.&nbsp; Start with Code Complete, move into a good Patterns book, get MSDN Magazine, find some bloggers your like, but keep moving.&nbsp; You will NEVER be done reading.&nbsp; I imagine that even Martin Fowler has a "to read" booklist a mile long.  <li>Read code.&nbsp;&nbsp; There are a plethora of open source project just waiting to be read -- do so.&nbsp; This is the single best way to expand your coding repertoire, find things your language can do that you didn't even know about..&nbsp;&nbsp; Scott Hanselman has recently popularized this idea, and I thank him for it.  <li>Practice.&nbsp; This means writing small applications at home.&nbsp; You get an idea that you want to try out -- do it.&nbsp; I don't mean you have to write finished applications, just have some exploring time.&nbsp; I remember talking with an 80 year old master woodworker (he lives down the street from me), who was telling me how many time he would practice making dovetail joints before he felt competent.&nbsp; It was years worth.  <li>Pay Attention.&nbsp; Being good at anything really amounts to that.&nbsp; And don't just pay attention when reading, writing, or talking about code.&nbsp; Inspiration come from everywhere, any good artist will tell you that.&nbsp; Pay attention when you cook, when you work on the car, when you are wood working, playing an instrument, whatever it is that you do.&nbsp; This will help you in the end, event if it is just learning the amount of dedication it really takes to become good at something.  <li>Beware of preferences.&nbsp; Any time I hear someone start a statement with "I prefer code to ..." you know things are going downhill.&nbsp; If you find someone who cares more about how your code is formatted then how it is written you have found yourself in a mess.&nbsp; More importantly, beware of them in yourself.&nbsp;&nbsp; Having code style standard is important, but it isn't worth loosing sleep over.&nbsp; This also pertains to inheritance, patterns, use of particular classes (e.g. always using the generic list class in C# when a Dictionary would be better).&nbsp; <li>No Sacred Cows. Believe no single source of information.&nbsp; This means not thinking that Microsoft, Sun, IBM, Richard Stallman, or anyone else will have all of the correct answers.&nbsp; Apply the scientific method and do some research, experiment, and question the authority.&nbsp;&nbsp; There should be no sacred cows in programming.  <li>Talk with others.&nbsp; Join a user group, show up every now and then, heckle the presenter, and -maybe- speak.&nbsp; You want a broad range of people who you can talk to and bounce ideas off of.&nbsp; This will help you from becoming that crazy guy in the corner who vehemently states that all your code should be in one file.&nbsp; Having a mentor (mentioned earlier) is great, but having people around to push you from multiple directions is also good.  <li>Learn languages.&nbsp; This gets back to the "read code" idea. Make that multiple types of languages as well.&nbsp; If you know C# or Java and SQL, learn Python or Ruby, get really good at JavaScript.&nbsp; If you want something really out there, learn MDX.&nbsp; This is also a repertoire thing.&nbsp; Seeing how other languages work will make you rethink your ideas about what your current code, in whatever languages you are working in, should look like.  <li>Keep Thinking!!!&nbsp; That is possibly the most important point.&nbsp; Keep thinking.&nbsp; Don't just evaluate something once and never return, go back and reevaluate. Did the technique work?&nbsp; How could it have been better?&nbsp;&nbsp; The idea is continual improvement.  <li>Switch jobs every now and then.&nbsp; When I announced I was leaving my first programming job out of college, the VP of R&amp;D had me sit down with him and talk.&nbsp; He wasn't trying to keep me (he knew I was moving to be closer to family), he wanted to give me some advice.&nbsp; He told me to change jobs every three years.&nbsp; After you have been with a place for three years you have probably learned everything you are going to learn and it time to move on.&nbsp; This doesn't mean changing companies either.&nbsp; I've been with the same company now for four years, but I've had three different jobs.&nbsp; If you have been writing commercial applications, become a consultant - or vise-versa, become a test engineer, expand your boundaries.&nbsp; Again, this is about pushing yourself to be better.  <li>Have a Hobby. But don't ask about me, I have too many.&nbsp;&nbsp; Creating software is about creating things.&nbsp; So I suggest picking a hobby that involves creating something.&nbsp; Popular hobbies amongst programmers I know include: cooking, music, woodworking, beer making, and BBQ (which is different than cooking).&nbsp; But don't discount sports (golf is always popular), computer games, and board games.&nbsp; Anything that promotes the development of skill levels can't be a bad thing. </li></ol> <p>And I'm stopping there.&nbsp; This is my beginner's guide to how to become the writer elegant code.&nbsp; If you have others you think I missed, add them too the comments.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>LINQ Framework Design Guidelines</title>
		<link>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=linq-framework-design-guidelines-2</link>
		<comments>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/#comments</comments>
		<pubDate>Wed, 19 Mar 2008 01:53:22 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[Microsoft]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/</guid>
		<description><![CDATA[Excellent article on design considerations for LINQ. Looks like these concepts will be incorporated into the 2nd Edition of Framework Design Guidelines: Conventions, Idioms, and Patterns for Reuseable .NET Libraries. Cant wait to get my hands on a copy of that!]]></description>
			<content:encoded><![CDATA[<p>Excellent article on <a href="http://blogs.msdn.com/mirceat/archive/2008/03/13/linq-framework-design-guidelines.aspx" target="_blank">design considerations for LINQ</a>. Looks like these concepts will be incorporated into the 2nd Edition of <a href="http://www.amazon.com/Framework-Design-Guidelines-Conventions-Development/dp/0321545613/ref=pd_bbs_sr_2?ie=UTF8&amp;s=books&amp;qid=1200508590&amp;sr=1-2" target="_blank">Framework Design Guidelines: Conventions, Idioms, and Patterns for Reuseable .NET Libraries</a>. Cant wait to get my hands on a copy of that!</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Boise Code Camp 2008</title>
		<link>http://elegantcode.com/2008/03/09/boise-code-camp-2008/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=boise-code-camp-2008</link>
		<comments>http://elegantcode.com/2008/03/09/boise-code-camp-2008/#comments</comments>
		<pubDate>Mon, 10 Mar 2008 02:25:35 +0000</pubDate>
		<dc:creator>Alex Mueller</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Code Camp]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Patterns and Practices]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/09/boise-code-camp-2008/</guid>
		<description><![CDATA[Boise Code Camp 2008 was awesome. Thank you to David Starr and his wife, Eleanor, for taking ownership of this event and dedicating many months of their time to make this a reality. Thank you to the presenters for having the passion and desire to present technologies and practices, both new and not-as-new. Thank you [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Boise Code Camp 2008" href="http://www.boisecodecamp.org/" target="_blank">Boise Code Camp 2008</a> was awesome. Thank you to <a title="David Starr" href="http://elegantcode.com/about/david-starr/" target="_blank">David Starr</a> and his wife, <a title="Domestic Oblivion" href="http://domesticoblivion.com/" target="_blank">Eleanor</a>, for taking ownership of this event and dedicating many months of their time to make this a reality. Thank you to the presenters for having the passion and desire to present technologies and practices, both new and not-as-new. Thank you to the many volunteers who put in countless hours to help coordinate the weekend's events. Thank you to the campers for attending and making this year's code camp a success.</p>  <p>I am sure everyone will be blogging about all the great sessions they attended at code camp. I enjoyed all of the presentations I attended, and I am excited to explore some new material. </p>  <p>I want to share a few thoughts on my presentation dealing with Model-View-Presenter in ASP.NET after listening to <a title="Scott Hanselman" href="http://www.hanselman.com" target="_blank">Scott Hanselman's</a> session on the <a title="ASP. NET MVC Project" href="http://www.asp.net/mvc" target="_blank">ASP.NET MVC Project</a>. I may have the percentage incorrect, but Scott mentioned something like it is predicted that only 10% of the ASP.NET community currently using Web Forms will adopt and use the MVC framework. The MVC framework is an addition to ASP.NET, it is not a replacement. What this means is that Model-View-Presenter will still be a viable pattern to implement with your ASP.NET applications, and it is not going away. ASP.NET Web Forms will not be going away either.</p>  <p>Use MVP to get your third party controls under test. Use MVP to provide that separation of concerns in your legacy applications. Use it entirely or in conjunction with the MVC framework. It is all about testability. <a title="Glenn Block" href="http://blogs.msdn.com/gblock/" target="_blank">Glenn Block</a> presented on the Web Client Software Factory, and what pattern does this implement? Model-View-Presenter.</p>  <p>I am providing my <a title="Presentation and Source Code" href="http://muellerdesigns.net/DasBlog/content/binary/ModeViewPresenter.AlexMueller.BoiseCodeCamp2008.rar" target="_blank">presentation and source code (3.67 MB)</a> from my talk on MVP. It will be available via the <a title="Boise Code Camp" href="http://www.boisecodecamp.org/" target="_blank">Boise Code Camp</a> site as well. </p>  <p>Again, thank the many individuals and their families who sacrificed their time to bring to the local community this years code camp.</p>  <p>Around the office's water cooler on Monday, I will be able to say, &quot;and this one time, at code camp...&quot;</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/09/boise-code-camp-2008/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Microsoft releases Unity, an IoC library</title>
		<link>http://elegantcode.com/2008/04/24/name-those-patterns/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=name-those-patterns</link>
		<comments>http://elegantcode.com/2008/04/24/name-those-patterns/#comments</comments>
		<pubDate>Thu, 24 Apr 2008 16:16:13 +0000</pubDate>
		<dc:creator>Tony Rasa</dc:creator>
				<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/24/name-those-patterns/</guid>
		<description><![CDATA[In yesterday's discussion of Gang of Four Design Patterns, I said &#34;Never name your classes after the pattern itself.&#34;&#160; But then someone pointed out &#34;What about Factory classes?&#34;&#160; &#34;Oh, ok, ALWAYS name classes after the pattern.&#34;&#160; Except for Singleton or Fa&#231;ade, and maybe a few others.&#160; Ok then, NEVER name after the pattern, except when [...]]]></description>
			<content:encoded><![CDATA[<p>In yesterday's discussion of Gang of Four Design Patterns, I said &quot;Never name your classes after the pattern itself.&quot;&#160; But then someone pointed out &quot;What about Factory classes?&quot;&#160; &quot;Oh, ok, ALWAYS name classes after the pattern.&quot;&#160; Except for Singleton or Fa&#231;ade, and maybe a few others.&#160; Ok then, NEVER name after the pattern, except when you do, sometimes, and then ALWAYS name those, unless otherwise noted.</p>  <p>So, here's a list of the 23 Gang of Four patterns, along with when I'd use the name of the pattern in the implementation.&#160; Unless I didn't want to, and then I wouldn't.&#160; Feel free to agree with this list, unless you don't care to.</p>  <h3>Creational Patterns</h3>  <p><strong>Abstract Factory</strong> - After consideration, I have to say that I call my factories Factory, unless they're Repositories, Data Access Layers, DAOs, or something else.&#160; But most of the time, Factory.&#160; Because this would just be wrong:</p>  <pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> FooCreatorMakerThingy { ... }</pre>

<p><strong>Builder</strong> - Hmm, another noun. Guess I'd use this one too, for the same reason as Abstract Factory. Most of the time.&#160; </p>

<p><strong>Factory Method</strong> - I probably would not call my Factory Methods &quot;GetFooFactoryMethod()&quot; because that just looks weird.&#160;&#160; Maybe if we verbify the noun:</p>

<pre class="csharpcode"><span class="kwrd">public</span> Foo FactorialMethodizeFoo() {...}</pre>

<p>Nope, that's still dumb.</p>

<p><strong>Prototype</strong> - &quot;Specify the kinds of objects to create using a prototypical instance...&quot; So we don't care that the class is a &quot;Prototype,&quot; but we do care about the &quot;kind of object&quot; part.&#160; So don't name your prototypical classes &quot;FooPrototype.&quot;&#160; (Unless you just want to.)</p>

<p><strong>Singleton</strong> - Somebody have a case where it makes sense to name your Singleton &quot;Singleton&quot;?&#160; I can't come up with one.&#160; Anyway, you shouldn't be using singletons anymore anyway.&#160; :)</p>

<h3>Structural Patterns</h3>

<p><strong>Adapter</strong> - Adapters are pretty much called Adapters.&#160; Except when they're called Wrappers.&#160; So you can use this name too.</p>

<p><strong>Bridge</strong> - I wouldn't label a participant in a bridge pattern &quot;FooBridge,&quot; unless I was trying to impress on an interview test or something.&#160; And even then, that's a bad idea.&#160; &quot;Ohh, look, I know a complicated design pattern!&#160; Lookitme!&quot;&#160; So don't call your bridges Bridges.&#160; </p>

<p><strong>Composite</strong> - &quot;One of the goals of the Composite&#160; pattern is to make clients unaware of the specific Leaf or Composite classes they're using.&quot;&#160; So you don't want to point out that Composites are in play, that breaks the suspension of disbelief for your audience.&#160; Or something.&#160; Anyway Composites get used all over the place, if we had to label them all then it'd be FooComposite, BarComposite, TreeComposite, EverythingComposite, and what's the fun in that.</p>

<p><strong>Decorator </strong>- I think in general people don't label their Decorators Decorators.&#160; I'm sure there's a counter-example to this, but all of the Stream- and Xml-handling classes I can think of don't point out that they are Decorators.&#160; You'd probably want to stick to more descriptive names, like ASCIIStreamReader instead of cluttering things up with ASCIIStreamDecorator.&#160; We're not decorating the stream (in a literal sense), we're reading it.&#160; </p>

<p><strong>Fa&#231;ade</strong> - Don't call your Fa&#231;ade &quot;FooFa&#231;ade,&quot; that's just silly.&#160; Besides, its too much work to type that cedille into the editor if you're using an ignorant American keyboard.</p>

<p><strong>Flyweight </strong>- This is another pattern where we're going to disguise what's going on inside, in order to simplify the client's usage and make better use of resources.&#160; No reason to flaunt to the client that we're low on resources, because that's a personal and private matter - times are tough all over, and there's no shame in struggling a little bit, and hey who could have predicted that the sub-prime loan market was going to ... anyway, back to patterns.&#160;&#160; Don't call your Flyweights Flyweights, unless what you're modeling is actually a Flyweight, and then thats OK.</p>

<p><strong>Proxy </strong>- This one is standard terminology.&#160; So Proxy's ok.</p>

<h3>Behavioral Patterns (or &quot;<em>MAN this book has a lot of pages in it..</em>&quot;)</h3>

<p><strong>Chain of Responsibility </strong>- Don't even think of naming a class ChainOfResponsibility.&#160; Even with ReSharper, who'd want to have to type that?</p>

<p><strong>Command</strong> - Another convenient noun.&#160; Ok, you can create Commands.&#160; </p>

<p><strong>Interpreter </strong>- This is another pattern that doesn't have one identifying class that you could label &quot;The Interpreter.&quot;&#160; But you might create a Fa&#231;ade, and call that class Interpreter.&#160; I'm not sure if naming one pattern after another is OK or not.&#160; Makes my head hurt.&#160; </p>

<p>Iterator - Another convenient noun.&#160; Iterate away!&#160; Not that you'll have to worry too much about naming classes with this pattern if you use C#, Java, C++ or pretty much any other modern language, since this pattern is built-in.</p>

<p><strong>Mediator </strong>- This pattern promotes loose coupling, most of the time I'd call what it does a &quot;Controller,&quot; or what's being Mediated is more on a service level, so perhaps the word &quot;Service&quot; would end up in the name.&#160; Can't say I've ever called a class specifically &quot;FooMediator&quot;, but I suppose I don't have a problem with it if you want to do that.</p>

<p><strong>Memento </strong>- This pattern captures and externalizes an object's state.&#160; So I think I'd call something like it &quot;State&quot; before &quot;Memento,&quot; since it's fewer letters to type.&#160; Besides, I had to see that movie twice before I really appreciated it.&#160; </p>

<p>Ok, that was just lame, sorry.</p>

<p><strong>Observer </strong>- Oh, you mean like an Event?&#160; Or more like a delegate?&#160; :)</p>

<p><strong>State </strong>- I'd call my implementation of this pattern &quot;Memento,&quot; just to confuse you.&#160; That's just how I roll.</p>

<p><strong>Strategy </strong>- This is another pattern that it seems like we use all the time, without having to give it a fancy Ivory Tower name.&#160; As someone pointed out (sorry I lost the reference to who said that) Microsoft calls all their Strategy implementations Providers now anyway.&#160; Rather than label something a &quot;Strategy&quot; I'd probably just say what the thing does: &quot;CryptographicProvider.&quot;</p>

<p><strong>Template Method</strong> - Yet another pattern where there's no obvious place to hang the name off of, so don't try to force it in there and impress your coworkers with how smart you are.&#160; We know, you're a Design Pattern God, no need to brag about it.</p>

<p><strong>Visitor </strong>- Hmm, not sure where I stand on this one.&#160; I have used this pattern where I've named a visitor &quot;SqlGeneratingVisitor.&quot;&#160; But maybe that was a mistake?&#160; So how about we just declare this one &quot;Free Parking,&quot; and you can do what you want.</p>

<p>&#160;</p>

<p>So, there you go.&#160; A bunch of design patterns, summarized into one neat page.&#160; Now you're ready to ace that interview!&#160; </p>

<p>&#160;</p>

<p><em>disclaimer: I make no guarantees that any or all of the information will not harm you in a technical interview, and if repeating this to an interview board gets your removed from the building by security, its not my fault.&#160; This information comes without warantees or conditions of any kind, and you probably shouldn't have bothered reading it..</em></p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/24/name-those-patterns/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; Design Patterns</title>
	<atom:link href="http://elegantcode.com/tag/design-patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Where to start a new program</title>
		<link>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=where-to-start-a-new-program</link>
		<comments>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/#comments</comments>
		<pubDate>Sun, 08 Jun 2008 19:50:39 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/06/08/where-to-start-a-new-program/</guid>
		<description><![CDATA[Here as an interesting question that popped up at my user group meeting last night, when you are starting a new program (green field development), where do you get started?&#160; I find these questions interesting because there really is no correct answer, but a persons answer will tell you something about how they problem solve. [...]]]></description>
			<content:encoded><![CDATA[<p>Here as an interesting question that popped up at my user group meeting last night, when you are starting a new program (green field development), where do you get started?&nbsp; I find these questions interesting because there really is no correct answer, but a persons answer will tell you something about how they problem solve.</p> <p>Depending on who you ask you will likely get one of two answers.&nbsp; If you are talking with a data guy, he will say to start with the database.&nbsp; If you are dealing with a visual guy, he will say to start with the UI.&nbsp; Historically, I did a conglomeration of those two ideas, creating both the UI and the database at the same time, letting the two drive each other.&nbsp; But I'm weird, I'm a visual/data guy.</p> <p>Lately though, I've started to thing about a third approach.&nbsp; Starting in the middle.&nbsp; No UI, no database, just the data classes.&nbsp; This approach has been spurred on buy a couple of things.&nbsp; Namely, test driven development (TDD), design patterns, and domain driven design.</p> <p>TDD really is the glue that allows all of this to happen.&nbsp; I can start in the middle because I can run the code in the middle any time I want.&nbsp; I just have to write a test.&nbsp; No having to wade through multiple layers of UI to test out some small chunk ok UI is a great productivity gain for me.</p> <p>Next is design patterns.&nbsp; This is what gives you a reference for how to approach your code.&nbsp; That said, this isn't about making everything absolutely perfect from the get go, but it is nice to know where you are going to made your comprises early on.</p> <p>But all of that does not explain where I begin.&nbsp; That is where Domain Driven Design (DDD) kicks in.&nbsp; For those not familiar with the concept, you can read up on <a href="http://en.wikipedia.org/wiki/Domain-driven_design">wikipedia</a>, buy <a href="http://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1212806974&amp;sr=8-1">The Book</a>, or buy one of the <a href="http://www.amazon.com/s?ie=UTF8&amp;tag=mozilla-20&amp;index=blended&amp;link%5Fcode=qs&amp;field-keywords=domain%20driven%20design&amp;sourceid=Mozilla-search">various other references</a>.&nbsp;&nbsp; One of the concepts in DDD (I'm still reading the book) is setting up something called a <a href="http://www.domaindrivendesign.org/discussion/messageboardarchive/UbiquitousLanguage.html">ubiquitous language</a>.</p> <p>The ubiquitous language is actually trying to solve a problem that I have seen happen on many projects I've been apart of (but not always my fault).&nbsp; At some point there is a linguistic difference between the nouns that the customer uses and the nouns that the programmers use.&nbsp; By focusing on the language first, you are attempting to head that off at the pass.</p> <p>These nouns consist of your domain classes and your tables and columns, they move everywhere...but not always consistently.&nbsp; Consistency does matter.&nbsp; Inconsistency is what leads to rather confusing conversations with people and everyone scratching their heads about "what just happened".&nbsp; So, this is the middle that I start with.</p> <p>But before you start...</p> <p>One of the other guys in the group was an actual data guy.&nbsp; The systems he has written worked with more data than yours does.&nbsp; Trust me on that.&nbsp; Historically, his biggest consideration was data input speed.&nbsp; I don't think I would start in the middle if that was my biggest concern.&nbsp; In that case you do start with the database, you tune for speed.&nbsp; You also don't use as many domain classes, and you tend to throw away your ORM solution and just hit the database layer directly via Ado.Net.</p> <p>But lets face it, that doesn't happen very often.&nbsp; Most business applications aren't going to touch what the architecture can take.&nbsp; It is amazing how many horribly written applications with very inefficient code run just fine.&nbsp; Most of my speed issues could be solved by indexing tables and changing generic lists to generic dictionaries (more on that later).&nbsp; Architect your applications for how they are going to be used.&nbsp; Most of my web applications have a max usage of 100 simultaneous users.&nbsp; If the data access is slightly slower I'll take that hit for programmer efficiency.&nbsp; If you are designing Amazon.com, you code for through-put.</p> <p>OK, someone is going to disagree with this...I can already feel it.&nbsp; Please take a moment to register your displeasure in the comments.&nbsp; I would love to hear it.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Learning Design Patterns</title>
		<link>http://elegantcode.com/2008/04/28/learning-design-patterns/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=learning-design-patterns</link>
		<comments>http://elegantcode.com/2008/04/28/learning-design-patterns/#comments</comments>
		<pubDate>Mon, 28 Apr 2008 19:01:17 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/28/learning-design-patterns/</guid>
		<description><![CDATA[To me this seems like an interesting way to learn about design patterns (watch the video). I wonder if it also works when I hit an ivory-tower architect with POEAA or DDD? I'll keep you posted. PS: I'm really looking forward to the next episode on code reviews and retrospectives.]]></description>
			<content:encoded><![CDATA[To me <a href="http://codebetter.com/blogs/glenn.block/archive/2008/04/28/extreme-patterns-with-oren.aspx" target="_blank">this</a> seems like an interesting way to learn about design patterns (watch the video). I wonder if it also works when I hit an ivory-tower architect with <a href="http://www.bookpool.com/sm/0321127420" target="_blank">POEAA</a> or <a href="http://www.bookpool.com/sm/0321125215" target="_blank">DDD</a>? I'll keep you posted.

PS: I'm really looking forward to the next episode on code reviews and retrospectives.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/28/learning-design-patterns/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>DTO&#8217;s or Serialized Domain Entities?</title>
		<link>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=dtos-or-serialized-domain-entities</link>
		<comments>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/#comments</comments>
		<pubDate>Sun, 27 Apr 2008 08:09:02 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/</guid>
		<description><![CDATA[I hear this question come up time and time again. It is something that I have been dealing with for a while now, and have some experience with doing both ways. I started writing this in response to a DDD thread, but it got rather large and so I turned it into a post. So [...]]]></description>
			<content:encoded><![CDATA[<p>I hear this question come up time and time again. It is something that I have been dealing with for a while now, and have some experience with doing both ways. I started writing this in response to a <a href="http://tech.groups.yahoo.com/group/domaindrivendesign/message/7286" target="_blank">DDD thread</a>, but it got rather large and so I turned it into a post. So here ya go!  <p>I think you have to write DTO's for any sort of long term Enterprise 'system of systems' solution. I fought this for some time, but since moving to an Assembler/DTO I much enjoy the freedom of being able to easily tweak my DTO "snapshot" of the aggregate root I am working with. Maintaining them is very little overhead.  <p>I tend to think of the DTO as a message, a value object. This is necessary because once an entity leaves the boundary it can no longer function within its original meaning. Its methods may be intended to only run within a given context, which is not the same once on another tier. (Things like lazy loading are a good example) Once the DTO/Message arrives to the caller, it very well may (and probably should) be translated/assembled into another model which can represent its new semantics.  <p>Some other advantages of DTO w/ Assembler or Builder:<br>-Can explicitly control the depth of the graph/tree you are serializing.<br>-Can flatten out the data where needed<br>-Can return significantly less data (maybe you only need 3 fields instead of 15)<br>-Not tightly coupled between remote boundaries (this could be a mess when working with a large team)<br>-Interoperable  <p>I wrote a post a while back showing a way to '<a href="http://elegantcode.com/2007/12/05/fun-with-generics-from-repository-to-dto/" target="_blank">templatize</a>' the process. My current implementations are much different with LINQ now, but maybe it could stimulate some ideas for whomever.  <p><em>The next thing I would like to do is remove the assembler altogether. By flagging fields with attributes or using config to specify a mapping, I could then feed <a href="http://blogs.msdn.com/meek/archive/2008/04/25/using-linq-expressions-to-generate-dynamic-methods.aspx" target="_blank">dynamic methods</a> and run a translation/builder strategy.</em>  <p>I know there are concerns about writing and maintaining assembler and DTO, but to me this is the least of the worry, a more challenging scenario would be the UnitOfWork (How to you intended to track remote state, concurrency etc) How about versioning? performance? scala.. well ilities in general?  <p>These kinds of problems lead one from Request/Response, Patterns of Enterprise Architecture &amp; DDD to a Pub/Sub Message Bus - Enterprise Integration Patterns &amp; DDDD.  <p>And to think all you want to do is send some frickin entities to a remote client&nbsp; :)</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Test Data Builders Refined</title>
		<link>http://elegantcode.com/2008/04/26/test-data-builders-refined/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=test-data-builders-refined</link>
		<comments>http://elegantcode.com/2008/04/26/test-data-builders-refined/#comments</comments>
		<pubDate>Sat, 26 Apr 2008 23:08:27 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/26/test-data-builders-refined/</guid>
		<description><![CDATA[Last year, I blogged about Test Data Builders here and here. I still use them heavily in my unit tests for creating objects with test data. Heck, I also use this pattern for fluent interfaces in production code. Here is a simple example of this approach: &#160; 1: public class CustomerBuilder 2: { 3: public [...]]]></description>
			<content:encoded><![CDATA[<p>Last year, I blogged about Test Data Builders <a href="http://vanryswyckjan.blogspot.com/2007/09/test-data-builder-versus-object-mother.html" target="_blank">here</a> and <a href="http://vanryswyckjan.blogspot.com/2007/12/test-data-builder.html" target="_blank">here</a>. I still use them heavily in my unit tests for creating objects with test data. Heck, I also use this pattern for fluent interfaces in production code. Here is a simple example of this approach:</p>  <p>&#160;</p>  <div>   <div class="csharpcode">     <pre class="alteven"><span class="lnum">   1:</span> <span class="kwrd">public</span> <span class="kwrd">class</span> CustomerBuilder</pre>

    <pre class="alteven"><span class="lnum">   2:</span> {</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     <span class="kwrd">public</span> String _firstName = <span class="str">&quot;Homer&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     <span class="kwrd">public</span> String _lastName = <span class="str">&quot;Simpson&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   5:</span>         </pre>

    <pre class="alteven"><span class="lnum">   6:</span>     <span class="kwrd">public</span> CustomerBuilder WithFirstName(String firstName)</pre>

    <pre class="alteven"><span class="lnum">   7:</span>     {</pre>

    <pre class="alteven"><span class="lnum">   8:</span>         _firstName = firstName;</pre>

    <pre class="alteven"><span class="lnum">   9:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;    </pre>

    <pre class="alteven"><span class="lnum">  10:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  11:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  12:</span>     <span class="kwrd">public</span> CustomerBuilder WithLastName(String lastName)</pre>

    <pre class="alteven"><span class="lnum">  13:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  14:</span>         _lastName = lastName;</pre>

    <pre class="alteven"><span class="lnum">  15:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven"><span class="lnum">  16:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  17:</span>         </pre>

    <pre class="alteven"><span class="lnum">  18:</span>     <span class="kwrd">public</span> Customer Build()</pre>

    <pre class="alteven"><span class="lnum">  19:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  20:</span>         <span class="kwrd">return</span> <span class="kwrd">new</span> Customer(_firstName, _lastName);    </pre>

    <pre class="alteven"><span class="lnum">  21:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  22:</span> }</pre>
  </div>
</div>

<pre class="alteven">This fluent builder class can then be used this way:</pre>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> Customer customer = <span class="kwrd">new</span> CustomerBuilder()</pre>

    <pre class="alteven"><span class="lnum">   2:</span>     .WithFirstName(<span class="str">&quot;Homer&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     .WithLastName(<span class="str">&quot;Simpson&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     .Build();</pre>
  </div>
</div>

<p>A while ago, <a href="http://codebetter.com/blogs/gregyoung/default.aspx" target="_blank">Greg Young</a> started a series of blog posts on DDDD (Distributed Domain-Driven Design), which I can highly recommend. Make sure to catch up now you still can because I think that he has a lot of stuff coming up, which I'm really looking forward to.</p>

<p>Anyhow, Greg had a couple of posts on fluent builders, which you can read <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/15/dddd-5-messages-have-fluent-builders.aspx" target="_blank">here</a>, <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/16/dddd-6-fluent-builders-alternate-ending.aspx" target="_blank">here</a> and <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/22/dddd-8-fluent-builders-and-tests.aspx" target="_blank">here</a>. I noticed an interesting approach in the way that the target object is built. Here is an example of this approach:</p>

<p>&#160;</p>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> <span class="kwrd">public</span> <span class="kwrd">class</span> CustomerBuilder</pre>

    <pre class="alteven"><span class="lnum">   2:</span> {</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     <span class="kwrd">public</span> String _firstName = <span class="str">&quot;Homer&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     <span class="kwrd">public</span> String _lastName = <span class="str">&quot;Simpson&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   5:</span>         </pre>

    <pre class="alteven"><span class="lnum">   6:</span>     <span class="kwrd">public</span> CustomerBuilder WithFirstName(String firstName)</pre>

    <pre class="alteven"><span class="lnum">   7:</span>     {</pre>

    <pre class="alteven"><span class="lnum">   8:</span>         _firstName = firstName;</pre>

    <pre class="alteven"><span class="lnum">   9:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;    </pre>

    <pre class="alteven"><span class="lnum">  10:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  11:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  12:</span>     <span class="kwrd">public</span> CustomerBuilder WithLastName(String lastName)</pre>

    <pre class="alteven"><span class="lnum">  13:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  14:</span>         _lastName = lastName;</pre>

    <pre class="alteven"><span class="lnum">  15:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven"><span class="lnum">  16:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  17:</span>         </pre>

    <pre class="alteven"><span class="lnum">  18:</span>     <span class="kwrd">public</span> Customer Build()</pre>

    <pre class="alteven"><span class="lnum">  19:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  20:</span>         <span class="kwrd">return</span> <span class="kwrd">new</span> Customer(_firstName, _lastName);    </pre>

    <pre class="alteven"><span class="lnum">  21:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  22:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  23:</span>     <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">implicit</span> <span class="kwrd">operator</span> Customer(</pre>

    <pre class="alteven"><span class="lnum">  24:</span>         CustomerBuilder builder) </pre>

    <pre class="alteven"><span class="lnum">  25:</span>     {  </pre>

    <pre class="alteven"><span class="lnum">  26:</span>         <span class="kwrd">return</span> builder.Build()    </pre>

    <pre class="alteven"><span class="lnum">  27:</span>     } </pre>

    <pre class="alteven"><span class="lnum">  28:</span> }</pre>
  </div>
</div>

<p>which results in the following usage:</p>

<p>&#160;</p>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> Customer customer = <span class="kwrd">new</span> CustomerBuilder()</pre>

    <pre class="alteven"><span class="lnum">   2:</span>     .WithFirstName(<span class="str">&quot;Homer&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     .WithLastName(<span class="str">&quot;Simpson&quot;</span>);</pre>
  </div>

  <br />Adding an implicit cast operator to the builder class makes that its no longer required to explicitly call the <em>Build</em> method. I keep the <em>Build</em> method around for backwards-compatibility reasons or in case I ever need it again (violating YAGNI in the process, I know, I know). I find that adding the implicit cast operator adds to the readability of the fluent interface, don't you agree?</div>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/26/test-data-builders-refined/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Name Those Patterns</title>
		<link>http://elegantcode.com/2008/04/24/name-those-patterns/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=name-those-patterns</link>
		<comments>http://elegantcode.com/2008/04/24/name-those-patterns/#comments</comments>
		<pubDate>Thu, 24 Apr 2008 16:16:13 +0000</pubDate>
		<dc:creator>Tony Rasa</dc:creator>
				<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/24/name-those-patterns/</guid>
		<description><![CDATA[In yesterday's discussion of Gang of Four Design Patterns, I said &#34;Never name your classes after the pattern itself.&#34;&#160; But then someone pointed out &#34;What about Factory classes?&#34;&#160; &#34;Oh, ok, ALWAYS name classes after the pattern.&#34;&#160; Except for Singleton or Fa&#231;ade, and maybe a few others.&#160; Ok then, NEVER name after the pattern, except when [...]]]></description>
			<content:encoded><![CDATA[<p>In yesterday's discussion of Gang of Four Design Patterns, I said &quot;Never name your classes after the pattern itself.&quot;&#160; But then someone pointed out &quot;What about Factory classes?&quot;&#160; &quot;Oh, ok, ALWAYS name classes after the pattern.&quot;&#160; Except for Singleton or Fa&#231;ade, and maybe a few others.&#160; Ok then, NEVER name after the pattern, except when you do, sometimes, and then ALWAYS name those, unless otherwise noted.</p>  <p>So, here's a list of the 23 Gang of Four patterns, along with when I'd use the name of the pattern in the implementation.&#160; Unless I didn't want to, and then I wouldn't.&#160; Feel free to agree with this list, unless you don't care to.</p>  <h3>Creational Patterns</h3>  <p><strong>Abstract Factory</strong> - After consideration, I have to say that I call my factories Factory, unless they're Repositories, Data Access Layers, DAOs, or something else.&#160; But most of the time, Factory.&#160; Because this would just be wrong:</p>  <pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> FooCreatorMakerThingy { ... }</pre>

<p><strong>Builder</strong> - Hmm, another noun. Guess I'd use this one too, for the same reason as Abstract Factory. Most of the time.&#160; </p>

<p><strong>Factory Method</strong> - I probably would not call my Factory Methods &quot;GetFooFactoryMethod()&quot; because that just looks weird.&#160;&#160; Maybe if we verbify the noun:</p>

<pre class="csharpcode"><span class="kwrd">public</span> Foo FactorialMethodizeFoo() {...}</pre>

<p>Nope, that's still dumb.</p>

<p><strong>Prototype</strong> - &quot;Specify the kinds of objects to create using a prototypical instance...&quot; So we don't care that the class is a &quot;Prototype,&quot; but we do care about the &quot;kind of object&quot; part.&#160; So don't name your prototypical classes &quot;FooPrototype.&quot;&#160; (Unless you just want to.)</p>

<p><strong>Singleton</strong> - Somebody have a case where it makes sense to name your Singleton &quot;Singleton&quot;?&#160; I can't come up with one.&#160; Anyway, you shouldn't be using singletons anymore anyway.&#160; :)</p>

<h3>Structural Patterns</h3>

<p><strong>Adapter</strong> - Adapters are pretty much called Adapters.&#160; Except when they're called Wrappers.&#160; So you can use this name too.</p>

<p><strong>Bridge</strong> - I wouldn't label a participant in a bridge pattern &quot;FooBridge,&quot; unless I was trying to impress on an interview test or something.&#160; And even then, that's a bad idea.&#160; &quot;Ohh, look, I know a complicated design pattern!&#160; Lookitme!&quot;&#160; So don't call your bridges Bridges.&#160; </p>

<p><strong>Composite</strong> - &quot;One of the goals of the Composite&#160; pattern is to make clients unaware of the specific Leaf or Composite classes they're using.&quot;&#160; So you don't want to point out that Composites are in play, that breaks the suspension of disbelief for your audience.&#160; Or something.&#160; Anyway Composites get used all over the place, if we had to label them all then it'd be FooComposite, BarComposite, TreeComposite, EverythingComposite, and what's the fun in that.</p>

<p><strong>Decorator </strong>- I think in general people don't label their Decorators Decorators.&#160; I'm sure there's a counter-example to this, but all of the Stream- and Xml-handling classes I can think of don't point out that they are Decorators.&#160; You'd probably want to stick to more descriptive names, like ASCIIStreamReader instead of cluttering things up with ASCIIStreamDecorator.&#160; We're not decorating the stream (in a literal sense), we're reading it.&#160; </p>

<p><strong>Fa&#231;ade</strong> - Don't call your Fa&#231;ade &quot;FooFa&#231;ade,&quot; that's just silly.&#160; Besides, its too much work to type that cedille into the editor if you're using an ignorant American keyboard.</p>

<p><strong>Flyweight </strong>- This is another pattern where we're going to disguise what's going on inside, in order to simplify the client's usage and make better use of resources.&#160; No reason to flaunt to the client that we're low on resources, because that's a personal and private matter - times are tough all over, and there's no shame in struggling a little bit, and hey who could have predicted that the sub-prime loan market was going to ... anyway, back to patterns.&#160;&#160; Don't call your Flyweights Flyweights, unless what you're modeling is actually a Flyweight, and then thats OK.</p>

<p><strong>Proxy </strong>- This one is standard terminology.&#160; So Proxy's ok.</p>

<h3>Behavioral Patterns (or &quot;<em>MAN this book has a lot of pages in it..</em>&quot;)</h3>

<p><strong>Chain of Responsibility </strong>- Don't even think of naming a class ChainOfResponsibility.&#160; Even with ReSharper, who'd want to have to type that?</p>

<p><strong>Command</strong> - Another convenient noun.&#160; Ok, you can create Commands.&#160; </p>

<p><strong>Interpreter </strong>- This is another pattern that doesn't have one identifying class that you could label &quot;The Interpreter.&quot;&#160; But you might create a Fa&#231;ade, and call that class Interpreter.&#160; I'm not sure if naming one pattern after another is OK or not.&#160; Makes my head hurt.&#160; </p>

<p>Iterator - Another convenient noun.&#160; Iterate away!&#160; Not that you'll have to worry too much about naming classes with this pattern if you use C#, Java, C++ or pretty much any other modern language, since this pattern is built-in.</p>

<p><strong>Mediator </strong>- This pattern promotes loose coupling, most of the time I'd call what it does a &quot;Controller,&quot; or what's being Mediated is more on a service level, so perhaps the word &quot;Service&quot; would end up in the name.&#160; Can't say I've ever called a class specifically &quot;FooMediator&quot;, but I suppose I don't have a problem with it if you want to do that.</p>

<p><strong>Memento </strong>- This pattern captures and externalizes an object's state.&#160; So I think I'd call something like it &quot;State&quot; before &quot;Memento,&quot; since it's fewer letters to type.&#160; Besides, I had to see that movie twice before I really appreciated it.&#160; </p>

<p>Ok, that was just lame, sorry.</p>

<p><strong>Observer </strong>- Oh, you mean like an Event?&#160; Or more like a delegate?&#160; :)</p>

<p><strong>State </strong>- I'd call my implementation of this pattern &quot;Memento,&quot; just to confuse you.&#160; That's just how I roll.</p>

<p><strong>Strategy </strong>- This is another pattern that it seems like we use all the time, without having to give it a fancy Ivory Tower name.&#160; As someone pointed out (sorry I lost the reference to who said that) Microsoft calls all their Strategy implementations Providers now anyway.&#160; Rather than label something a &quot;Strategy&quot; I'd probably just say what the thing does: &quot;CryptographicProvider.&quot;</p>

<p><strong>Template Method</strong> - Yet another pattern where there's no obvious place to hang the name off of, so don't try to force it in there and impress your coworkers with how smart you are.&#160; We know, you're a Design Pattern God, no need to brag about it.</p>

<p><strong>Visitor </strong>- Hmm, not sure where I stand on this one.&#160; I have used this pattern where I've named a visitor &quot;SqlGeneratingVisitor.&quot;&#160; But maybe that was a mistake?&#160; So how about we just declare this one &quot;Free Parking,&quot; and you can do what you want.</p>

<p>&#160;</p>

<p>So, there you go.&#160; A bunch of design patterns, summarized into one neat page.&#160; Now you're ready to ace that interview!&#160; </p>

<p>&#160;</p>

<p><em>disclaimer: I make no guarantees that any or all of the information will not harm you in a technical interview, and if repeating this to an interview board gets your removed from the building by security, its not my fault.&#160; This information comes without warantees or conditions of any kind, and you probably shouldn't have bothered reading it..</em></p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/24/name-those-patterns/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.Net HierarchicalDataSource&lt;T&gt;</title>
		<link>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=aspnet-hierarchicaldatasourcet</link>
		<comments>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/#comments</comments>
		<pubDate>Sun, 06 Apr 2008 06:45:44 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Extension Methods]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/</guid>
		<description><![CDATA[The Asp.net 2.0 tree control, as well as many 3rd party controls binds to hierarchical data via an IHierarchicalDataSource interface. Asp.net provides an implementation OOTB with the HierarchicalDataSourceControl! Easy, let’s hook that up to our… wait this control only takes data in xml format or from a sitemap? Hmm, we don’t want to convert our [...]]]></description>
			<content:encoded><![CDATA[<p>The Asp.net 2.0 tree control, as well as many 3rd party controls binds to hierarchical data via an IHierarchicalDataSource interface. Asp.net provides an implementation OOTB with the HierarchicalDataSourceControl! Easy, let’s hook that up to our… wait this control only takes data in xml format or from a sitemap? Hmm, we don’t want to convert our model to xml, and a sitemap seems inappropriate. We spend a lot time doing great patterns such as MVP &amp; MVC, we should just be able to bind up a list of model objects we already have.  <p>Good thing MS has exposed a set of interfaces and abstract classes in System.Web.UI for us to implement our own IHierarchicalDataSource and HierarchicalDataSourceView and IHierarchicalEnumerable and then IHierarchyData... geesh.  <p>Data? So let's see here, in order to bind our model to the tree control it needs to implement an interface in System.Web.UI? Well that's just not going to happen, so its time for some Elegant Code.  <p><strong>Story:</strong>  <p>Dude, as a software developer, needs to create a custom implementation of IHierarchicalDataSource, and the rest of its parts, so that he can bind a list of objects to a tree control in asp.net  <ul> <li>Use existing model to bind to tree control  <li>Must be able to reuse data source for future models  <li>Model must not contain a reference to System.Web.UI </li></ul> <p>In order to setup our model to address the acceptance criteria we are going to use an interface with a self-referencing generic declaration, where the type will specify itself as the concrete type. This will let us create a nice generic hierarchical model to pass into our datasource without resorting to any sort of base class.  <div><pre class="csharpcode"><span class="kwrd">namespace</span> POC.Common
{
    <span class="kwrd">public</span> <span class="kwrd">interface</span> IModelWithHierarchy&lt;T&gt;
    {
        <span class="kwrd">string</span> Name { get; set; }
        T Parent { get; set; }
        List&lt;T&gt; Children { get; set; }
    }
}</pre><em>Lets note that we have placed the interface in a Common namespace which represents a shared.dll between Web &amp; Model</em> </div>
<p>The UI part of criteria is now a bit more tricky because the HierarchicalDataSourceView expects a IHierarchicalEnumerable datasource which contains IHierarchyData. Said IHierarchyData != IModelWithHierarchy&lt;T&gt;.&nbsp; Luckily someone else has already figured this one out for us. We need an <a href="http://www.dofactory.com/Patterns/PatternAdapter.aspx" target="_blank">adapter</a>. 
<p><a href="http://elegantcode.com/wp-content/uploads/2008/04/image7.png" target="_blank"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="123" alt="image" src="http://elegantcode.com/wp-content/uploads/2008/04/image-thumb7.png" width="244" border="0"></a> 
<p>To instantiate our HierarchyData adapter objects we will use an extension method and linq 'Select' to convert our datasource IEnumerable&lt;T&gt; of IModelWithHierarchy&lt;T&gt; 's to an IHierarchicalEnumerable of IHierarchyData. 
<div><pre class="csharpcode"><span class="kwrd">namespace</span> POC.Web.HierarchyExtensions
{
    <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">class</span> HierarchyConverter
    {
        <span class="kwrd">public</span> <span class="kwrd">static</span> HierarchicalModelList
            ToHierarchicalModelList&lt;T&gt;(<span class="kwrd">this</span> IEnumerable&lt;T&gt; modelWithHierarchy)
            <span class="kwrd">where</span> T : IModelWithHierarchy&lt;T&gt;
        {
            <span class="kwrd">return</span> <span class="kwrd">new</span> HierarchicalModelList(
                modelWithHierarchy.Select(m =&gt; <span class="kwrd">new</span> HierarchyData&lt;T&gt;(m) <span class="kwrd">as</span> IHierarchyData));
        }
    }
}</pre></div>
<p>I am not going to bore you with the implementation details of the HierarachyData&lt;T&gt; adapter and the .net abstractions, but have dropped the <a href="http://elegantcode.com/wp-content/uploads/2008/04/hierarchydatasourcepoc.zip" target="_blank">source code here</a> if your interested. The end result: 
<div><pre class="csharpcode">TreeView1.DataSource = <span class="kwrd">new</span> HierarchicalModelDataSource&lt;Category&gt; { DataSource = Categories };
TreeView1.DataBind();
TreeView1.CollapseAll();</pre></div>
<p>Story complete!&nbsp; </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is Elegant Code to me?</title>
		<link>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-is-elegant-code-to-me</link>
		<comments>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/#comments</comments>
		<pubDate>Mon, 31 Mar 2008 03:47:45 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Patterns and Practices]]></category>
		<category><![CDATA[Widgets of Wisdom]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/</guid>
		<description><![CDATA[This question keeps popping up around here ("around here" being the loose conglomeration that makes up the Elegant Code group).&#160; It isn't easy to describe.&#160; And really, the notion of what constitutes elegance in code changes over time.&#160; There is no static "this is good code" test, and I doubt there ever will be.&#160; Plus, [...]]]></description>
			<content:encoded><![CDATA[<p>This question keeps popping up around here ("around here" being the loose conglomeration that makes up the Elegant Code group).&nbsp; It isn't easy to describe.&nbsp; And really, the notion of what constitutes elegance in code changes over time.&nbsp; There is no static "this is good code" test, and I doubt there ever will be.&nbsp; Plus, what makes good code in one language, may not apply to the next.</p> <p>So let me state for the record: today's elegant code is tomorrow's drivel.&nbsp; Don't feel bad, many writers have the same problems.&nbsp; What was super amazing back in the day is now rubbish or near unreadable.&nbsp; I am thinking of the Victorian writing that Hemingway usurped, and now Hemingway himself is almost unreadable (to me anyway).&nbsp; Tastes change with the times.&nbsp; That is a simple fact.</p> <p>So what can you do about this?&nbsp; As I say that old writing sucks to read (read Washington Irvine lately?), great works of literature still abound (for instance, Hemingway is still a great writer -- even if I prefer Tolkien) .&nbsp; So take some notes from them, and from other crafts in looking for the answer.&nbsp; If you want a cliff notes version of what this is going to tell you, it is this: you must always push yourself to get better.</p> <ol> <li>Find a good teacher.&nbsp; Nothing is better than sitting at the feet of a master who can nudge you along in the right direction.&nbsp; While early mistakes can often be corrected easily with a little bit of guidance, after they have had some time to fester all bets are off.&nbsp; I believe a fare number of "Anit-patterns" were created by self taught developers (find <a href="http://elegantcode.com/2008/03/21/sql-ejaculation/">SQL Ejaculation</a> on this site).&nbsp; <li>Read.&nbsp;&nbsp; Good writer are first good readers.&nbsp; Start with Code Complete, move into a good Patterns book, get MSDN Magazine, find some bloggers your like, but keep moving.&nbsp; You will NEVER be done reading.&nbsp; I imagine that even Martin Fowler has a "to read" booklist a mile long.  <li>Read code.&nbsp;&nbsp; There are a plethora of open source project just waiting to be read -- do so.&nbsp; This is the single best way to expand your coding repertoire, find things your language can do that you didn't even know about..&nbsp;&nbsp; Scott Hanselman has recently popularized this idea, and I thank him for it.  <li>Practice.&nbsp; This means writing small applications at home.&nbsp; You get an idea that you want to try out -- do it.&nbsp; I don't mean you have to write finished applications, just have some exploring time.&nbsp; I remember talking with an 80 year old master woodworker (he lives down the street from me), who was telling me how many time he would practice making dovetail joints before he felt competent.&nbsp; It was years worth.  <li>Pay Attention.&nbsp; Being good at anything really amounts to that.&nbsp; And don't just pay attention when reading, writing, or talking about code.&nbsp; Inspiration come from everywhere, any good artist will tell you that.&nbsp; Pay attention when you cook, when you work on the car, when you are wood working, playing an instrument, whatever it is that you do.&nbsp; This will help you in the end, event if it is just learning the amount of dedication it really takes to become good at something.  <li>Beware of preferences.&nbsp; Any time I hear someone start a statement with "I prefer code to ..." you know things are going downhill.&nbsp; If you find someone who cares more about how your code is formatted then how it is written you have found yourself in a mess.&nbsp; More importantly, beware of them in yourself.&nbsp;&nbsp; Having code style standard is important, but it isn't worth loosing sleep over.&nbsp; This also pertains to inheritance, patterns, use of particular classes (e.g. always using the generic list class in C# when a Dictionary would be better).&nbsp; <li>No Sacred Cows. Believe no single source of information.&nbsp; This means not thinking that Microsoft, Sun, IBM, Richard Stallman, or anyone else will have all of the correct answers.&nbsp; Apply the scientific method and do some research, experiment, and question the authority.&nbsp;&nbsp; There should be no sacred cows in programming.  <li>Talk with others.&nbsp; Join a user group, show up every now and then, heckle the presenter, and -maybe- speak.&nbsp; You want a broad range of people who you can talk to and bounce ideas off of.&nbsp; This will help you from becoming that crazy guy in the corner who vehemently states that all your code should be in one file.&nbsp; Having a mentor (mentioned earlier) is great, but having people around to push you from multiple directions is also good.  <li>Learn languages.&nbsp; This gets back to the "read code" idea. Make that multiple types of languages as well.&nbsp; If you know C# or Java and SQL, learn Python or Ruby, get really good at JavaScript.&nbsp; If you want something really out there, learn MDX.&nbsp; This is also a repertoire thing.&nbsp; Seeing how other languages work will make you rethink your ideas about what your current code, in whatever languages you are working in, should look like.  <li>Keep Thinking!!!&nbsp; That is possibly the most important point.&nbsp; Keep thinking.&nbsp; Don't just evaluate something once and never return, go back and reevaluate. Did the technique work?&nbsp; How could it have been better?&nbsp;&nbsp; The idea is continual improvement.  <li>Switch jobs every now and then.&nbsp; When I announced I was leaving my first programming job out of college, the VP of R&amp;D had me sit down with him and talk.&nbsp; He wasn't trying to keep me (he knew I was moving to be closer to family), he wanted to give me some advice.&nbsp; He told me to change jobs every three years.&nbsp; After you have been with a place for three years you have probably learned everything you are going to learn and it time to move on.&nbsp; This doesn't mean changing companies either.&nbsp; I've been with the same company now for four years, but I've had three different jobs.&nbsp; If you have been writing commercial applications, become a consultant - or vise-versa, become a test engineer, expand your boundaries.&nbsp; Again, this is about pushing yourself to be better.  <li>Have a Hobby. But don't ask about me, I have too many.&nbsp;&nbsp; Creating software is about creating things.&nbsp; So I suggest picking a hobby that involves creating something.&nbsp; Popular hobbies amongst programmers I know include: cooking, music, woodworking, beer making, and BBQ (which is different than cooking).&nbsp; But don't discount sports (golf is always popular), computer games, and board games.&nbsp; Anything that promotes the development of skill levels can't be a bad thing. </li></ol> <p>And I'm stopping there.&nbsp; This is my beginner's guide to how to become the writer elegant code.&nbsp; If you have others you think I missed, add them too the comments.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>LINQ Framework Design Guidelines</title>
		<link>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=linq-framework-design-guidelines-2</link>
		<comments>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/#comments</comments>
		<pubDate>Wed, 19 Mar 2008 01:53:22 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[Microsoft]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/</guid>
		<description><![CDATA[Excellent article on design considerations for LINQ. Looks like these concepts will be incorporated into the 2nd Edition of Framework Design Guidelines: Conventions, Idioms, and Patterns for Reuseable .NET Libraries. Cant wait to get my hands on a copy of that!]]></description>
			<content:encoded><![CDATA[<p>Excellent article on <a href="http://blogs.msdn.com/mirceat/archive/2008/03/13/linq-framework-design-guidelines.aspx" target="_blank">design considerations for LINQ</a>. Looks like these concepts will be incorporated into the 2nd Edition of <a href="http://www.amazon.com/Framework-Design-Guidelines-Conventions-Development/dp/0321545613/ref=pd_bbs_sr_2?ie=UTF8&amp;s=books&amp;qid=1200508590&amp;sr=1-2" target="_blank">Framework Design Guidelines: Conventions, Idioms, and Patterns for Reuseable .NET Libraries</a>. Cant wait to get my hands on a copy of that!</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Boise Code Camp 2008</title>
		<link>http://elegantcode.com/2008/03/09/boise-code-camp-2008/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=boise-code-camp-2008</link>
		<comments>http://elegantcode.com/2008/03/09/boise-code-camp-2008/#comments</comments>
		<pubDate>Mon, 10 Mar 2008 02:25:35 +0000</pubDate>
		<dc:creator>Alex Mueller</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Code Camp]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Patterns and Practices]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/09/boise-code-camp-2008/</guid>
		<description><![CDATA[Boise Code Camp 2008 was awesome. Thank you to David Starr and his wife, Eleanor, for taking ownership of this event and dedicating many months of their time to make this a reality. Thank you to the presenters for having the passion and desire to present technologies and practices, both new and not-as-new. Thank you [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Boise Code Camp 2008" href="http://www.boisecodecamp.org/" target="_blank">Boise Code Camp 2008</a> was awesome. Thank you to <a title="David Starr" href="http://elegantcode.com/about/david-starr/" target="_blank">David Starr</a> and his wife, <a title="Domestic Oblivion" href="http://domesticoblivion.com/" target="_blank">Eleanor</a>, for taking ownership of this event and dedicating many months of their time to make this a reality. Thank you to the presenters for having the passion and desire to present technologies and practices, both new and not-as-new. Thank you to the many volunteers who put in countless hours to help coordinate the weekend's events. Thank you to the campers for attending and making this year's code camp a success.</p>  <p>I am sure everyone will be blogging about all the great sessions they attended at code camp. I enjoyed all of the presentations I attended, and I am excited to explore some new material. </p>  <p>I want to share a few thoughts on my presentation dealing with Model-View-Presenter in ASP.NET after listening to <a title="Scott Hanselman" href="http://www.hanselman.com" target="_blank">Scott Hanselman's</a> session on the <a title="ASP. NET MVC Project" href="http://www.asp.net/mvc" target="_blank">ASP.NET MVC Project</a>. I may have the percentage incorrect, but Scott mentioned something like it is predicted that only 10% of the ASP.NET community currently using Web Forms will adopt and use the MVC framework. The MVC framework is an addition to ASP.NET, it is not a replacement. What this means is that Model-View-Presenter will still be a viable pattern to implement with your ASP.NET applications, and it is not going away. ASP.NET Web Forms will not be going away either.</p>  <p>Use MVP to get your third party controls under test. Use MVP to provide that separation of concerns in your legacy applications. Use it entirely or in conjunction with the MVC framework. It is all about testability. <a title="Glenn Block" href="http://blogs.msdn.com/gblock/" target="_blank">Glenn Block</a> presented on the Web Client Software Factory, and what pattern does this implement? Model-View-Presenter.</p>  <p>I am providing my <a title="Presentation and Source Code" href="http://muellerdesigns.net/DasBlog/content/binary/ModeViewPresenter.AlexMueller.BoiseCodeCamp2008.rar" target="_blank">presentation and source code (3.67 MB)</a> from my talk on MVP. It will be available via the <a title="Boise Code Camp" href="http://www.boisecodecamp.org/" target="_blank">Boise Code Camp</a> site as well. </p>  <p>Again, thank the many individuals and their families who sacrificed their time to bring to the local community this years code camp.</p>  <p>Around the office's water cooler on Monday, I will be able to say, &quot;and this one time, at code camp...&quot;</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/09/boise-code-camp-2008/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Microsoft releases Unity, an IoC library</title>
		<link>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=aspnet-hierarchicaldatasourcet</link>
		<comments>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/#comments</comments>
		<pubDate>Sun, 06 Apr 2008 06:45:44 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Extension Methods]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/</guid>
		<description><![CDATA[The Asp.net 2.0 tree control, as well as many 3rd party controls binds to hierarchical data via an IHierarchicalDataSource interface. Asp.net provides an implementation OOTB with the HierarchicalDataSourceControl! Easy, let’s hook that up to our… wait this control only takes data in xml format or from a sitemap? Hmm, we don’t want to convert our [...]]]></description>
			<content:encoded><![CDATA[<p>The Asp.net 2.0 tree control, as well as many 3rd party controls binds to hierarchical data via an IHierarchicalDataSource interface. Asp.net provides an implementation OOTB with the HierarchicalDataSourceControl! Easy, let’s hook that up to our… wait this control only takes data in xml format or from a sitemap? Hmm, we don’t want to convert our model to xml, and a sitemap seems inappropriate. We spend a lot time doing great patterns such as MVP &amp; MVC, we should just be able to bind up a list of model objects we already have.  <p>Good thing MS has exposed a set of interfaces and abstract classes in System.Web.UI for us to implement our own IHierarchicalDataSource and HierarchicalDataSourceView and IHierarchicalEnumerable and then IHierarchyData... geesh.  <p>Data? So let's see here, in order to bind our model to the tree control it needs to implement an interface in System.Web.UI? Well that's just not going to happen, so its time for some Elegant Code.  <p><strong>Story:</strong>  <p>Dude, as a software developer, needs to create a custom implementation of IHierarchicalDataSource, and the rest of its parts, so that he can bind a list of objects to a tree control in asp.net  <ul> <li>Use existing model to bind to tree control  <li>Must be able to reuse data source for future models  <li>Model must not contain a reference to System.Web.UI </li></ul> <p>In order to setup our model to address the acceptance criteria we are going to use an interface with a self-referencing generic declaration, where the type will specify itself as the concrete type. This will let us create a nice generic hierarchical model to pass into our datasource without resorting to any sort of base class.  <div><pre class="csharpcode"><span class="kwrd">namespace</span> POC.Common
{
    <span class="kwrd">public</span> <span class="kwrd">interface</span> IModelWithHierarchy&lt;T&gt;
    {
        <span class="kwrd">string</span> Name { get; set; }
        T Parent { get; set; }
        List&lt;T&gt; Children { get; set; }
    }
}</pre><em>Lets note that we have placed the interface in a Common namespace which represents a shared.dll between Web &amp; Model</em> </div>
<p>The UI part of criteria is now a bit more tricky because the HierarchicalDataSourceView expects a IHierarchicalEnumerable datasource which contains IHierarchyData. Said IHierarchyData != IModelWithHierarchy&lt;T&gt;.&nbsp; Luckily someone else has already figured this one out for us. We need an <a href="http://www.dofactory.com/Patterns/PatternAdapter.aspx" target="_blank">adapter</a>. 
<p><a href="http://elegantcode.com/wp-content/uploads/2008/04/image7.png" target="_blank"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="123" alt="image" src="http://elegantcode.com/wp-content/uploads/2008/04/image-thumb7.png" width="244" border="0"></a> 
<p>To instantiate our HierarchyData adapter objects we will use an extension method and linq 'Select' to convert our datasource IEnumerable&lt;T&gt; of IModelWithHierarchy&lt;T&gt; 's to an IHierarchicalEnumerable of IHierarchyData. 
<div><pre class="csharpcode"><span class="kwrd">namespace</span> POC.Web.HierarchyExtensions
{
    <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">class</span> HierarchyConverter
    {
        <span class="kwrd">public</span> <span class="kwrd">static</span> HierarchicalModelList
            ToHierarchicalModelList&lt;T&gt;(<span class="kwrd">this</span> IEnumerable&lt;T&gt; modelWithHierarchy)
            <span class="kwrd">where</span> T : IModelWithHierarchy&lt;T&gt;
        {
            <span class="kwrd">return</span> <span class="kwrd">new</span> HierarchicalModelList(
                modelWithHierarchy.Select(m =&gt; <span class="kwrd">new</span> HierarchyData&lt;T&gt;(m) <span class="kwrd">as</span> IHierarchyData));
        }
    }
}</pre></div>
<p>I am not going to bore you with the implementation details of the HierarachyData&lt;T&gt; adapter and the .net abstractions, but have dropped the <a href="http://elegantcode.com/wp-content/uploads/2008/04/hierarchydatasourcepoc.zip" target="_blank">source code here</a> if your interested. The end result: 
<div><pre class="csharpcode">TreeView1.DataSource = <span class="kwrd">new</span> HierarchicalModelDataSource&lt;Category&gt; { DataSource = Categories };
TreeView1.DataBind();
TreeView1.CollapseAll();</pre></div>
<p>Story complete!&nbsp; </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; Design Patterns</title>
	<atom:link href="http://elegantcode.com/tag/design-patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Where to start a new program</title>
		<link>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=where-to-start-a-new-program</link>
		<comments>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/#comments</comments>
		<pubDate>Sun, 08 Jun 2008 19:50:39 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/06/08/where-to-start-a-new-program/</guid>
		<description><![CDATA[Here as an interesting question that popped up at my user group meeting last night, when you are starting a new program (green field development), where do you get started?&#160; I find these questions interesting because there really is no correct answer, but a persons answer will tell you something about how they problem solve. [...]]]></description>
			<content:encoded><![CDATA[<p>Here as an interesting question that popped up at my user group meeting last night, when you are starting a new program (green field development), where do you get started?&nbsp; I find these questions interesting because there really is no correct answer, but a persons answer will tell you something about how they problem solve.</p> <p>Depending on who you ask you will likely get one of two answers.&nbsp; If you are talking with a data guy, he will say to start with the database.&nbsp; If you are dealing with a visual guy, he will say to start with the UI.&nbsp; Historically, I did a conglomeration of those two ideas, creating both the UI and the database at the same time, letting the two drive each other.&nbsp; But I'm weird, I'm a visual/data guy.</p> <p>Lately though, I've started to thing about a third approach.&nbsp; Starting in the middle.&nbsp; No UI, no database, just the data classes.&nbsp; This approach has been spurred on buy a couple of things.&nbsp; Namely, test driven development (TDD), design patterns, and domain driven design.</p> <p>TDD really is the glue that allows all of this to happen.&nbsp; I can start in the middle because I can run the code in the middle any time I want.&nbsp; I just have to write a test.&nbsp; No having to wade through multiple layers of UI to test out some small chunk ok UI is a great productivity gain for me.</p> <p>Next is design patterns.&nbsp; This is what gives you a reference for how to approach your code.&nbsp; That said, this isn't about making everything absolutely perfect from the get go, but it is nice to know where you are going to made your comprises early on.</p> <p>But all of that does not explain where I begin.&nbsp; That is where Domain Driven Design (DDD) kicks in.&nbsp; For those not familiar with the concept, you can read up on <a href="http://en.wikipedia.org/wiki/Domain-driven_design">wikipedia</a>, buy <a href="http://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1212806974&amp;sr=8-1">The Book</a>, or buy one of the <a href="http://www.amazon.com/s?ie=UTF8&amp;tag=mozilla-20&amp;index=blended&amp;link%5Fcode=qs&amp;field-keywords=domain%20driven%20design&amp;sourceid=Mozilla-search">various other references</a>.&nbsp;&nbsp; One of the concepts in DDD (I'm still reading the book) is setting up something called a <a href="http://www.domaindrivendesign.org/discussion/messageboardarchive/UbiquitousLanguage.html">ubiquitous language</a>.</p> <p>The ubiquitous language is actually trying to solve a problem that I have seen happen on many projects I've been apart of (but not always my fault).&nbsp; At some point there is a linguistic difference between the nouns that the customer uses and the nouns that the programmers use.&nbsp; By focusing on the language first, you are attempting to head that off at the pass.</p> <p>These nouns consist of your domain classes and your tables and columns, they move everywhere...but not always consistently.&nbsp; Consistency does matter.&nbsp; Inconsistency is what leads to rather confusing conversations with people and everyone scratching their heads about "what just happened".&nbsp; So, this is the middle that I start with.</p> <p>But before you start...</p> <p>One of the other guys in the group was an actual data guy.&nbsp; The systems he has written worked with more data than yours does.&nbsp; Trust me on that.&nbsp; Historically, his biggest consideration was data input speed.&nbsp; I don't think I would start in the middle if that was my biggest concern.&nbsp; In that case you do start with the database, you tune for speed.&nbsp; You also don't use as many domain classes, and you tend to throw away your ORM solution and just hit the database layer directly via Ado.Net.</p> <p>But lets face it, that doesn't happen very often.&nbsp; Most business applications aren't going to touch what the architecture can take.&nbsp; It is amazing how many horribly written applications with very inefficient code run just fine.&nbsp; Most of my speed issues could be solved by indexing tables and changing generic lists to generic dictionaries (more on that later).&nbsp; Architect your applications for how they are going to be used.&nbsp; Most of my web applications have a max usage of 100 simultaneous users.&nbsp; If the data access is slightly slower I'll take that hit for programmer efficiency.&nbsp; If you are designing Amazon.com, you code for through-put.</p> <p>OK, someone is going to disagree with this...I can already feel it.&nbsp; Please take a moment to register your displeasure in the comments.&nbsp; I would love to hear it.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Learning Design Patterns</title>
		<link>http://elegantcode.com/2008/04/28/learning-design-patterns/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=learning-design-patterns</link>
		<comments>http://elegantcode.com/2008/04/28/learning-design-patterns/#comments</comments>
		<pubDate>Mon, 28 Apr 2008 19:01:17 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/28/learning-design-patterns/</guid>
		<description><![CDATA[To me this seems like an interesting way to learn about design patterns (watch the video). I wonder if it also works when I hit an ivory-tower architect with POEAA or DDD? I'll keep you posted. PS: I'm really looking forward to the next episode on code reviews and retrospectives.]]></description>
			<content:encoded><![CDATA[To me <a href="http://codebetter.com/blogs/glenn.block/archive/2008/04/28/extreme-patterns-with-oren.aspx" target="_blank">this</a> seems like an interesting way to learn about design patterns (watch the video). I wonder if it also works when I hit an ivory-tower architect with <a href="http://www.bookpool.com/sm/0321127420" target="_blank">POEAA</a> or <a href="http://www.bookpool.com/sm/0321125215" target="_blank">DDD</a>? I'll keep you posted.

PS: I'm really looking forward to the next episode on code reviews and retrospectives.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/28/learning-design-patterns/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>DTO&#8217;s or Serialized Domain Entities?</title>
		<link>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=dtos-or-serialized-domain-entities</link>
		<comments>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/#comments</comments>
		<pubDate>Sun, 27 Apr 2008 08:09:02 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/</guid>
		<description><![CDATA[I hear this question come up time and time again. It is something that I have been dealing with for a while now, and have some experience with doing both ways. I started writing this in response to a DDD thread, but it got rather large and so I turned it into a post. So [...]]]></description>
			<content:encoded><![CDATA[<p>I hear this question come up time and time again. It is something that I have been dealing with for a while now, and have some experience with doing both ways. I started writing this in response to a <a href="http://tech.groups.yahoo.com/group/domaindrivendesign/message/7286" target="_blank">DDD thread</a>, but it got rather large and so I turned it into a post. So here ya go!  <p>I think you have to write DTO's for any sort of long term Enterprise 'system of systems' solution. I fought this for some time, but since moving to an Assembler/DTO I much enjoy the freedom of being able to easily tweak my DTO "snapshot" of the aggregate root I am working with. Maintaining them is very little overhead.  <p>I tend to think of the DTO as a message, a value object. This is necessary because once an entity leaves the boundary it can no longer function within its original meaning. Its methods may be intended to only run within a given context, which is not the same once on another tier. (Things like lazy loading are a good example) Once the DTO/Message arrives to the caller, it very well may (and probably should) be translated/assembled into another model which can represent its new semantics.  <p>Some other advantages of DTO w/ Assembler or Builder:<br>-Can explicitly control the depth of the graph/tree you are serializing.<br>-Can flatten out the data where needed<br>-Can return significantly less data (maybe you only need 3 fields instead of 15)<br>-Not tightly coupled between remote boundaries (this could be a mess when working with a large team)<br>-Interoperable  <p>I wrote a post a while back showing a way to '<a href="http://elegantcode.com/2007/12/05/fun-with-generics-from-repository-to-dto/" target="_blank">templatize</a>' the process. My current implementations are much different with LINQ now, but maybe it could stimulate some ideas for whomever.  <p><em>The next thing I would like to do is remove the assembler altogether. By flagging fields with attributes or using config to specify a mapping, I could then feed <a href="http://blogs.msdn.com/meek/archive/2008/04/25/using-linq-expressions-to-generate-dynamic-methods.aspx" target="_blank">dynamic methods</a> and run a translation/builder strategy.</em>  <p>I know there are concerns about writing and maintaining assembler and DTO, but to me this is the least of the worry, a more challenging scenario would be the UnitOfWork (How to you intended to track remote state, concurrency etc) How about versioning? performance? scala.. well ilities in general?  <p>These kinds of problems lead one from Request/Response, Patterns of Enterprise Architecture &amp; DDD to a Pub/Sub Message Bus - Enterprise Integration Patterns &amp; DDDD.  <p>And to think all you want to do is send some frickin entities to a remote client&nbsp; :)</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Test Data Builders Refined</title>
		<link>http://elegantcode.com/2008/04/26/test-data-builders-refined/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=test-data-builders-refined</link>
		<comments>http://elegantcode.com/2008/04/26/test-data-builders-refined/#comments</comments>
		<pubDate>Sat, 26 Apr 2008 23:08:27 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/26/test-data-builders-refined/</guid>
		<description><![CDATA[Last year, I blogged about Test Data Builders here and here. I still use them heavily in my unit tests for creating objects with test data. Heck, I also use this pattern for fluent interfaces in production code. Here is a simple example of this approach: &#160; 1: public class CustomerBuilder 2: { 3: public [...]]]></description>
			<content:encoded><![CDATA[<p>Last year, I blogged about Test Data Builders <a href="http://vanryswyckjan.blogspot.com/2007/09/test-data-builder-versus-object-mother.html" target="_blank">here</a> and <a href="http://vanryswyckjan.blogspot.com/2007/12/test-data-builder.html" target="_blank">here</a>. I still use them heavily in my unit tests for creating objects with test data. Heck, I also use this pattern for fluent interfaces in production code. Here is a simple example of this approach:</p>  <p>&#160;</p>  <div>   <div class="csharpcode">     <pre class="alteven"><span class="lnum">   1:</span> <span class="kwrd">public</span> <span class="kwrd">class</span> CustomerBuilder</pre>

    <pre class="alteven"><span class="lnum">   2:</span> {</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     <span class="kwrd">public</span> String _firstName = <span class="str">&quot;Homer&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     <span class="kwrd">public</span> String _lastName = <span class="str">&quot;Simpson&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   5:</span>         </pre>

    <pre class="alteven"><span class="lnum">   6:</span>     <span class="kwrd">public</span> CustomerBuilder WithFirstName(String firstName)</pre>

    <pre class="alteven"><span class="lnum">   7:</span>     {</pre>

    <pre class="alteven"><span class="lnum">   8:</span>         _firstName = firstName;</pre>

    <pre class="alteven"><span class="lnum">   9:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;    </pre>

    <pre class="alteven"><span class="lnum">  10:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  11:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  12:</span>     <span class="kwrd">public</span> CustomerBuilder WithLastName(String lastName)</pre>

    <pre class="alteven"><span class="lnum">  13:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  14:</span>         _lastName = lastName;</pre>

    <pre class="alteven"><span class="lnum">  15:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven"><span class="lnum">  16:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  17:</span>         </pre>

    <pre class="alteven"><span class="lnum">  18:</span>     <span class="kwrd">public</span> Customer Build()</pre>

    <pre class="alteven"><span class="lnum">  19:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  20:</span>         <span class="kwrd">return</span> <span class="kwrd">new</span> Customer(_firstName, _lastName);    </pre>

    <pre class="alteven"><span class="lnum">  21:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  22:</span> }</pre>
  </div>
</div>

<pre class="alteven">This fluent builder class can then be used this way:</pre>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> Customer customer = <span class="kwrd">new</span> CustomerBuilder()</pre>

    <pre class="alteven"><span class="lnum">   2:</span>     .WithFirstName(<span class="str">&quot;Homer&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     .WithLastName(<span class="str">&quot;Simpson&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     .Build();</pre>
  </div>
</div>

<p>A while ago, <a href="http://codebetter.com/blogs/gregyoung/default.aspx" target="_blank">Greg Young</a> started a series of blog posts on DDDD (Distributed Domain-Driven Design), which I can highly recommend. Make sure to catch up now you still can because I think that he has a lot of stuff coming up, which I'm really looking forward to.</p>

<p>Anyhow, Greg had a couple of posts on fluent builders, which you can read <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/15/dddd-5-messages-have-fluent-builders.aspx" target="_blank">here</a>, <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/16/dddd-6-fluent-builders-alternate-ending.aspx" target="_blank">here</a> and <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/22/dddd-8-fluent-builders-and-tests.aspx" target="_blank">here</a>. I noticed an interesting approach in the way that the target object is built. Here is an example of this approach:</p>

<p>&#160;</p>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> <span class="kwrd">public</span> <span class="kwrd">class</span> CustomerBuilder</pre>

    <pre class="alteven"><span class="lnum">   2:</span> {</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     <span class="kwrd">public</span> String _firstName = <span class="str">&quot;Homer&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     <span class="kwrd">public</span> String _lastName = <span class="str">&quot;Simpson&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   5:</span>         </pre>

    <pre class="alteven"><span class="lnum">   6:</span>     <span class="kwrd">public</span> CustomerBuilder WithFirstName(String firstName)</pre>

    <pre class="alteven"><span class="lnum">   7:</span>     {</pre>

    <pre class="alteven"><span class="lnum">   8:</span>         _firstName = firstName;</pre>

    <pre class="alteven"><span class="lnum">   9:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;    </pre>

    <pre class="alteven"><span class="lnum">  10:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  11:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  12:</span>     <span class="kwrd">public</span> CustomerBuilder WithLastName(String lastName)</pre>

    <pre class="alteven"><span class="lnum">  13:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  14:</span>         _lastName = lastName;</pre>

    <pre class="alteven"><span class="lnum">  15:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven"><span class="lnum">  16:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  17:</span>         </pre>

    <pre class="alteven"><span class="lnum">  18:</span>     <span class="kwrd">public</span> Customer Build()</pre>

    <pre class="alteven"><span class="lnum">  19:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  20:</span>         <span class="kwrd">return</span> <span class="kwrd">new</span> Customer(_firstName, _lastName);    </pre>

    <pre class="alteven"><span class="lnum">  21:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  22:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  23:</span>     <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">implicit</span> <span class="kwrd">operator</span> Customer(</pre>

    <pre class="alteven"><span class="lnum">  24:</span>         CustomerBuilder builder) </pre>

    <pre class="alteven"><span class="lnum">  25:</span>     {  </pre>

    <pre class="alteven"><span class="lnum">  26:</span>         <span class="kwrd">return</span> builder.Build()    </pre>

    <pre class="alteven"><span class="lnum">  27:</span>     } </pre>

    <pre class="alteven"><span class="lnum">  28:</span> }</pre>
  </div>
</div>

<p>which results in the following usage:</p>

<p>&#160;</p>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> Customer customer = <span class="kwrd">new</span> CustomerBuilder()</pre>

    <pre class="alteven"><span class="lnum">   2:</span>     .WithFirstName(<span class="str">&quot;Homer&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     .WithLastName(<span class="str">&quot;Simpson&quot;</span>);</pre>
  </div>

  <br />Adding an implicit cast operator to the builder class makes that its no longer required to explicitly call the <em>Build</em> method. I keep the <em>Build</em> method around for backwards-compatibility reasons or in case I ever need it again (violating YAGNI in the process, I know, I know). I find that adding the implicit cast operator adds to the readability of the fluent interface, don't you agree?</div>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/26/test-data-builders-refined/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Name Those Patterns</title>
		<link>http://elegantcode.com/2008/04/24/name-those-patterns/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=name-those-patterns</link>
		<comments>http://elegantcode.com/2008/04/24/name-those-patterns/#comments</comments>
		<pubDate>Thu, 24 Apr 2008 16:16:13 +0000</pubDate>
		<dc:creator>Tony Rasa</dc:creator>
				<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/24/name-those-patterns/</guid>
		<description><![CDATA[In yesterday's discussion of Gang of Four Design Patterns, I said &#34;Never name your classes after the pattern itself.&#34;&#160; But then someone pointed out &#34;What about Factory classes?&#34;&#160; &#34;Oh, ok, ALWAYS name classes after the pattern.&#34;&#160; Except for Singleton or Fa&#231;ade, and maybe a few others.&#160; Ok then, NEVER name after the pattern, except when [...]]]></description>
			<content:encoded><![CDATA[<p>In yesterday's discussion of Gang of Four Design Patterns, I said &quot;Never name your classes after the pattern itself.&quot;&#160; But then someone pointed out &quot;What about Factory classes?&quot;&#160; &quot;Oh, ok, ALWAYS name classes after the pattern.&quot;&#160; Except for Singleton or Fa&#231;ade, and maybe a few others.&#160; Ok then, NEVER name after the pattern, except when you do, sometimes, and then ALWAYS name those, unless otherwise noted.</p>  <p>So, here's a list of the 23 Gang of Four patterns, along with when I'd use the name of the pattern in the implementation.&#160; Unless I didn't want to, and then I wouldn't.&#160; Feel free to agree with this list, unless you don't care to.</p>  <h3>Creational Patterns</h3>  <p><strong>Abstract Factory</strong> - After consideration, I have to say that I call my factories Factory, unless they're Repositories, Data Access Layers, DAOs, or something else.&#160; But most of the time, Factory.&#160; Because this would just be wrong:</p>  <pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> FooCreatorMakerThingy { ... }</pre>

<p><strong>Builder</strong> - Hmm, another noun. Guess I'd use this one too, for the same reason as Abstract Factory. Most of the time.&#160; </p>

<p><strong>Factory Method</strong> - I probably would not call my Factory Methods &quot;GetFooFactoryMethod()&quot; because that just looks weird.&#160;&#160; Maybe if we verbify the noun:</p>

<pre class="csharpcode"><span class="kwrd">public</span> Foo FactorialMethodizeFoo() {...}</pre>

<p>Nope, that's still dumb.</p>

<p><strong>Prototype</strong> - &quot;Specify the kinds of objects to create using a prototypical instance...&quot; So we don't care that the class is a &quot;Prototype,&quot; but we do care about the &quot;kind of object&quot; part.&#160; So don't name your prototypical classes &quot;FooPrototype.&quot;&#160; (Unless you just want to.)</p>

<p><strong>Singleton</strong> - Somebody have a case where it makes sense to name your Singleton &quot;Singleton&quot;?&#160; I can't come up with one.&#160; Anyway, you shouldn't be using singletons anymore anyway.&#160; :)</p>

<h3>Structural Patterns</h3>

<p><strong>Adapter</strong> - Adapters are pretty much called Adapters.&#160; Except when they're called Wrappers.&#160; So you can use this name too.</p>

<p><strong>Bridge</strong> - I wouldn't label a participant in a bridge pattern &quot;FooBridge,&quot; unless I was trying to impress on an interview test or something.&#160; And even then, that's a bad idea.&#160; &quot;Ohh, look, I know a complicated design pattern!&#160; Lookitme!&quot;&#160; So don't call your bridges Bridges.&#160; </p>

<p><strong>Composite</strong> - &quot;One of the goals of the Composite&#160; pattern is to make clients unaware of the specific Leaf or Composite classes they're using.&quot;&#160; So you don't want to point out that Composites are in play, that breaks the suspension of disbelief for your audience.&#160; Or something.&#160; Anyway Composites get used all over the place, if we had to label them all then it'd be FooComposite, BarComposite, TreeComposite, EverythingComposite, and what's the fun in that.</p>

<p><strong>Decorator </strong>- I think in general people don't label their Decorators Decorators.&#160; I'm sure there's a counter-example to this, but all of the Stream- and Xml-handling classes I can think of don't point out that they are Decorators.&#160; You'd probably want to stick to more descriptive names, like ASCIIStreamReader instead of cluttering things up with ASCIIStreamDecorator.&#160; We're not decorating the stream (in a literal sense), we're reading it.&#160; </p>

<p><strong>Fa&#231;ade</strong> - Don't call your Fa&#231;ade &quot;FooFa&#231;ade,&quot; that's just silly.&#160; Besides, its too much work to type that cedille into the editor if you're using an ignorant American keyboard.</p>

<p><strong>Flyweight </strong>- This is another pattern where we're going to disguise what's going on inside, in order to simplify the client's usage and make better use of resources.&#160; No reason to flaunt to the client that we're low on resources, because that's a personal and private matter - times are tough all over, and there's no shame in struggling a little bit, and hey who could have predicted that the sub-prime loan market was going to ... anyway, back to patterns.&#160;&#160; Don't call your Flyweights Flyweights, unless what you're modeling is actually a Flyweight, and then thats OK.</p>

<p><strong>Proxy </strong>- This one is standard terminology.&#160; So Proxy's ok.</p>

<h3>Behavioral Patterns (or &quot;<em>MAN this book has a lot of pages in it..</em>&quot;)</h3>

<p><strong>Chain of Responsibility </strong>- Don't even think of naming a class ChainOfResponsibility.&#160; Even with ReSharper, who'd want to have to type that?</p>

<p><strong>Command</strong> - Another convenient noun.&#160; Ok, you can create Commands.&#160; </p>

<p><strong>Interpreter </strong>- This is another pattern that doesn't have one identifying class that you could label &quot;The Interpreter.&quot;&#160; But you might create a Fa&#231;ade, and call that class Interpreter.&#160; I'm not sure if naming one pattern after another is OK or not.&#160; Makes my head hurt.&#160; </p>

<p>Iterator - Another convenient noun.&#160; Iterate away!&#160; Not that you'll have to worry too much about naming classes with this pattern if you use C#, Java, C++ or pretty much any other modern language, since this pattern is built-in.</p>

<p><strong>Mediator </strong>- This pattern promotes loose coupling, most of the time I'd call what it does a &quot;Controller,&quot; or what's being Mediated is more on a service level, so perhaps the word &quot;Service&quot; would end up in the name.&#160; Can't say I've ever called a class specifically &quot;FooMediator&quot;, but I suppose I don't have a problem with it if you want to do that.</p>

<p><strong>Memento </strong>- This pattern captures and externalizes an object's state.&#160; So I think I'd call something like it &quot;State&quot; before &quot;Memento,&quot; since it's fewer letters to type.&#160; Besides, I had to see that movie twice before I really appreciated it.&#160; </p>

<p>Ok, that was just lame, sorry.</p>

<p><strong>Observer </strong>- Oh, you mean like an Event?&#160; Or more like a delegate?&#160; :)</p>

<p><strong>State </strong>- I'd call my implementation of this pattern &quot;Memento,&quot; just to confuse you.&#160; That's just how I roll.</p>

<p><strong>Strategy </strong>- This is another pattern that it seems like we use all the time, without having to give it a fancy Ivory Tower name.&#160; As someone pointed out (sorry I lost the reference to who said that) Microsoft calls all their Strategy implementations Providers now anyway.&#160; Rather than label something a &quot;Strategy&quot; I'd probably just say what the thing does: &quot;CryptographicProvider.&quot;</p>

<p><strong>Template Method</strong> - Yet another pattern where there's no obvious place to hang the name off of, so don't try to force it in there and impress your coworkers with how smart you are.&#160; We know, you're a Design Pattern God, no need to brag about it.</p>

<p><strong>Visitor </strong>- Hmm, not sure where I stand on this one.&#160; I have used this pattern where I've named a visitor &quot;SqlGeneratingVisitor.&quot;&#160; But maybe that was a mistake?&#160; So how about we just declare this one &quot;Free Parking,&quot; and you can do what you want.</p>

<p>&#160;</p>

<p>So, there you go.&#160; A bunch of design patterns, summarized into one neat page.&#160; Now you're ready to ace that interview!&#160; </p>

<p>&#160;</p>

<p><em>disclaimer: I make no guarantees that any or all of the information will not harm you in a technical interview, and if repeating this to an interview board gets your removed from the building by security, its not my fault.&#160; This information comes without warantees or conditions of any kind, and you probably shouldn't have bothered reading it..</em></p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/24/name-those-patterns/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.Net HierarchicalDataSource&lt;T&gt;</title>
		<link>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=aspnet-hierarchicaldatasourcet</link>
		<comments>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/#comments</comments>
		<pubDate>Sun, 06 Apr 2008 06:45:44 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Extension Methods]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/</guid>
		<description><![CDATA[The Asp.net 2.0 tree control, as well as many 3rd party controls binds to hierarchical data via an IHierarchicalDataSource interface. Asp.net provides an implementation OOTB with the HierarchicalDataSourceControl! Easy, let’s hook that up to our… wait this control only takes data in xml format or from a sitemap? Hmm, we don’t want to convert our [...]]]></description>
			<content:encoded><![CDATA[<p>The Asp.net 2.0 tree control, as well as many 3rd party controls binds to hierarchical data via an IHierarchicalDataSource interface. Asp.net provides an implementation OOTB with the HierarchicalDataSourceControl! Easy, let’s hook that up to our… wait this control only takes data in xml format or from a sitemap? Hmm, we don’t want to convert our model to xml, and a sitemap seems inappropriate. We spend a lot time doing great patterns such as MVP &amp; MVC, we should just be able to bind up a list of model objects we already have.  <p>Good thing MS has exposed a set of interfaces and abstract classes in System.Web.UI for us to implement our own IHierarchicalDataSource and HierarchicalDataSourceView and IHierarchicalEnumerable and then IHierarchyData... geesh.  <p>Data? So let's see here, in order to bind our model to the tree control it needs to implement an interface in System.Web.UI? Well that's just not going to happen, so its time for some Elegant Code.  <p><strong>Story:</strong>  <p>Dude, as a software developer, needs to create a custom implementation of IHierarchicalDataSource, and the rest of its parts, so that he can bind a list of objects to a tree control in asp.net  <ul> <li>Use existing model to bind to tree control  <li>Must be able to reuse data source for future models  <li>Model must not contain a reference to System.Web.UI </li></ul> <p>In order to setup our model to address the acceptance criteria we are going to use an interface with a self-referencing generic declaration, where the type will specify itself as the concrete type. This will let us create a nice generic hierarchical model to pass into our datasource without resorting to any sort of base class.  <div><pre class="csharpcode"><span class="kwrd">namespace</span> POC.Common
{
    <span class="kwrd">public</span> <span class="kwrd">interface</span> IModelWithHierarchy&lt;T&gt;
    {
        <span class="kwrd">string</span> Name { get; set; }
        T Parent { get; set; }
        List&lt;T&gt; Children { get; set; }
    }
}</pre><em>Lets note that we have placed the interface in a Common namespace which represents a shared.dll between Web &amp; Model</em> </div>
<p>The UI part of criteria is now a bit more tricky because the HierarchicalDataSourceView expects a IHierarchicalEnumerable datasource which contains IHierarchyData. Said IHierarchyData != IModelWithHierarchy&lt;T&gt;.&nbsp; Luckily someone else has already figured this one out for us. We need an <a href="http://www.dofactory.com/Patterns/PatternAdapter.aspx" target="_blank">adapter</a>. 
<p><a href="http://elegantcode.com/wp-content/uploads/2008/04/image7.png" target="_blank"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="123" alt="image" src="http://elegantcode.com/wp-content/uploads/2008/04/image-thumb7.png" width="244" border="0"></a> 
<p>To instantiate our HierarchyData adapter objects we will use an extension method and linq 'Select' to convert our datasource IEnumerable&lt;T&gt; of IModelWithHierarchy&lt;T&gt; 's to an IHierarchicalEnumerable of IHierarchyData. 
<div><pre class="csharpcode"><span class="kwrd">namespace</span> POC.Web.HierarchyExtensions
{
    <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">class</span> HierarchyConverter
    {
        <span class="kwrd">public</span> <span class="kwrd">static</span> HierarchicalModelList
            ToHierarchicalModelList&lt;T&gt;(<span class="kwrd">this</span> IEnumerable&lt;T&gt; modelWithHierarchy)
            <span class="kwrd">where</span> T : IModelWithHierarchy&lt;T&gt;
        {
            <span class="kwrd">return</span> <span class="kwrd">new</span> HierarchicalModelList(
                modelWithHierarchy.Select(m =&gt; <span class="kwrd">new</span> HierarchyData&lt;T&gt;(m) <span class="kwrd">as</span> IHierarchyData));
        }
    }
}</pre></div>
<p>I am not going to bore you with the implementation details of the HierarachyData&lt;T&gt; adapter and the .net abstractions, but have dropped the <a href="http://elegantcode.com/wp-content/uploads/2008/04/hierarchydatasourcepoc.zip" target="_blank">source code here</a> if your interested. The end result: 
<div><pre class="csharpcode">TreeView1.DataSource = <span class="kwrd">new</span> HierarchicalModelDataSource&lt;Category&gt; { DataSource = Categories };
TreeView1.DataBind();
TreeView1.CollapseAll();</pre></div>
<p>Story complete!&nbsp; </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is Elegant Code to me?</title>
		<link>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-is-elegant-code-to-me</link>
		<comments>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/#comments</comments>
		<pubDate>Mon, 31 Mar 2008 03:47:45 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Patterns and Practices]]></category>
		<category><![CDATA[Widgets of Wisdom]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/</guid>
		<description><![CDATA[This question keeps popping up around here ("around here" being the loose conglomeration that makes up the Elegant Code group).&#160; It isn't easy to describe.&#160; And really, the notion of what constitutes elegance in code changes over time.&#160; There is no static "this is good code" test, and I doubt there ever will be.&#160; Plus, [...]]]></description>
			<content:encoded><![CDATA[<p>This question keeps popping up around here ("around here" being the loose conglomeration that makes up the Elegant Code group).&nbsp; It isn't easy to describe.&nbsp; And really, the notion of what constitutes elegance in code changes over time.&nbsp; There is no static "this is good code" test, and I doubt there ever will be.&nbsp; Plus, what makes good code in one language, may not apply to the next.</p> <p>So let me state for the record: today's elegant code is tomorrow's drivel.&nbsp; Don't feel bad, many writers have the same problems.&nbsp; What was super amazing back in the day is now rubbish or near unreadable.&nbsp; I am thinking of the Victorian writing that Hemingway usurped, and now Hemingway himself is almost unreadable (to me anyway).&nbsp; Tastes change with the times.&nbsp; That is a simple fact.</p> <p>So what can you do about this?&nbsp; As I say that old writing sucks to read (read Washington Irvine lately?), great works of literature still abound (for instance, Hemingway is still a great writer -- even if I prefer Tolkien) .&nbsp; So take some notes from them, and from other crafts in looking for the answer.&nbsp; If you want a cliff notes version of what this is going to tell you, it is this: you must always push yourself to get better.</p> <ol> <li>Find a good teacher.&nbsp; Nothing is better than sitting at the feet of a master who can nudge you along in the right direction.&nbsp; While early mistakes can often be corrected easily with a little bit of guidance, after they have had some time to fester all bets are off.&nbsp; I believe a fare number of "Anit-patterns" were created by self taught developers (find <a href="http://elegantcode.com/2008/03/21/sql-ejaculation/">SQL Ejaculation</a> on this site).&nbsp; <li>Read.&nbsp;&nbsp; Good writer are first good readers.&nbsp; Start with Code Complete, move into a good Patterns book, get MSDN Magazine, find some bloggers your like, but keep moving.&nbsp; You will NEVER be done reading.&nbsp; I imagine that even Martin Fowler has a "to read" booklist a mile long.  <li>Read code.&nbsp;&nbsp; There are a plethora of open source project just waiting to be read -- do so.&nbsp; This is the single best way to expand your coding repertoire, find things your language can do that you didn't even know about..&nbsp;&nbsp; Scott Hanselman has recently popularized this idea, and I thank him for it.  <li>Practice.&nbsp; This means writing small applications at home.&nbsp; You get an idea that you want to try out -- do it.&nbsp; I don't mean you have to write finished applications, just have some exploring time.&nbsp; I remember talking with an 80 year old master woodworker (he lives down the street from me), who was telling me how many time he would practice making dovetail joints before he felt competent.&nbsp; It was years worth.  <li>Pay Attention.&nbsp; Being good at anything really amounts to that.&nbsp; And don't just pay attention when reading, writing, or talking about code.&nbsp; Inspiration come from everywhere, any good artist will tell you that.&nbsp; Pay attention when you cook, when you work on the car, when you are wood working, playing an instrument, whatever it is that you do.&nbsp; This will help you in the end, event if it is just learning the amount of dedication it really takes to become good at something.  <li>Beware of preferences.&nbsp; Any time I hear someone start a statement with "I prefer code to ..." you know things are going downhill.&nbsp; If you find someone who cares more about how your code is formatted then how it is written you have found yourself in a mess.&nbsp; More importantly, beware of them in yourself.&nbsp;&nbsp; Having code style standard is important, but it isn't worth loosing sleep over.&nbsp; This also pertains to inheritance, patterns, use of particular classes (e.g. always using the generic list class in C# when a Dictionary would be better).&nbsp; <li>No Sacred Cows. Believe no single source of information.&nbsp; This means not thinking that Microsoft, Sun, IBM, Richard Stallman, or anyone else will have all of the correct answers.&nbsp; Apply the scientific method and do some research, experiment, and question the authority.&nbsp;&nbsp; There should be no sacred cows in programming.  <li>Talk with others.&nbsp; Join a user group, show up every now and then, heckle the presenter, and -maybe- speak.&nbsp; You want a broad range of people who you can talk to and bounce ideas off of.&nbsp; This will help you from becoming that crazy guy in the corner who vehemently states that all your code should be in one file.&nbsp; Having a mentor (mentioned earlier) is great, but having people around to push you from multiple directions is also good.  <li>Learn languages.&nbsp; This gets back to the "read code" idea. Make that multiple types of languages as well.&nbsp; If you know C# or Java and SQL, learn Python or Ruby, get really good at JavaScript.&nbsp; If you want something really out there, learn MDX.&nbsp; This is also a repertoire thing.&nbsp; Seeing how other languages work will make you rethink your ideas about what your current code, in whatever languages you are working in, should look like.  <li>Keep Thinking!!!&nbsp; That is possibly the most important point.&nbsp; Keep thinking.&nbsp; Don't just evaluate something once and never return, go back and reevaluate. Did the technique work?&nbsp; How could it have been better?&nbsp;&nbsp; The idea is continual improvement.  <li>Switch jobs every now and then.&nbsp; When I announced I was leaving my first programming job out of college, the VP of R&amp;D had me sit down with him and talk.&nbsp; He wasn't trying to keep me (he knew I was moving to be closer to family), he wanted to give me some advice.&nbsp; He told me to change jobs every three years.&nbsp; After you have been with a place for three years you have probably learned everything you are going to learn and it time to move on.&nbsp; This doesn't mean changing companies either.&nbsp; I've been with the same company now for four years, but I've had three different jobs.&nbsp; If you have been writing commercial applications, become a consultant - or vise-versa, become a test engineer, expand your boundaries.&nbsp; Again, this is about pushing yourself to be better.  <li>Have a Hobby. But don't ask about me, I have too many.&nbsp;&nbsp; Creating software is about creating things.&nbsp; So I suggest picking a hobby that involves creating something.&nbsp; Popular hobbies amongst programmers I know include: cooking, music, woodworking, beer making, and BBQ (which is different than cooking).&nbsp; But don't discount sports (golf is always popular), computer games, and board games.&nbsp; Anything that promotes the development of skill levels can't be a bad thing. </li></ol> <p>And I'm stopping there.&nbsp; This is my beginner's guide to how to become the writer elegant code.&nbsp; If you have others you think I missed, add them too the comments.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>LINQ Framework Design Guidelines</title>
		<link>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=linq-framework-design-guidelines-2</link>
		<comments>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/#comments</comments>
		<pubDate>Wed, 19 Mar 2008 01:53:22 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[Microsoft]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/</guid>
		<description><![CDATA[Excellent article on design considerations for LINQ. Looks like these concepts will be incorporated into the 2nd Edition of Framework Design Guidelines: Conventions, Idioms, and Patterns for Reuseable .NET Libraries. Cant wait to get my hands on a copy of that!]]></description>
			<content:encoded><![CDATA[<p>Excellent article on <a href="http://blogs.msdn.com/mirceat/archive/2008/03/13/linq-framework-design-guidelines.aspx" target="_blank">design considerations for LINQ</a>. Looks like these concepts will be incorporated into the 2nd Edition of <a href="http://www.amazon.com/Framework-Design-Guidelines-Conventions-Development/dp/0321545613/ref=pd_bbs_sr_2?ie=UTF8&amp;s=books&amp;qid=1200508590&amp;sr=1-2" target="_blank">Framework Design Guidelines: Conventions, Idioms, and Patterns for Reuseable .NET Libraries</a>. Cant wait to get my hands on a copy of that!</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Boise Code Camp 2008</title>
		<link>http://elegantcode.com/2008/03/09/boise-code-camp-2008/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=boise-code-camp-2008</link>
		<comments>http://elegantcode.com/2008/03/09/boise-code-camp-2008/#comments</comments>
		<pubDate>Mon, 10 Mar 2008 02:25:35 +0000</pubDate>
		<dc:creator>Alex Mueller</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Code Camp]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Patterns and Practices]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/09/boise-code-camp-2008/</guid>
		<description><![CDATA[Boise Code Camp 2008 was awesome. Thank you to David Starr and his wife, Eleanor, for taking ownership of this event and dedicating many months of their time to make this a reality. Thank you to the presenters for having the passion and desire to present technologies and practices, both new and not-as-new. Thank you [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Boise Code Camp 2008" href="http://www.boisecodecamp.org/" target="_blank">Boise Code Camp 2008</a> was awesome. Thank you to <a title="David Starr" href="http://elegantcode.com/about/david-starr/" target="_blank">David Starr</a> and his wife, <a title="Domestic Oblivion" href="http://domesticoblivion.com/" target="_blank">Eleanor</a>, for taking ownership of this event and dedicating many months of their time to make this a reality. Thank you to the presenters for having the passion and desire to present technologies and practices, both new and not-as-new. Thank you to the many volunteers who put in countless hours to help coordinate the weekend's events. Thank you to the campers for attending and making this year's code camp a success.</p>  <p>I am sure everyone will be blogging about all the great sessions they attended at code camp. I enjoyed all of the presentations I attended, and I am excited to explore some new material. </p>  <p>I want to share a few thoughts on my presentation dealing with Model-View-Presenter in ASP.NET after listening to <a title="Scott Hanselman" href="http://www.hanselman.com" target="_blank">Scott Hanselman's</a> session on the <a title="ASP. NET MVC Project" href="http://www.asp.net/mvc" target="_blank">ASP.NET MVC Project</a>. I may have the percentage incorrect, but Scott mentioned something like it is predicted that only 10% of the ASP.NET community currently using Web Forms will adopt and use the MVC framework. The MVC framework is an addition to ASP.NET, it is not a replacement. What this means is that Model-View-Presenter will still be a viable pattern to implement with your ASP.NET applications, and it is not going away. ASP.NET Web Forms will not be going away either.</p>  <p>Use MVP to get your third party controls under test. Use MVP to provide that separation of concerns in your legacy applications. Use it entirely or in conjunction with the MVC framework. It is all about testability. <a title="Glenn Block" href="http://blogs.msdn.com/gblock/" target="_blank">Glenn Block</a> presented on the Web Client Software Factory, and what pattern does this implement? Model-View-Presenter.</p>  <p>I am providing my <a title="Presentation and Source Code" href="http://muellerdesigns.net/DasBlog/content/binary/ModeViewPresenter.AlexMueller.BoiseCodeCamp2008.rar" target="_blank">presentation and source code (3.67 MB)</a> from my talk on MVP. It will be available via the <a title="Boise Code Camp" href="http://www.boisecodecamp.org/" target="_blank">Boise Code Camp</a> site as well. </p>  <p>Again, thank the many individuals and their families who sacrificed their time to bring to the local community this years code camp.</p>  <p>Around the office's water cooler on Monday, I will be able to say, &quot;and this one time, at code camp...&quot;</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/09/boise-code-camp-2008/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Microsoft releases Unity, an IoC library</title>
		<link>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-is-elegant-code-to-me</link>
		<comments>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/#comments</comments>
		<pubDate>Mon, 31 Mar 2008 03:47:45 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Patterns and Practices]]></category>
		<category><![CDATA[Widgets of Wisdom]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/</guid>
		<description><![CDATA[This question keeps popping up around here ("around here" being the loose conglomeration that makes up the Elegant Code group).&#160; It isn't easy to describe.&#160; And really, the notion of what constitutes elegance in code changes over time.&#160; There is no static "this is good code" test, and I doubt there ever will be.&#160; Plus, [...]]]></description>
			<content:encoded><![CDATA[<p>This question keeps popping up around here ("around here" being the loose conglomeration that makes up the Elegant Code group).&nbsp; It isn't easy to describe.&nbsp; And really, the notion of what constitutes elegance in code changes over time.&nbsp; There is no static "this is good code" test, and I doubt there ever will be.&nbsp; Plus, what makes good code in one language, may not apply to the next.</p> <p>So let me state for the record: today's elegant code is tomorrow's drivel.&nbsp; Don't feel bad, many writers have the same problems.&nbsp; What was super amazing back in the day is now rubbish or near unreadable.&nbsp; I am thinking of the Victorian writing that Hemingway usurped, and now Hemingway himself is almost unreadable (to me anyway).&nbsp; Tastes change with the times.&nbsp; That is a simple fact.</p> <p>So what can you do about this?&nbsp; As I say that old writing sucks to read (read Washington Irvine lately?), great works of literature still abound (for instance, Hemingway is still a great writer -- even if I prefer Tolkien) .&nbsp; So take some notes from them, and from other crafts in looking for the answer.&nbsp; If you want a cliff notes version of what this is going to tell you, it is this: you must always push yourself to get better.</p> <ol> <li>Find a good teacher.&nbsp; Nothing is better than sitting at the feet of a master who can nudge you along in the right direction.&nbsp; While early mistakes can often be corrected easily with a little bit of guidance, after they have had some time to fester all bets are off.&nbsp; I believe a fare number of "Anit-patterns" were created by self taught developers (find <a href="http://elegantcode.com/2008/03/21/sql-ejaculation/">SQL Ejaculation</a> on this site).&nbsp; <li>Read.&nbsp;&nbsp; Good writer are first good readers.&nbsp; Start with Code Complete, move into a good Patterns book, get MSDN Magazine, find some bloggers your like, but keep moving.&nbsp; You will NEVER be done reading.&nbsp; I imagine that even Martin Fowler has a "to read" booklist a mile long.  <li>Read code.&nbsp;&nbsp; There are a plethora of open source project just waiting to be read -- do so.&nbsp; This is the single best way to expand your coding repertoire, find things your language can do that you didn't even know about..&nbsp;&nbsp; Scott Hanselman has recently popularized this idea, and I thank him for it.  <li>Practice.&nbsp; This means writing small applications at home.&nbsp; You get an idea that you want to try out -- do it.&nbsp; I don't mean you have to write finished applications, just have some exploring time.&nbsp; I remember talking with an 80 year old master woodworker (he lives down the street from me), who was telling me how many time he would practice making dovetail joints before he felt competent.&nbsp; It was years worth.  <li>Pay Attention.&nbsp; Being good at anything really amounts to that.&nbsp; And don't just pay attention when reading, writing, or talking about code.&nbsp; Inspiration come from everywhere, any good artist will tell you that.&nbsp; Pay attention when you cook, when you work on the car, when you are wood working, playing an instrument, whatever it is that you do.&nbsp; This will help you in the end, event if it is just learning the amount of dedication it really takes to become good at something.  <li>Beware of preferences.&nbsp; Any time I hear someone start a statement with "I prefer code to ..." you know things are going downhill.&nbsp; If you find someone who cares more about how your code is formatted then how it is written you have found yourself in a mess.&nbsp; More importantly, beware of them in yourself.&nbsp;&nbsp; Having code style standard is important, but it isn't worth loosing sleep over.&nbsp; This also pertains to inheritance, patterns, use of particular classes (e.g. always using the generic list class in C# when a Dictionary would be better).&nbsp; <li>No Sacred Cows. Believe no single source of information.&nbsp; This means not thinking that Microsoft, Sun, IBM, Richard Stallman, or anyone else will have all of the correct answers.&nbsp; Apply the scientific method and do some research, experiment, and question the authority.&nbsp;&nbsp; There should be no sacred cows in programming.  <li>Talk with others.&nbsp; Join a user group, show up every now and then, heckle the presenter, and -maybe- speak.&nbsp; You want a broad range of people who you can talk to and bounce ideas off of.&nbsp; This will help you from becoming that crazy guy in the corner who vehemently states that all your code should be in one file.&nbsp; Having a mentor (mentioned earlier) is great, but having people around to push you from multiple directions is also good.  <li>Learn languages.&nbsp; This gets back to the "read code" idea. Make that multiple types of languages as well.&nbsp; If you know C# or Java and SQL, learn Python or Ruby, get really good at JavaScript.&nbsp; If you want something really out there, learn MDX.&nbsp; This is also a repertoire thing.&nbsp; Seeing how other languages work will make you rethink your ideas about what your current code, in whatever languages you are working in, should look like.  <li>Keep Thinking!!!&nbsp; That is possibly the most important point.&nbsp; Keep thinking.&nbsp; Don't just evaluate something once and never return, go back and reevaluate. Did the technique work?&nbsp; How could it have been better?&nbsp;&nbsp; The idea is continual improvement.  <li>Switch jobs every now and then.&nbsp; When I announced I was leaving my first programming job out of college, the VP of R&amp;D had me sit down with him and talk.&nbsp; He wasn't trying to keep me (he knew I was moving to be closer to family), he wanted to give me some advice.&nbsp; He told me to change jobs every three years.&nbsp; After you have been with a place for three years you have probably learned everything you are going to learn and it time to move on.&nbsp; This doesn't mean changing companies either.&nbsp; I've been with the same company now for four years, but I've had three different jobs.&nbsp; If you have been writing commercial applications, become a consultant - or vise-versa, become a test engineer, expand your boundaries.&nbsp; Again, this is about pushing yourself to be better.  <li>Have a Hobby. But don't ask about me, I have too many.&nbsp;&nbsp; Creating software is about creating things.&nbsp; So I suggest picking a hobby that involves creating something.&nbsp; Popular hobbies amongst programmers I know include: cooking, music, woodworking, beer making, and BBQ (which is different than cooking).&nbsp; But don't discount sports (golf is always popular), computer games, and board games.&nbsp; Anything that promotes the development of skill levels can't be a bad thing. </li></ol> <p>And I'm stopping there.&nbsp; This is my beginner's guide to how to become the writer elegant code.&nbsp; If you have others you think I missed, add them too the comments.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; Design Patterns</title>
	<atom:link href="http://elegantcode.com/tag/design-patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Where to start a new program</title>
		<link>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=where-to-start-a-new-program</link>
		<comments>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/#comments</comments>
		<pubDate>Sun, 08 Jun 2008 19:50:39 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/06/08/where-to-start-a-new-program/</guid>
		<description><![CDATA[Here as an interesting question that popped up at my user group meeting last night, when you are starting a new program (green field development), where do you get started?&#160; I find these questions interesting because there really is no correct answer, but a persons answer will tell you something about how they problem solve. [...]]]></description>
			<content:encoded><![CDATA[<p>Here as an interesting question that popped up at my user group meeting last night, when you are starting a new program (green field development), where do you get started?&nbsp; I find these questions interesting because there really is no correct answer, but a persons answer will tell you something about how they problem solve.</p> <p>Depending on who you ask you will likely get one of two answers.&nbsp; If you are talking with a data guy, he will say to start with the database.&nbsp; If you are dealing with a visual guy, he will say to start with the UI.&nbsp; Historically, I did a conglomeration of those two ideas, creating both the UI and the database at the same time, letting the two drive each other.&nbsp; But I'm weird, I'm a visual/data guy.</p> <p>Lately though, I've started to thing about a third approach.&nbsp; Starting in the middle.&nbsp; No UI, no database, just the data classes.&nbsp; This approach has been spurred on buy a couple of things.&nbsp; Namely, test driven development (TDD), design patterns, and domain driven design.</p> <p>TDD really is the glue that allows all of this to happen.&nbsp; I can start in the middle because I can run the code in the middle any time I want.&nbsp; I just have to write a test.&nbsp; No having to wade through multiple layers of UI to test out some small chunk ok UI is a great productivity gain for me.</p> <p>Next is design patterns.&nbsp; This is what gives you a reference for how to approach your code.&nbsp; That said, this isn't about making everything absolutely perfect from the get go, but it is nice to know where you are going to made your comprises early on.</p> <p>But all of that does not explain where I begin.&nbsp; That is where Domain Driven Design (DDD) kicks in.&nbsp; For those not familiar with the concept, you can read up on <a href="http://en.wikipedia.org/wiki/Domain-driven_design">wikipedia</a>, buy <a href="http://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1212806974&amp;sr=8-1">The Book</a>, or buy one of the <a href="http://www.amazon.com/s?ie=UTF8&amp;tag=mozilla-20&amp;index=blended&amp;link%5Fcode=qs&amp;field-keywords=domain%20driven%20design&amp;sourceid=Mozilla-search">various other references</a>.&nbsp;&nbsp; One of the concepts in DDD (I'm still reading the book) is setting up something called a <a href="http://www.domaindrivendesign.org/discussion/messageboardarchive/UbiquitousLanguage.html">ubiquitous language</a>.</p> <p>The ubiquitous language is actually trying to solve a problem that I have seen happen on many projects I've been apart of (but not always my fault).&nbsp; At some point there is a linguistic difference between the nouns that the customer uses and the nouns that the programmers use.&nbsp; By focusing on the language first, you are attempting to head that off at the pass.</p> <p>These nouns consist of your domain classes and your tables and columns, they move everywhere...but not always consistently.&nbsp; Consistency does matter.&nbsp; Inconsistency is what leads to rather confusing conversations with people and everyone scratching their heads about "what just happened".&nbsp; So, this is the middle that I start with.</p> <p>But before you start...</p> <p>One of the other guys in the group was an actual data guy.&nbsp; The systems he has written worked with more data than yours does.&nbsp; Trust me on that.&nbsp; Historically, his biggest consideration was data input speed.&nbsp; I don't think I would start in the middle if that was my biggest concern.&nbsp; In that case you do start with the database, you tune for speed.&nbsp; You also don't use as many domain classes, and you tend to throw away your ORM solution and just hit the database layer directly via Ado.Net.</p> <p>But lets face it, that doesn't happen very often.&nbsp; Most business applications aren't going to touch what the architecture can take.&nbsp; It is amazing how many horribly written applications with very inefficient code run just fine.&nbsp; Most of my speed issues could be solved by indexing tables and changing generic lists to generic dictionaries (more on that later).&nbsp; Architect your applications for how they are going to be used.&nbsp; Most of my web applications have a max usage of 100 simultaneous users.&nbsp; If the data access is slightly slower I'll take that hit for programmer efficiency.&nbsp; If you are designing Amazon.com, you code for through-put.</p> <p>OK, someone is going to disagree with this...I can already feel it.&nbsp; Please take a moment to register your displeasure in the comments.&nbsp; I would love to hear it.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Learning Design Patterns</title>
		<link>http://elegantcode.com/2008/04/28/learning-design-patterns/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=learning-design-patterns</link>
		<comments>http://elegantcode.com/2008/04/28/learning-design-patterns/#comments</comments>
		<pubDate>Mon, 28 Apr 2008 19:01:17 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/28/learning-design-patterns/</guid>
		<description><![CDATA[To me this seems like an interesting way to learn about design patterns (watch the video). I wonder if it also works when I hit an ivory-tower architect with POEAA or DDD? I'll keep you posted. PS: I'm really looking forward to the next episode on code reviews and retrospectives.]]></description>
			<content:encoded><![CDATA[To me <a href="http://codebetter.com/blogs/glenn.block/archive/2008/04/28/extreme-patterns-with-oren.aspx" target="_blank">this</a> seems like an interesting way to learn about design patterns (watch the video). I wonder if it also works when I hit an ivory-tower architect with <a href="http://www.bookpool.com/sm/0321127420" target="_blank">POEAA</a> or <a href="http://www.bookpool.com/sm/0321125215" target="_blank">DDD</a>? I'll keep you posted.

PS: I'm really looking forward to the next episode on code reviews and retrospectives.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/28/learning-design-patterns/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>DTO&#8217;s or Serialized Domain Entities?</title>
		<link>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=dtos-or-serialized-domain-entities</link>
		<comments>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/#comments</comments>
		<pubDate>Sun, 27 Apr 2008 08:09:02 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/</guid>
		<description><![CDATA[I hear this question come up time and time again. It is something that I have been dealing with for a while now, and have some experience with doing both ways. I started writing this in response to a DDD thread, but it got rather large and so I turned it into a post. So [...]]]></description>
			<content:encoded><![CDATA[<p>I hear this question come up time and time again. It is something that I have been dealing with for a while now, and have some experience with doing both ways. I started writing this in response to a <a href="http://tech.groups.yahoo.com/group/domaindrivendesign/message/7286" target="_blank">DDD thread</a>, but it got rather large and so I turned it into a post. So here ya go!  <p>I think you have to write DTO's for any sort of long term Enterprise 'system of systems' solution. I fought this for some time, but since moving to an Assembler/DTO I much enjoy the freedom of being able to easily tweak my DTO "snapshot" of the aggregate root I am working with. Maintaining them is very little overhead.  <p>I tend to think of the DTO as a message, a value object. This is necessary because once an entity leaves the boundary it can no longer function within its original meaning. Its methods may be intended to only run within a given context, which is not the same once on another tier. (Things like lazy loading are a good example) Once the DTO/Message arrives to the caller, it very well may (and probably should) be translated/assembled into another model which can represent its new semantics.  <p>Some other advantages of DTO w/ Assembler or Builder:<br>-Can explicitly control the depth of the graph/tree you are serializing.<br>-Can flatten out the data where needed<br>-Can return significantly less data (maybe you only need 3 fields instead of 15)<br>-Not tightly coupled between remote boundaries (this could be a mess when working with a large team)<br>-Interoperable  <p>I wrote a post a while back showing a way to '<a href="http://elegantcode.com/2007/12/05/fun-with-generics-from-repository-to-dto/" target="_blank">templatize</a>' the process. My current implementations are much different with LINQ now, but maybe it could stimulate some ideas for whomever.  <p><em>The next thing I would like to do is remove the assembler altogether. By flagging fields with attributes or using config to specify a mapping, I could then feed <a href="http://blogs.msdn.com/meek/archive/2008/04/25/using-linq-expressions-to-generate-dynamic-methods.aspx" target="_blank">dynamic methods</a> and run a translation/builder strategy.</em>  <p>I know there are concerns about writing and maintaining assembler and DTO, but to me this is the least of the worry, a more challenging scenario would be the UnitOfWork (How to you intended to track remote state, concurrency etc) How about versioning? performance? scala.. well ilities in general?  <p>These kinds of problems lead one from Request/Response, Patterns of Enterprise Architecture &amp; DDD to a Pub/Sub Message Bus - Enterprise Integration Patterns &amp; DDDD.  <p>And to think all you want to do is send some frickin entities to a remote client&nbsp; :)</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Test Data Builders Refined</title>
		<link>http://elegantcode.com/2008/04/26/test-data-builders-refined/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=test-data-builders-refined</link>
		<comments>http://elegantcode.com/2008/04/26/test-data-builders-refined/#comments</comments>
		<pubDate>Sat, 26 Apr 2008 23:08:27 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/26/test-data-builders-refined/</guid>
		<description><![CDATA[Last year, I blogged about Test Data Builders here and here. I still use them heavily in my unit tests for creating objects with test data. Heck, I also use this pattern for fluent interfaces in production code. Here is a simple example of this approach: &#160; 1: public class CustomerBuilder 2: { 3: public [...]]]></description>
			<content:encoded><![CDATA[<p>Last year, I blogged about Test Data Builders <a href="http://vanryswyckjan.blogspot.com/2007/09/test-data-builder-versus-object-mother.html" target="_blank">here</a> and <a href="http://vanryswyckjan.blogspot.com/2007/12/test-data-builder.html" target="_blank">here</a>. I still use them heavily in my unit tests for creating objects with test data. Heck, I also use this pattern for fluent interfaces in production code. Here is a simple example of this approach:</p>  <p>&#160;</p>  <div>   <div class="csharpcode">     <pre class="alteven"><span class="lnum">   1:</span> <span class="kwrd">public</span> <span class="kwrd">class</span> CustomerBuilder</pre>

    <pre class="alteven"><span class="lnum">   2:</span> {</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     <span class="kwrd">public</span> String _firstName = <span class="str">&quot;Homer&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     <span class="kwrd">public</span> String _lastName = <span class="str">&quot;Simpson&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   5:</span>         </pre>

    <pre class="alteven"><span class="lnum">   6:</span>     <span class="kwrd">public</span> CustomerBuilder WithFirstName(String firstName)</pre>

    <pre class="alteven"><span class="lnum">   7:</span>     {</pre>

    <pre class="alteven"><span class="lnum">   8:</span>         _firstName = firstName;</pre>

    <pre class="alteven"><span class="lnum">   9:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;    </pre>

    <pre class="alteven"><span class="lnum">  10:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  11:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  12:</span>     <span class="kwrd">public</span> CustomerBuilder WithLastName(String lastName)</pre>

    <pre class="alteven"><span class="lnum">  13:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  14:</span>         _lastName = lastName;</pre>

    <pre class="alteven"><span class="lnum">  15:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven"><span class="lnum">  16:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  17:</span>         </pre>

    <pre class="alteven"><span class="lnum">  18:</span>     <span class="kwrd">public</span> Customer Build()</pre>

    <pre class="alteven"><span class="lnum">  19:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  20:</span>         <span class="kwrd">return</span> <span class="kwrd">new</span> Customer(_firstName, _lastName);    </pre>

    <pre class="alteven"><span class="lnum">  21:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  22:</span> }</pre>
  </div>
</div>

<pre class="alteven">This fluent builder class can then be used this way:</pre>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> Customer customer = <span class="kwrd">new</span> CustomerBuilder()</pre>

    <pre class="alteven"><span class="lnum">   2:</span>     .WithFirstName(<span class="str">&quot;Homer&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     .WithLastName(<span class="str">&quot;Simpson&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     .Build();</pre>
  </div>
</div>

<p>A while ago, <a href="http://codebetter.com/blogs/gregyoung/default.aspx" target="_blank">Greg Young</a> started a series of blog posts on DDDD (Distributed Domain-Driven Design), which I can highly recommend. Make sure to catch up now you still can because I think that he has a lot of stuff coming up, which I'm really looking forward to.</p>

<p>Anyhow, Greg had a couple of posts on fluent builders, which you can read <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/15/dddd-5-messages-have-fluent-builders.aspx" target="_blank">here</a>, <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/16/dddd-6-fluent-builders-alternate-ending.aspx" target="_blank">here</a> and <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/22/dddd-8-fluent-builders-and-tests.aspx" target="_blank">here</a>. I noticed an interesting approach in the way that the target object is built. Here is an example of this approach:</p>

<p>&#160;</p>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> <span class="kwrd">public</span> <span class="kwrd">class</span> CustomerBuilder</pre>

    <pre class="alteven"><span class="lnum">   2:</span> {</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     <span class="kwrd">public</span> String _firstName = <span class="str">&quot;Homer&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     <span class="kwrd">public</span> String _lastName = <span class="str">&quot;Simpson&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   5:</span>         </pre>

    <pre class="alteven"><span class="lnum">   6:</span>     <span class="kwrd">public</span> CustomerBuilder WithFirstName(String firstName)</pre>

    <pre class="alteven"><span class="lnum">   7:</span>     {</pre>

    <pre class="alteven"><span class="lnum">   8:</span>         _firstName = firstName;</pre>

    <pre class="alteven"><span class="lnum">   9:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;    </pre>

    <pre class="alteven"><span class="lnum">  10:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  11:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  12:</span>     <span class="kwrd">public</span> CustomerBuilder WithLastName(String lastName)</pre>

    <pre class="alteven"><span class="lnum">  13:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  14:</span>         _lastName = lastName;</pre>

    <pre class="alteven"><span class="lnum">  15:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven"><span class="lnum">  16:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  17:</span>         </pre>

    <pre class="alteven"><span class="lnum">  18:</span>     <span class="kwrd">public</span> Customer Build()</pre>

    <pre class="alteven"><span class="lnum">  19:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  20:</span>         <span class="kwrd">return</span> <span class="kwrd">new</span> Customer(_firstName, _lastName);    </pre>

    <pre class="alteven"><span class="lnum">  21:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  22:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  23:</span>     <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">implicit</span> <span class="kwrd">operator</span> Customer(</pre>

    <pre class="alteven"><span class="lnum">  24:</span>         CustomerBuilder builder) </pre>

    <pre class="alteven"><span class="lnum">  25:</span>     {  </pre>

    <pre class="alteven"><span class="lnum">  26:</span>         <span class="kwrd">return</span> builder.Build()    </pre>

    <pre class="alteven"><span class="lnum">  27:</span>     } </pre>

    <pre class="alteven"><span class="lnum">  28:</span> }</pre>
  </div>
</div>

<p>which results in the following usage:</p>

<p>&#160;</p>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> Customer customer = <span class="kwrd">new</span> CustomerBuilder()</pre>

    <pre class="alteven"><span class="lnum">   2:</span>     .WithFirstName(<span class="str">&quot;Homer&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     .WithLastName(<span class="str">&quot;Simpson&quot;</span>);</pre>
  </div>

  <br />Adding an implicit cast operator to the builder class makes that its no longer required to explicitly call the <em>Build</em> method. I keep the <em>Build</em> method around for backwards-compatibility reasons or in case I ever need it again (violating YAGNI in the process, I know, I know). I find that adding the implicit cast operator adds to the readability of the fluent interface, don't you agree?</div>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/26/test-data-builders-refined/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Name Those Patterns</title>
		<link>http://elegantcode.com/2008/04/24/name-those-patterns/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=name-those-patterns</link>
		<comments>http://elegantcode.com/2008/04/24/name-those-patterns/#comments</comments>
		<pubDate>Thu, 24 Apr 2008 16:16:13 +0000</pubDate>
		<dc:creator>Tony Rasa</dc:creator>
				<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/24/name-those-patterns/</guid>
		<description><![CDATA[In yesterday's discussion of Gang of Four Design Patterns, I said &#34;Never name your classes after the pattern itself.&#34;&#160; But then someone pointed out &#34;What about Factory classes?&#34;&#160; &#34;Oh, ok, ALWAYS name classes after the pattern.&#34;&#160; Except for Singleton or Fa&#231;ade, and maybe a few others.&#160; Ok then, NEVER name after the pattern, except when [...]]]></description>
			<content:encoded><![CDATA[<p>In yesterday's discussion of Gang of Four Design Patterns, I said &quot;Never name your classes after the pattern itself.&quot;&#160; But then someone pointed out &quot;What about Factory classes?&quot;&#160; &quot;Oh, ok, ALWAYS name classes after the pattern.&quot;&#160; Except for Singleton or Fa&#231;ade, and maybe a few others.&#160; Ok then, NEVER name after the pattern, except when you do, sometimes, and then ALWAYS name those, unless otherwise noted.</p>  <p>So, here's a list of the 23 Gang of Four patterns, along with when I'd use the name of the pattern in the implementation.&#160; Unless I didn't want to, and then I wouldn't.&#160; Feel free to agree with this list, unless you don't care to.</p>  <h3>Creational Patterns</h3>  <p><strong>Abstract Factory</strong> - After consideration, I have to say that I call my factories Factory, unless they're Repositories, Data Access Layers, DAOs, or something else.&#160; But most of the time, Factory.&#160; Because this would just be wrong:</p>  <pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> FooCreatorMakerThingy { ... }</pre>

<p><strong>Builder</strong> - Hmm, another noun. Guess I'd use this one too, for the same reason as Abstract Factory. Most of the time.&#160; </p>

<p><strong>Factory Method</strong> - I probably would not call my Factory Methods &quot;GetFooFactoryMethod()&quot; because that just looks weird.&#160;&#160; Maybe if we verbify the noun:</p>

<pre class="csharpcode"><span class="kwrd">public</span> Foo FactorialMethodizeFoo() {...}</pre>

<p>Nope, that's still dumb.</p>

<p><strong>Prototype</strong> - &quot;Specify the kinds of objects to create using a prototypical instance...&quot; So we don't care that the class is a &quot;Prototype,&quot; but we do care about the &quot;kind of object&quot; part.&#160; So don't name your prototypical classes &quot;FooPrototype.&quot;&#160; (Unless you just want to.)</p>

<p><strong>Singleton</strong> - Somebody have a case where it makes sense to name your Singleton &quot;Singleton&quot;?&#160; I can't come up with one.&#160; Anyway, you shouldn't be using singletons anymore anyway.&#160; :)</p>

<h3>Structural Patterns</h3>

<p><strong>Adapter</strong> - Adapters are pretty much called Adapters.&#160; Except when they're called Wrappers.&#160; So you can use this name too.</p>

<p><strong>Bridge</strong> - I wouldn't label a participant in a bridge pattern &quot;FooBridge,&quot; unless I was trying to impress on an interview test or something.&#160; And even then, that's a bad idea.&#160; &quot;Ohh, look, I know a complicated design pattern!&#160; Lookitme!&quot;&#160; So don't call your bridges Bridges.&#160; </p>

<p><strong>Composite</strong> - &quot;One of the goals of the Composite&#160; pattern is to make clients unaware of the specific Leaf or Composite classes they're using.&quot;&#160; So you don't want to point out that Composites are in play, that breaks the suspension of disbelief for your audience.&#160; Or something.&#160; Anyway Composites get used all over the place, if we had to label them all then it'd be FooComposite, BarComposite, TreeComposite, EverythingComposite, and what's the fun in that.</p>

<p><strong>Decorator </strong>- I think in general people don't label their Decorators Decorators.&#160; I'm sure there's a counter-example to this, but all of the Stream- and Xml-handling classes I can think of don't point out that they are Decorators.&#160; You'd probably want to stick to more descriptive names, like ASCIIStreamReader instead of cluttering things up with ASCIIStreamDecorator.&#160; We're not decorating the stream (in a literal sense), we're reading it.&#160; </p>

<p><strong>Fa&#231;ade</strong> - Don't call your Fa&#231;ade &quot;FooFa&#231;ade,&quot; that's just silly.&#160; Besides, its too much work to type that cedille into the editor if you're using an ignorant American keyboard.</p>

<p><strong>Flyweight </strong>- This is another pattern where we're going to disguise what's going on inside, in order to simplify the client's usage and make better use of resources.&#160; No reason to flaunt to the client that we're low on resources, because that's a personal and private matter - times are tough all over, and there's no shame in struggling a little bit, and hey who could have predicted that the sub-prime loan market was going to ... anyway, back to patterns.&#160;&#160; Don't call your Flyweights Flyweights, unless what you're modeling is actually a Flyweight, and then thats OK.</p>

<p><strong>Proxy </strong>- This one is standard terminology.&#160; So Proxy's ok.</p>

<h3>Behavioral Patterns (or &quot;<em>MAN this book has a lot of pages in it..</em>&quot;)</h3>

<p><strong>Chain of Responsibility </strong>- Don't even think of naming a class ChainOfResponsibility.&#160; Even with ReSharper, who'd want to have to type that?</p>

<p><strong>Command</strong> - Another convenient noun.&#160; Ok, you can create Commands.&#160; </p>

<p><strong>Interpreter </strong>- This is another pattern that doesn't have one identifying class that you could label &quot;The Interpreter.&quot;&#160; But you might create a Fa&#231;ade, and call that class Interpreter.&#160; I'm not sure if naming one pattern after another is OK or not.&#160; Makes my head hurt.&#160; </p>

<p>Iterator - Another convenient noun.&#160; Iterate away!&#160; Not that you'll have to worry too much about naming classes with this pattern if you use C#, Java, C++ or pretty much any other modern language, since this pattern is built-in.</p>

<p><strong>Mediator </strong>- This pattern promotes loose coupling, most of the time I'd call what it does a &quot;Controller,&quot; or what's being Mediated is more on a service level, so perhaps the word &quot;Service&quot; would end up in the name.&#160; Can't say I've ever called a class specifically &quot;FooMediator&quot;, but I suppose I don't have a problem with it if you want to do that.</p>

<p><strong>Memento </strong>- This pattern captures and externalizes an object's state.&#160; So I think I'd call something like it &quot;State&quot; before &quot;Memento,&quot; since it's fewer letters to type.&#160; Besides, I had to see that movie twice before I really appreciated it.&#160; </p>

<p>Ok, that was just lame, sorry.</p>

<p><strong>Observer </strong>- Oh, you mean like an Event?&#160; Or more like a delegate?&#160; :)</p>

<p><strong>State </strong>- I'd call my implementation of this pattern &quot;Memento,&quot; just to confuse you.&#160; That's just how I roll.</p>

<p><strong>Strategy </strong>- This is another pattern that it seems like we use all the time, without having to give it a fancy Ivory Tower name.&#160; As someone pointed out (sorry I lost the reference to who said that) Microsoft calls all their Strategy implementations Providers now anyway.&#160; Rather than label something a &quot;Strategy&quot; I'd probably just say what the thing does: &quot;CryptographicProvider.&quot;</p>

<p><strong>Template Method</strong> - Yet another pattern where there's no obvious place to hang the name off of, so don't try to force it in there and impress your coworkers with how smart you are.&#160; We know, you're a Design Pattern God, no need to brag about it.</p>

<p><strong>Visitor </strong>- Hmm, not sure where I stand on this one.&#160; I have used this pattern where I've named a visitor &quot;SqlGeneratingVisitor.&quot;&#160; But maybe that was a mistake?&#160; So how about we just declare this one &quot;Free Parking,&quot; and you can do what you want.</p>

<p>&#160;</p>

<p>So, there you go.&#160; A bunch of design patterns, summarized into one neat page.&#160; Now you're ready to ace that interview!&#160; </p>

<p>&#160;</p>

<p><em>disclaimer: I make no guarantees that any or all of the information will not harm you in a technical interview, and if repeating this to an interview board gets your removed from the building by security, its not my fault.&#160; This information comes without warantees or conditions of any kind, and you probably shouldn't have bothered reading it..</em></p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/24/name-those-patterns/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.Net HierarchicalDataSource&lt;T&gt;</title>
		<link>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=aspnet-hierarchicaldatasourcet</link>
		<comments>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/#comments</comments>
		<pubDate>Sun, 06 Apr 2008 06:45:44 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Extension Methods]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/</guid>
		<description><![CDATA[The Asp.net 2.0 tree control, as well as many 3rd party controls binds to hierarchical data via an IHierarchicalDataSource interface. Asp.net provides an implementation OOTB with the HierarchicalDataSourceControl! Easy, let’s hook that up to our… wait this control only takes data in xml format or from a sitemap? Hmm, we don’t want to convert our [...]]]></description>
			<content:encoded><![CDATA[<p>The Asp.net 2.0 tree control, as well as many 3rd party controls binds to hierarchical data via an IHierarchicalDataSource interface. Asp.net provides an implementation OOTB with the HierarchicalDataSourceControl! Easy, let’s hook that up to our… wait this control only takes data in xml format or from a sitemap? Hmm, we don’t want to convert our model to xml, and a sitemap seems inappropriate. We spend a lot time doing great patterns such as MVP &amp; MVC, we should just be able to bind up a list of model objects we already have.  <p>Good thing MS has exposed a set of interfaces and abstract classes in System.Web.UI for us to implement our own IHierarchicalDataSource and HierarchicalDataSourceView and IHierarchicalEnumerable and then IHierarchyData... geesh.  <p>Data? So let's see here, in order to bind our model to the tree control it needs to implement an interface in System.Web.UI? Well that's just not going to happen, so its time for some Elegant Code.  <p><strong>Story:</strong>  <p>Dude, as a software developer, needs to create a custom implementation of IHierarchicalDataSource, and the rest of its parts, so that he can bind a list of objects to a tree control in asp.net  <ul> <li>Use existing model to bind to tree control  <li>Must be able to reuse data source for future models  <li>Model must not contain a reference to System.Web.UI </li></ul> <p>In order to setup our model to address the acceptance criteria we are going to use an interface with a self-referencing generic declaration, where the type will specify itself as the concrete type. This will let us create a nice generic hierarchical model to pass into our datasource without resorting to any sort of base class.  <div><pre class="csharpcode"><span class="kwrd">namespace</span> POC.Common
{
    <span class="kwrd">public</span> <span class="kwrd">interface</span> IModelWithHierarchy&lt;T&gt;
    {
        <span class="kwrd">string</span> Name { get; set; }
        T Parent { get; set; }
        List&lt;T&gt; Children { get; set; }
    }
}</pre><em>Lets note that we have placed the interface in a Common namespace which represents a shared.dll between Web &amp; Model</em> </div>
<p>The UI part of criteria is now a bit more tricky because the HierarchicalDataSourceView expects a IHierarchicalEnumerable datasource which contains IHierarchyData. Said IHierarchyData != IModelWithHierarchy&lt;T&gt;.&nbsp; Luckily someone else has already figured this one out for us. We need an <a href="http://www.dofactory.com/Patterns/PatternAdapter.aspx" target="_blank">adapter</a>. 
<p><a href="http://elegantcode.com/wp-content/uploads/2008/04/image7.png" target="_blank"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="123" alt="image" src="http://elegantcode.com/wp-content/uploads/2008/04/image-thumb7.png" width="244" border="0"></a> 
<p>To instantiate our HierarchyData adapter objects we will use an extension method and linq 'Select' to convert our datasource IEnumerable&lt;T&gt; of IModelWithHierarchy&lt;T&gt; 's to an IHierarchicalEnumerable of IHierarchyData. 
<div><pre class="csharpcode"><span class="kwrd">namespace</span> POC.Web.HierarchyExtensions
{
    <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">class</span> HierarchyConverter
    {
        <span class="kwrd">public</span> <span class="kwrd">static</span> HierarchicalModelList
            ToHierarchicalModelList&lt;T&gt;(<span class="kwrd">this</span> IEnumerable&lt;T&gt; modelWithHierarchy)
            <span class="kwrd">where</span> T : IModelWithHierarchy&lt;T&gt;
        {
            <span class="kwrd">return</span> <span class="kwrd">new</span> HierarchicalModelList(
                modelWithHierarchy.Select(m =&gt; <span class="kwrd">new</span> HierarchyData&lt;T&gt;(m) <span class="kwrd">as</span> IHierarchyData));
        }
    }
}</pre></div>
<p>I am not going to bore you with the implementation details of the HierarachyData&lt;T&gt; adapter and the .net abstractions, but have dropped the <a href="http://elegantcode.com/wp-content/uploads/2008/04/hierarchydatasourcepoc.zip" target="_blank">source code here</a> if your interested. The end result: 
<div><pre class="csharpcode">TreeView1.DataSource = <span class="kwrd">new</span> HierarchicalModelDataSource&lt;Category&gt; { DataSource = Categories };
TreeView1.DataBind();
TreeView1.CollapseAll();</pre></div>
<p>Story complete!&nbsp; </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is Elegant Code to me?</title>
		<link>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-is-elegant-code-to-me</link>
		<comments>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/#comments</comments>
		<pubDate>Mon, 31 Mar 2008 03:47:45 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Patterns and Practices]]></category>
		<category><![CDATA[Widgets of Wisdom]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/</guid>
		<description><![CDATA[This question keeps popping up around here ("around here" being the loose conglomeration that makes up the Elegant Code group).&#160; It isn't easy to describe.&#160; And really, the notion of what constitutes elegance in code changes over time.&#160; There is no static "this is good code" test, and I doubt there ever will be.&#160; Plus, [...]]]></description>
			<content:encoded><![CDATA[<p>This question keeps popping up around here ("around here" being the loose conglomeration that makes up the Elegant Code group).&nbsp; It isn't easy to describe.&nbsp; And really, the notion of what constitutes elegance in code changes over time.&nbsp; There is no static "this is good code" test, and I doubt there ever will be.&nbsp; Plus, what makes good code in one language, may not apply to the next.</p> <p>So let me state for the record: today's elegant code is tomorrow's drivel.&nbsp; Don't feel bad, many writers have the same problems.&nbsp; What was super amazing back in the day is now rubbish or near unreadable.&nbsp; I am thinking of the Victorian writing that Hemingway usurped, and now Hemingway himself is almost unreadable (to me anyway).&nbsp; Tastes change with the times.&nbsp; That is a simple fact.</p> <p>So what can you do about this?&nbsp; As I say that old writing sucks to read (read Washington Irvine lately?), great works of literature still abound (for instance, Hemingway is still a great writer -- even if I prefer Tolkien) .&nbsp; So take some notes from them, and from other crafts in looking for the answer.&nbsp; If you want a cliff notes version of what this is going to tell you, it is this: you must always push yourself to get better.</p> <ol> <li>Find a good teacher.&nbsp; Nothing is better than sitting at the feet of a master who can nudge you along in the right direction.&nbsp; While early mistakes can often be corrected easily with a little bit of guidance, after they have had some time to fester all bets are off.&nbsp; I believe a fare number of "Anit-patterns" were created by self taught developers (find <a href="http://elegantcode.com/2008/03/21/sql-ejaculation/">SQL Ejaculation</a> on this site).&nbsp; <li>Read.&nbsp;&nbsp; Good writer are first good readers.&nbsp; Start with Code Complete, move into a good Patterns book, get MSDN Magazine, find some bloggers your like, but keep moving.&nbsp; You will NEVER be done reading.&nbsp; I imagine that even Martin Fowler has a "to read" booklist a mile long.  <li>Read code.&nbsp;&nbsp; There are a plethora of open source project just waiting to be read -- do so.&nbsp; This is the single best way to expand your coding repertoire, find things your language can do that you didn't even know about..&nbsp;&nbsp; Scott Hanselman has recently popularized this idea, and I thank him for it.  <li>Practice.&nbsp; This means writing small applications at home.&nbsp; You get an idea that you want to try out -- do it.&nbsp; I don't mean you have to write finished applications, just have some exploring time.&nbsp; I remember talking with an 80 year old master woodworker (he lives down the street from me), who was telling me how many time he would practice making dovetail joints before he felt competent.&nbsp; It was years worth.  <li>Pay Attention.&nbsp; Being good at anything really amounts to that.&nbsp; And don't just pay attention when reading, writing, or talking about code.&nbsp; Inspiration come from everywhere, any good artist will tell you that.&nbsp; Pay attention when you cook, when you work on the car, when you are wood working, playing an instrument, whatever it is that you do.&nbsp; This will help you in the end, event if it is just learning the amount of dedication it really takes to become good at something.  <li>Beware of preferences.&nbsp; Any time I hear someone start a statement with "I prefer code to ..." you know things are going downhill.&nbsp; If you find someone who cares more about how your code is formatted then how it is written you have found yourself in a mess.&nbsp; More importantly, beware of them in yourself.&nbsp;&nbsp; Having code style standard is important, but it isn't worth loosing sleep over.&nbsp; This also pertains to inheritance, patterns, use of particular classes (e.g. always using the generic list class in C# when a Dictionary would be better).&nbsp; <li>No Sacred Cows. Believe no single source of information.&nbsp; This means not thinking that Microsoft, Sun, IBM, Richard Stallman, or anyone else will have all of the correct answers.&nbsp; Apply the scientific method and do some research, experiment, and question the authority.&nbsp;&nbsp; There should be no sacred cows in programming.  <li>Talk with others.&nbsp; Join a user group, show up every now and then, heckle the presenter, and -maybe- speak.&nbsp; You want a broad range of people who you can talk to and bounce ideas off of.&nbsp; This will help you from becoming that crazy guy in the corner who vehemently states that all your code should be in one file.&nbsp; Having a mentor (mentioned earlier) is great, but having people around to push you from multiple directions is also good.  <li>Learn languages.&nbsp; This gets back to the "read code" idea. Make that multiple types of languages as well.&nbsp; If you know C# or Java and SQL, learn Python or Ruby, get really good at JavaScript.&nbsp; If you want something really out there, learn MDX.&nbsp; This is also a repertoire thing.&nbsp; Seeing how other languages work will make you rethink your ideas about what your current code, in whatever languages you are working in, should look like.  <li>Keep Thinking!!!&nbsp; That is possibly the most important point.&nbsp; Keep thinking.&nbsp; Don't just evaluate something once and never return, go back and reevaluate. Did the technique work?&nbsp; How could it have been better?&nbsp;&nbsp; The idea is continual improvement.  <li>Switch jobs every now and then.&nbsp; When I announced I was leaving my first programming job out of college, the VP of R&amp;D had me sit down with him and talk.&nbsp; He wasn't trying to keep me (he knew I was moving to be closer to family), he wanted to give me some advice.&nbsp; He told me to change jobs every three years.&nbsp; After you have been with a place for three years you have probably learned everything you are going to learn and it time to move on.&nbsp; This doesn't mean changing companies either.&nbsp; I've been with the same company now for four years, but I've had three different jobs.&nbsp; If you have been writing commercial applications, become a consultant - or vise-versa, become a test engineer, expand your boundaries.&nbsp; Again, this is about pushing yourself to be better.  <li>Have a Hobby. But don't ask about me, I have too many.&nbsp;&nbsp; Creating software is about creating things.&nbsp; So I suggest picking a hobby that involves creating something.&nbsp; Popular hobbies amongst programmers I know include: cooking, music, woodworking, beer making, and BBQ (which is different than cooking).&nbsp; But don't discount sports (golf is always popular), computer games, and board games.&nbsp; Anything that promotes the development of skill levels can't be a bad thing. </li></ol> <p>And I'm stopping there.&nbsp; This is my beginner's guide to how to become the writer elegant code.&nbsp; If you have others you think I missed, add them too the comments.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>LINQ Framework Design Guidelines</title>
		<link>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=linq-framework-design-guidelines-2</link>
		<comments>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/#comments</comments>
		<pubDate>Wed, 19 Mar 2008 01:53:22 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[Microsoft]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/</guid>
		<description><![CDATA[Excellent article on design considerations for LINQ. Looks like these concepts will be incorporated into the 2nd Edition of Framework Design Guidelines: Conventions, Idioms, and Patterns for Reuseable .NET Libraries. Cant wait to get my hands on a copy of that!]]></description>
			<content:encoded><![CDATA[<p>Excellent article on <a href="http://blogs.msdn.com/mirceat/archive/2008/03/13/linq-framework-design-guidelines.aspx" target="_blank">design considerations for LINQ</a>. Looks like these concepts will be incorporated into the 2nd Edition of <a href="http://www.amazon.com/Framework-Design-Guidelines-Conventions-Development/dp/0321545613/ref=pd_bbs_sr_2?ie=UTF8&amp;s=books&amp;qid=1200508590&amp;sr=1-2" target="_blank">Framework Design Guidelines: Conventions, Idioms, and Patterns for Reuseable .NET Libraries</a>. Cant wait to get my hands on a copy of that!</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Boise Code Camp 2008</title>
		<link>http://elegantcode.com/2008/03/09/boise-code-camp-2008/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=boise-code-camp-2008</link>
		<comments>http://elegantcode.com/2008/03/09/boise-code-camp-2008/#comments</comments>
		<pubDate>Mon, 10 Mar 2008 02:25:35 +0000</pubDate>
		<dc:creator>Alex Mueller</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Code Camp]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Patterns and Practices]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/09/boise-code-camp-2008/</guid>
		<description><![CDATA[Boise Code Camp 2008 was awesome. Thank you to David Starr and his wife, Eleanor, for taking ownership of this event and dedicating many months of their time to make this a reality. Thank you to the presenters for having the passion and desire to present technologies and practices, both new and not-as-new. Thank you [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Boise Code Camp 2008" href="http://www.boisecodecamp.org/" target="_blank">Boise Code Camp 2008</a> was awesome. Thank you to <a title="David Starr" href="http://elegantcode.com/about/david-starr/" target="_blank">David Starr</a> and his wife, <a title="Domestic Oblivion" href="http://domesticoblivion.com/" target="_blank">Eleanor</a>, for taking ownership of this event and dedicating many months of their time to make this a reality. Thank you to the presenters for having the passion and desire to present technologies and practices, both new and not-as-new. Thank you to the many volunteers who put in countless hours to help coordinate the weekend's events. Thank you to the campers for attending and making this year's code camp a success.</p>  <p>I am sure everyone will be blogging about all the great sessions they attended at code camp. I enjoyed all of the presentations I attended, and I am excited to explore some new material. </p>  <p>I want to share a few thoughts on my presentation dealing with Model-View-Presenter in ASP.NET after listening to <a title="Scott Hanselman" href="http://www.hanselman.com" target="_blank">Scott Hanselman's</a> session on the <a title="ASP. NET MVC Project" href="http://www.asp.net/mvc" target="_blank">ASP.NET MVC Project</a>. I may have the percentage incorrect, but Scott mentioned something like it is predicted that only 10% of the ASP.NET community currently using Web Forms will adopt and use the MVC framework. The MVC framework is an addition to ASP.NET, it is not a replacement. What this means is that Model-View-Presenter will still be a viable pattern to implement with your ASP.NET applications, and it is not going away. ASP.NET Web Forms will not be going away either.</p>  <p>Use MVP to get your third party controls under test. Use MVP to provide that separation of concerns in your legacy applications. Use it entirely or in conjunction with the MVC framework. It is all about testability. <a title="Glenn Block" href="http://blogs.msdn.com/gblock/" target="_blank">Glenn Block</a> presented on the Web Client Software Factory, and what pattern does this implement? Model-View-Presenter.</p>  <p>I am providing my <a title="Presentation and Source Code" href="http://muellerdesigns.net/DasBlog/content/binary/ModeViewPresenter.AlexMueller.BoiseCodeCamp2008.rar" target="_blank">presentation and source code (3.67 MB)</a> from my talk on MVP. It will be available via the <a title="Boise Code Camp" href="http://www.boisecodecamp.org/" target="_blank">Boise Code Camp</a> site as well. </p>  <p>Again, thank the many individuals and their families who sacrificed their time to bring to the local community this years code camp.</p>  <p>Around the office's water cooler on Monday, I will be able to say, &quot;and this one time, at code camp...&quot;</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/09/boise-code-camp-2008/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Microsoft releases Unity, an IoC library</title>
		<link>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=linq-framework-design-guidelines-2</link>
		<comments>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/#comments</comments>
		<pubDate>Wed, 19 Mar 2008 01:53:22 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[Microsoft]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/</guid>
		<description><![CDATA[Excellent article on design considerations for LINQ. Looks like these concepts will be incorporated into the 2nd Edition of Framework Design Guidelines: Conventions, Idioms, and Patterns for Reuseable .NET Libraries. Cant wait to get my hands on a copy of that!]]></description>
			<content:encoded><![CDATA[<p>Excellent article on <a href="http://blogs.msdn.com/mirceat/archive/2008/03/13/linq-framework-design-guidelines.aspx" target="_blank">design considerations for LINQ</a>. Looks like these concepts will be incorporated into the 2nd Edition of <a href="http://www.amazon.com/Framework-Design-Guidelines-Conventions-Development/dp/0321545613/ref=pd_bbs_sr_2?ie=UTF8&amp;s=books&amp;qid=1200508590&amp;sr=1-2" target="_blank">Framework Design Guidelines: Conventions, Idioms, and Patterns for Reuseable .NET Libraries</a>. Cant wait to get my hands on a copy of that!</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; Design Patterns</title>
	<atom:link href="http://elegantcode.com/tag/design-patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Where to start a new program</title>
		<link>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=where-to-start-a-new-program</link>
		<comments>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/#comments</comments>
		<pubDate>Sun, 08 Jun 2008 19:50:39 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/06/08/where-to-start-a-new-program/</guid>
		<description><![CDATA[Here as an interesting question that popped up at my user group meeting last night, when you are starting a new program (green field development), where do you get started?&#160; I find these questions interesting because there really is no correct answer, but a persons answer will tell you something about how they problem solve. [...]]]></description>
			<content:encoded><![CDATA[<p>Here as an interesting question that popped up at my user group meeting last night, when you are starting a new program (green field development), where do you get started?&nbsp; I find these questions interesting because there really is no correct answer, but a persons answer will tell you something about how they problem solve.</p> <p>Depending on who you ask you will likely get one of two answers.&nbsp; If you are talking with a data guy, he will say to start with the database.&nbsp; If you are dealing with a visual guy, he will say to start with the UI.&nbsp; Historically, I did a conglomeration of those two ideas, creating both the UI and the database at the same time, letting the two drive each other.&nbsp; But I'm weird, I'm a visual/data guy.</p> <p>Lately though, I've started to thing about a third approach.&nbsp; Starting in the middle.&nbsp; No UI, no database, just the data classes.&nbsp; This approach has been spurred on buy a couple of things.&nbsp; Namely, test driven development (TDD), design patterns, and domain driven design.</p> <p>TDD really is the glue that allows all of this to happen.&nbsp; I can start in the middle because I can run the code in the middle any time I want.&nbsp; I just have to write a test.&nbsp; No having to wade through multiple layers of UI to test out some small chunk ok UI is a great productivity gain for me.</p> <p>Next is design patterns.&nbsp; This is what gives you a reference for how to approach your code.&nbsp; That said, this isn't about making everything absolutely perfect from the get go, but it is nice to know where you are going to made your comprises early on.</p> <p>But all of that does not explain where I begin.&nbsp; That is where Domain Driven Design (DDD) kicks in.&nbsp; For those not familiar with the concept, you can read up on <a href="http://en.wikipedia.org/wiki/Domain-driven_design">wikipedia</a>, buy <a href="http://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1212806974&amp;sr=8-1">The Book</a>, or buy one of the <a href="http://www.amazon.com/s?ie=UTF8&amp;tag=mozilla-20&amp;index=blended&amp;link%5Fcode=qs&amp;field-keywords=domain%20driven%20design&amp;sourceid=Mozilla-search">various other references</a>.&nbsp;&nbsp; One of the concepts in DDD (I'm still reading the book) is setting up something called a <a href="http://www.domaindrivendesign.org/discussion/messageboardarchive/UbiquitousLanguage.html">ubiquitous language</a>.</p> <p>The ubiquitous language is actually trying to solve a problem that I have seen happen on many projects I've been apart of (but not always my fault).&nbsp; At some point there is a linguistic difference between the nouns that the customer uses and the nouns that the programmers use.&nbsp; By focusing on the language first, you are attempting to head that off at the pass.</p> <p>These nouns consist of your domain classes and your tables and columns, they move everywhere...but not always consistently.&nbsp; Consistency does matter.&nbsp; Inconsistency is what leads to rather confusing conversations with people and everyone scratching their heads about "what just happened".&nbsp; So, this is the middle that I start with.</p> <p>But before you start...</p> <p>One of the other guys in the group was an actual data guy.&nbsp; The systems he has written worked with more data than yours does.&nbsp; Trust me on that.&nbsp; Historically, his biggest consideration was data input speed.&nbsp; I don't think I would start in the middle if that was my biggest concern.&nbsp; In that case you do start with the database, you tune for speed.&nbsp; You also don't use as many domain classes, and you tend to throw away your ORM solution and just hit the database layer directly via Ado.Net.</p> <p>But lets face it, that doesn't happen very often.&nbsp; Most business applications aren't going to touch what the architecture can take.&nbsp; It is amazing how many horribly written applications with very inefficient code run just fine.&nbsp; Most of my speed issues could be solved by indexing tables and changing generic lists to generic dictionaries (more on that later).&nbsp; Architect your applications for how they are going to be used.&nbsp; Most of my web applications have a max usage of 100 simultaneous users.&nbsp; If the data access is slightly slower I'll take that hit for programmer efficiency.&nbsp; If you are designing Amazon.com, you code for through-put.</p> <p>OK, someone is going to disagree with this...I can already feel it.&nbsp; Please take a moment to register your displeasure in the comments.&nbsp; I would love to hear it.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Learning Design Patterns</title>
		<link>http://elegantcode.com/2008/04/28/learning-design-patterns/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=learning-design-patterns</link>
		<comments>http://elegantcode.com/2008/04/28/learning-design-patterns/#comments</comments>
		<pubDate>Mon, 28 Apr 2008 19:01:17 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/28/learning-design-patterns/</guid>
		<description><![CDATA[To me this seems like an interesting way to learn about design patterns (watch the video). I wonder if it also works when I hit an ivory-tower architect with POEAA or DDD? I'll keep you posted. PS: I'm really looking forward to the next episode on code reviews and retrospectives.]]></description>
			<content:encoded><![CDATA[To me <a href="http://codebetter.com/blogs/glenn.block/archive/2008/04/28/extreme-patterns-with-oren.aspx" target="_blank">this</a> seems like an interesting way to learn about design patterns (watch the video). I wonder if it also works when I hit an ivory-tower architect with <a href="http://www.bookpool.com/sm/0321127420" target="_blank">POEAA</a> or <a href="http://www.bookpool.com/sm/0321125215" target="_blank">DDD</a>? I'll keep you posted.

PS: I'm really looking forward to the next episode on code reviews and retrospectives.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/28/learning-design-patterns/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>DTO&#8217;s or Serialized Domain Entities?</title>
		<link>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=dtos-or-serialized-domain-entities</link>
		<comments>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/#comments</comments>
		<pubDate>Sun, 27 Apr 2008 08:09:02 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/</guid>
		<description><![CDATA[I hear this question come up time and time again. It is something that I have been dealing with for a while now, and have some experience with doing both ways. I started writing this in response to a DDD thread, but it got rather large and so I turned it into a post. So [...]]]></description>
			<content:encoded><![CDATA[<p>I hear this question come up time and time again. It is something that I have been dealing with for a while now, and have some experience with doing both ways. I started writing this in response to a <a href="http://tech.groups.yahoo.com/group/domaindrivendesign/message/7286" target="_blank">DDD thread</a>, but it got rather large and so I turned it into a post. So here ya go!  <p>I think you have to write DTO's for any sort of long term Enterprise 'system of systems' solution. I fought this for some time, but since moving to an Assembler/DTO I much enjoy the freedom of being able to easily tweak my DTO "snapshot" of the aggregate root I am working with. Maintaining them is very little overhead.  <p>I tend to think of the DTO as a message, a value object. This is necessary because once an entity leaves the boundary it can no longer function within its original meaning. Its methods may be intended to only run within a given context, which is not the same once on another tier. (Things like lazy loading are a good example) Once the DTO/Message arrives to the caller, it very well may (and probably should) be translated/assembled into another model which can represent its new semantics.  <p>Some other advantages of DTO w/ Assembler or Builder:<br>-Can explicitly control the depth of the graph/tree you are serializing.<br>-Can flatten out the data where needed<br>-Can return significantly less data (maybe you only need 3 fields instead of 15)<br>-Not tightly coupled between remote boundaries (this could be a mess when working with a large team)<br>-Interoperable  <p>I wrote a post a while back showing a way to '<a href="http://elegantcode.com/2007/12/05/fun-with-generics-from-repository-to-dto/" target="_blank">templatize</a>' the process. My current implementations are much different with LINQ now, but maybe it could stimulate some ideas for whomever.  <p><em>The next thing I would like to do is remove the assembler altogether. By flagging fields with attributes or using config to specify a mapping, I could then feed <a href="http://blogs.msdn.com/meek/archive/2008/04/25/using-linq-expressions-to-generate-dynamic-methods.aspx" target="_blank">dynamic methods</a> and run a translation/builder strategy.</em>  <p>I know there are concerns about writing and maintaining assembler and DTO, but to me this is the least of the worry, a more challenging scenario would be the UnitOfWork (How to you intended to track remote state, concurrency etc) How about versioning? performance? scala.. well ilities in general?  <p>These kinds of problems lead one from Request/Response, Patterns of Enterprise Architecture &amp; DDD to a Pub/Sub Message Bus - Enterprise Integration Patterns &amp; DDDD.  <p>And to think all you want to do is send some frickin entities to a remote client&nbsp; :)</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Test Data Builders Refined</title>
		<link>http://elegantcode.com/2008/04/26/test-data-builders-refined/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=test-data-builders-refined</link>
		<comments>http://elegantcode.com/2008/04/26/test-data-builders-refined/#comments</comments>
		<pubDate>Sat, 26 Apr 2008 23:08:27 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/26/test-data-builders-refined/</guid>
		<description><![CDATA[Last year, I blogged about Test Data Builders here and here. I still use them heavily in my unit tests for creating objects with test data. Heck, I also use this pattern for fluent interfaces in production code. Here is a simple example of this approach: &#160; 1: public class CustomerBuilder 2: { 3: public [...]]]></description>
			<content:encoded><![CDATA[<p>Last year, I blogged about Test Data Builders <a href="http://vanryswyckjan.blogspot.com/2007/09/test-data-builder-versus-object-mother.html" target="_blank">here</a> and <a href="http://vanryswyckjan.blogspot.com/2007/12/test-data-builder.html" target="_blank">here</a>. I still use them heavily in my unit tests for creating objects with test data. Heck, I also use this pattern for fluent interfaces in production code. Here is a simple example of this approach:</p>  <p>&#160;</p>  <div>   <div class="csharpcode">     <pre class="alteven"><span class="lnum">   1:</span> <span class="kwrd">public</span> <span class="kwrd">class</span> CustomerBuilder</pre>

    <pre class="alteven"><span class="lnum">   2:</span> {</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     <span class="kwrd">public</span> String _firstName = <span class="str">&quot;Homer&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     <span class="kwrd">public</span> String _lastName = <span class="str">&quot;Simpson&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   5:</span>         </pre>

    <pre class="alteven"><span class="lnum">   6:</span>     <span class="kwrd">public</span> CustomerBuilder WithFirstName(String firstName)</pre>

    <pre class="alteven"><span class="lnum">   7:</span>     {</pre>

    <pre class="alteven"><span class="lnum">   8:</span>         _firstName = firstName;</pre>

    <pre class="alteven"><span class="lnum">   9:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;    </pre>

    <pre class="alteven"><span class="lnum">  10:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  11:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  12:</span>     <span class="kwrd">public</span> CustomerBuilder WithLastName(String lastName)</pre>

    <pre class="alteven"><span class="lnum">  13:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  14:</span>         _lastName = lastName;</pre>

    <pre class="alteven"><span class="lnum">  15:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven"><span class="lnum">  16:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  17:</span>         </pre>

    <pre class="alteven"><span class="lnum">  18:</span>     <span class="kwrd">public</span> Customer Build()</pre>

    <pre class="alteven"><span class="lnum">  19:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  20:</span>         <span class="kwrd">return</span> <span class="kwrd">new</span> Customer(_firstName, _lastName);    </pre>

    <pre class="alteven"><span class="lnum">  21:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  22:</span> }</pre>
  </div>
</div>

<pre class="alteven">This fluent builder class can then be used this way:</pre>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> Customer customer = <span class="kwrd">new</span> CustomerBuilder()</pre>

    <pre class="alteven"><span class="lnum">   2:</span>     .WithFirstName(<span class="str">&quot;Homer&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     .WithLastName(<span class="str">&quot;Simpson&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     .Build();</pre>
  </div>
</div>

<p>A while ago, <a href="http://codebetter.com/blogs/gregyoung/default.aspx" target="_blank">Greg Young</a> started a series of blog posts on DDDD (Distributed Domain-Driven Design), which I can highly recommend. Make sure to catch up now you still can because I think that he has a lot of stuff coming up, which I'm really looking forward to.</p>

<p>Anyhow, Greg had a couple of posts on fluent builders, which you can read <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/15/dddd-5-messages-have-fluent-builders.aspx" target="_blank">here</a>, <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/16/dddd-6-fluent-builders-alternate-ending.aspx" target="_blank">here</a> and <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/22/dddd-8-fluent-builders-and-tests.aspx" target="_blank">here</a>. I noticed an interesting approach in the way that the target object is built. Here is an example of this approach:</p>

<p>&#160;</p>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> <span class="kwrd">public</span> <span class="kwrd">class</span> CustomerBuilder</pre>

    <pre class="alteven"><span class="lnum">   2:</span> {</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     <span class="kwrd">public</span> String _firstName = <span class="str">&quot;Homer&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     <span class="kwrd">public</span> String _lastName = <span class="str">&quot;Simpson&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   5:</span>         </pre>

    <pre class="alteven"><span class="lnum">   6:</span>     <span class="kwrd">public</span> CustomerBuilder WithFirstName(String firstName)</pre>

    <pre class="alteven"><span class="lnum">   7:</span>     {</pre>

    <pre class="alteven"><span class="lnum">   8:</span>         _firstName = firstName;</pre>

    <pre class="alteven"><span class="lnum">   9:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;    </pre>

    <pre class="alteven"><span class="lnum">  10:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  11:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  12:</span>     <span class="kwrd">public</span> CustomerBuilder WithLastName(String lastName)</pre>

    <pre class="alteven"><span class="lnum">  13:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  14:</span>         _lastName = lastName;</pre>

    <pre class="alteven"><span class="lnum">  15:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven"><span class="lnum">  16:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  17:</span>         </pre>

    <pre class="alteven"><span class="lnum">  18:</span>     <span class="kwrd">public</span> Customer Build()</pre>

    <pre class="alteven"><span class="lnum">  19:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  20:</span>         <span class="kwrd">return</span> <span class="kwrd">new</span> Customer(_firstName, _lastName);    </pre>

    <pre class="alteven"><span class="lnum">  21:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  22:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  23:</span>     <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">implicit</span> <span class="kwrd">operator</span> Customer(</pre>

    <pre class="alteven"><span class="lnum">  24:</span>         CustomerBuilder builder) </pre>

    <pre class="alteven"><span class="lnum">  25:</span>     {  </pre>

    <pre class="alteven"><span class="lnum">  26:</span>         <span class="kwrd">return</span> builder.Build()    </pre>

    <pre class="alteven"><span class="lnum">  27:</span>     } </pre>

    <pre class="alteven"><span class="lnum">  28:</span> }</pre>
  </div>
</div>

<p>which results in the following usage:</p>

<p>&#160;</p>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> Customer customer = <span class="kwrd">new</span> CustomerBuilder()</pre>

    <pre class="alteven"><span class="lnum">   2:</span>     .WithFirstName(<span class="str">&quot;Homer&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     .WithLastName(<span class="str">&quot;Simpson&quot;</span>);</pre>
  </div>

  <br />Adding an implicit cast operator to the builder class makes that its no longer required to explicitly call the <em>Build</em> method. I keep the <em>Build</em> method around for backwards-compatibility reasons or in case I ever need it again (violating YAGNI in the process, I know, I know). I find that adding the implicit cast operator adds to the readability of the fluent interface, don't you agree?</div>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/26/test-data-builders-refined/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Name Those Patterns</title>
		<link>http://elegantcode.com/2008/04/24/name-those-patterns/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=name-those-patterns</link>
		<comments>http://elegantcode.com/2008/04/24/name-those-patterns/#comments</comments>
		<pubDate>Thu, 24 Apr 2008 16:16:13 +0000</pubDate>
		<dc:creator>Tony Rasa</dc:creator>
				<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/24/name-those-patterns/</guid>
		<description><![CDATA[In yesterday's discussion of Gang of Four Design Patterns, I said &#34;Never name your classes after the pattern itself.&#34;&#160; But then someone pointed out &#34;What about Factory classes?&#34;&#160; &#34;Oh, ok, ALWAYS name classes after the pattern.&#34;&#160; Except for Singleton or Fa&#231;ade, and maybe a few others.&#160; Ok then, NEVER name after the pattern, except when [...]]]></description>
			<content:encoded><![CDATA[<p>In yesterday's discussion of Gang of Four Design Patterns, I said &quot;Never name your classes after the pattern itself.&quot;&#160; But then someone pointed out &quot;What about Factory classes?&quot;&#160; &quot;Oh, ok, ALWAYS name classes after the pattern.&quot;&#160; Except for Singleton or Fa&#231;ade, and maybe a few others.&#160; Ok then, NEVER name after the pattern, except when you do, sometimes, and then ALWAYS name those, unless otherwise noted.</p>  <p>So, here's a list of the 23 Gang of Four patterns, along with when I'd use the name of the pattern in the implementation.&#160; Unless I didn't want to, and then I wouldn't.&#160; Feel free to agree with this list, unless you don't care to.</p>  <h3>Creational Patterns</h3>  <p><strong>Abstract Factory</strong> - After consideration, I have to say that I call my factories Factory, unless they're Repositories, Data Access Layers, DAOs, or something else.&#160; But most of the time, Factory.&#160; Because this would just be wrong:</p>  <pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> FooCreatorMakerThingy { ... }</pre>

<p><strong>Builder</strong> - Hmm, another noun. Guess I'd use this one too, for the same reason as Abstract Factory. Most of the time.&#160; </p>

<p><strong>Factory Method</strong> - I probably would not call my Factory Methods &quot;GetFooFactoryMethod()&quot; because that just looks weird.&#160;&#160; Maybe if we verbify the noun:</p>

<pre class="csharpcode"><span class="kwrd">public</span> Foo FactorialMethodizeFoo() {...}</pre>

<p>Nope, that's still dumb.</p>

<p><strong>Prototype</strong> - &quot;Specify the kinds of objects to create using a prototypical instance...&quot; So we don't care that the class is a &quot;Prototype,&quot; but we do care about the &quot;kind of object&quot; part.&#160; So don't name your prototypical classes &quot;FooPrototype.&quot;&#160; (Unless you just want to.)</p>

<p><strong>Singleton</strong> - Somebody have a case where it makes sense to name your Singleton &quot;Singleton&quot;?&#160; I can't come up with one.&#160; Anyway, you shouldn't be using singletons anymore anyway.&#160; :)</p>

<h3>Structural Patterns</h3>

<p><strong>Adapter</strong> - Adapters are pretty much called Adapters.&#160; Except when they're called Wrappers.&#160; So you can use this name too.</p>

<p><strong>Bridge</strong> - I wouldn't label a participant in a bridge pattern &quot;FooBridge,&quot; unless I was trying to impress on an interview test or something.&#160; And even then, that's a bad idea.&#160; &quot;Ohh, look, I know a complicated design pattern!&#160; Lookitme!&quot;&#160; So don't call your bridges Bridges.&#160; </p>

<p><strong>Composite</strong> - &quot;One of the goals of the Composite&#160; pattern is to make clients unaware of the specific Leaf or Composite classes they're using.&quot;&#160; So you don't want to point out that Composites are in play, that breaks the suspension of disbelief for your audience.&#160; Or something.&#160; Anyway Composites get used all over the place, if we had to label them all then it'd be FooComposite, BarComposite, TreeComposite, EverythingComposite, and what's the fun in that.</p>

<p><strong>Decorator </strong>- I think in general people don't label their Decorators Decorators.&#160; I'm sure there's a counter-example to this, but all of the Stream- and Xml-handling classes I can think of don't point out that they are Decorators.&#160; You'd probably want to stick to more descriptive names, like ASCIIStreamReader instead of cluttering things up with ASCIIStreamDecorator.&#160; We're not decorating the stream (in a literal sense), we're reading it.&#160; </p>

<p><strong>Fa&#231;ade</strong> - Don't call your Fa&#231;ade &quot;FooFa&#231;ade,&quot; that's just silly.&#160; Besides, its too much work to type that cedille into the editor if you're using an ignorant American keyboard.</p>

<p><strong>Flyweight </strong>- This is another pattern where we're going to disguise what's going on inside, in order to simplify the client's usage and make better use of resources.&#160; No reason to flaunt to the client that we're low on resources, because that's a personal and private matter - times are tough all over, and there's no shame in struggling a little bit, and hey who could have predicted that the sub-prime loan market was going to ... anyway, back to patterns.&#160;&#160; Don't call your Flyweights Flyweights, unless what you're modeling is actually a Flyweight, and then thats OK.</p>

<p><strong>Proxy </strong>- This one is standard terminology.&#160; So Proxy's ok.</p>

<h3>Behavioral Patterns (or &quot;<em>MAN this book has a lot of pages in it..</em>&quot;)</h3>

<p><strong>Chain of Responsibility </strong>- Don't even think of naming a class ChainOfResponsibility.&#160; Even with ReSharper, who'd want to have to type that?</p>

<p><strong>Command</strong> - Another convenient noun.&#160; Ok, you can create Commands.&#160; </p>

<p><strong>Interpreter </strong>- This is another pattern that doesn't have one identifying class that you could label &quot;The Interpreter.&quot;&#160; But you might create a Fa&#231;ade, and call that class Interpreter.&#160; I'm not sure if naming one pattern after another is OK or not.&#160; Makes my head hurt.&#160; </p>

<p>Iterator - Another convenient noun.&#160; Iterate away!&#160; Not that you'll have to worry too much about naming classes with this pattern if you use C#, Java, C++ or pretty much any other modern language, since this pattern is built-in.</p>

<p><strong>Mediator </strong>- This pattern promotes loose coupling, most of the time I'd call what it does a &quot;Controller,&quot; or what's being Mediated is more on a service level, so perhaps the word &quot;Service&quot; would end up in the name.&#160; Can't say I've ever called a class specifically &quot;FooMediator&quot;, but I suppose I don't have a problem with it if you want to do that.</p>

<p><strong>Memento </strong>- This pattern captures and externalizes an object's state.&#160; So I think I'd call something like it &quot;State&quot; before &quot;Memento,&quot; since it's fewer letters to type.&#160; Besides, I had to see that movie twice before I really appreciated it.&#160; </p>

<p>Ok, that was just lame, sorry.</p>

<p><strong>Observer </strong>- Oh, you mean like an Event?&#160; Or more like a delegate?&#160; :)</p>

<p><strong>State </strong>- I'd call my implementation of this pattern &quot;Memento,&quot; just to confuse you.&#160; That's just how I roll.</p>

<p><strong>Strategy </strong>- This is another pattern that it seems like we use all the time, without having to give it a fancy Ivory Tower name.&#160; As someone pointed out (sorry I lost the reference to who said that) Microsoft calls all their Strategy implementations Providers now anyway.&#160; Rather than label something a &quot;Strategy&quot; I'd probably just say what the thing does: &quot;CryptographicProvider.&quot;</p>

<p><strong>Template Method</strong> - Yet another pattern where there's no obvious place to hang the name off of, so don't try to force it in there and impress your coworkers with how smart you are.&#160; We know, you're a Design Pattern God, no need to brag about it.</p>

<p><strong>Visitor </strong>- Hmm, not sure where I stand on this one.&#160; I have used this pattern where I've named a visitor &quot;SqlGeneratingVisitor.&quot;&#160; But maybe that was a mistake?&#160; So how about we just declare this one &quot;Free Parking,&quot; and you can do what you want.</p>

<p>&#160;</p>

<p>So, there you go.&#160; A bunch of design patterns, summarized into one neat page.&#160; Now you're ready to ace that interview!&#160; </p>

<p>&#160;</p>

<p><em>disclaimer: I make no guarantees that any or all of the information will not harm you in a technical interview, and if repeating this to an interview board gets your removed from the building by security, its not my fault.&#160; This information comes without warantees or conditions of any kind, and you probably shouldn't have bothered reading it..</em></p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/24/name-those-patterns/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.Net HierarchicalDataSource&lt;T&gt;</title>
		<link>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=aspnet-hierarchicaldatasourcet</link>
		<comments>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/#comments</comments>
		<pubDate>Sun, 06 Apr 2008 06:45:44 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Extension Methods]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/</guid>
		<description><![CDATA[The Asp.net 2.0 tree control, as well as many 3rd party controls binds to hierarchical data via an IHierarchicalDataSource interface. Asp.net provides an implementation OOTB with the HierarchicalDataSourceControl! Easy, let’s hook that up to our… wait this control only takes data in xml format or from a sitemap? Hmm, we don’t want to convert our [...]]]></description>
			<content:encoded><![CDATA[<p>The Asp.net 2.0 tree control, as well as many 3rd party controls binds to hierarchical data via an IHierarchicalDataSource interface. Asp.net provides an implementation OOTB with the HierarchicalDataSourceControl! Easy, let’s hook that up to our… wait this control only takes data in xml format or from a sitemap? Hmm, we don’t want to convert our model to xml, and a sitemap seems inappropriate. We spend a lot time doing great patterns such as MVP &amp; MVC, we should just be able to bind up a list of model objects we already have.  <p>Good thing MS has exposed a set of interfaces and abstract classes in System.Web.UI for us to implement our own IHierarchicalDataSource and HierarchicalDataSourceView and IHierarchicalEnumerable and then IHierarchyData... geesh.  <p>Data? So let's see here, in order to bind our model to the tree control it needs to implement an interface in System.Web.UI? Well that's just not going to happen, so its time for some Elegant Code.  <p><strong>Story:</strong>  <p>Dude, as a software developer, needs to create a custom implementation of IHierarchicalDataSource, and the rest of its parts, so that he can bind a list of objects to a tree control in asp.net  <ul> <li>Use existing model to bind to tree control  <li>Must be able to reuse data source for future models  <li>Model must not contain a reference to System.Web.UI </li></ul> <p>In order to setup our model to address the acceptance criteria we are going to use an interface with a self-referencing generic declaration, where the type will specify itself as the concrete type. This will let us create a nice generic hierarchical model to pass into our datasource without resorting to any sort of base class.  <div><pre class="csharpcode"><span class="kwrd">namespace</span> POC.Common
{
    <span class="kwrd">public</span> <span class="kwrd">interface</span> IModelWithHierarchy&lt;T&gt;
    {
        <span class="kwrd">string</span> Name { get; set; }
        T Parent { get; set; }
        List&lt;T&gt; Children { get; set; }
    }
}</pre><em>Lets note that we have placed the interface in a Common namespace which represents a shared.dll between Web &amp; Model</em> </div>
<p>The UI part of criteria is now a bit more tricky because the HierarchicalDataSourceView expects a IHierarchicalEnumerable datasource which contains IHierarchyData. Said IHierarchyData != IModelWithHierarchy&lt;T&gt;.&nbsp; Luckily someone else has already figured this one out for us. We need an <a href="http://www.dofactory.com/Patterns/PatternAdapter.aspx" target="_blank">adapter</a>. 
<p><a href="http://elegantcode.com/wp-content/uploads/2008/04/image7.png" target="_blank"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="123" alt="image" src="http://elegantcode.com/wp-content/uploads/2008/04/image-thumb7.png" width="244" border="0"></a> 
<p>To instantiate our HierarchyData adapter objects we will use an extension method and linq 'Select' to convert our datasource IEnumerable&lt;T&gt; of IModelWithHierarchy&lt;T&gt; 's to an IHierarchicalEnumerable of IHierarchyData. 
<div><pre class="csharpcode"><span class="kwrd">namespace</span> POC.Web.HierarchyExtensions
{
    <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">class</span> HierarchyConverter
    {
        <span class="kwrd">public</span> <span class="kwrd">static</span> HierarchicalModelList
            ToHierarchicalModelList&lt;T&gt;(<span class="kwrd">this</span> IEnumerable&lt;T&gt; modelWithHierarchy)
            <span class="kwrd">where</span> T : IModelWithHierarchy&lt;T&gt;
        {
            <span class="kwrd">return</span> <span class="kwrd">new</span> HierarchicalModelList(
                modelWithHierarchy.Select(m =&gt; <span class="kwrd">new</span> HierarchyData&lt;T&gt;(m) <span class="kwrd">as</span> IHierarchyData));
        }
    }
}</pre></div>
<p>I am not going to bore you with the implementation details of the HierarachyData&lt;T&gt; adapter and the .net abstractions, but have dropped the <a href="http://elegantcode.com/wp-content/uploads/2008/04/hierarchydatasourcepoc.zip" target="_blank">source code here</a> if your interested. The end result: 
<div><pre class="csharpcode">TreeView1.DataSource = <span class="kwrd">new</span> HierarchicalModelDataSource&lt;Category&gt; { DataSource = Categories };
TreeView1.DataBind();
TreeView1.CollapseAll();</pre></div>
<p>Story complete!&nbsp; </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is Elegant Code to me?</title>
		<link>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-is-elegant-code-to-me</link>
		<comments>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/#comments</comments>
		<pubDate>Mon, 31 Mar 2008 03:47:45 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Patterns and Practices]]></category>
		<category><![CDATA[Widgets of Wisdom]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/</guid>
		<description><![CDATA[This question keeps popping up around here ("around here" being the loose conglomeration that makes up the Elegant Code group).&#160; It isn't easy to describe.&#160; And really, the notion of what constitutes elegance in code changes over time.&#160; There is no static "this is good code" test, and I doubt there ever will be.&#160; Plus, [...]]]></description>
			<content:encoded><![CDATA[<p>This question keeps popping up around here ("around here" being the loose conglomeration that makes up the Elegant Code group).&nbsp; It isn't easy to describe.&nbsp; And really, the notion of what constitutes elegance in code changes over time.&nbsp; There is no static "this is good code" test, and I doubt there ever will be.&nbsp; Plus, what makes good code in one language, may not apply to the next.</p> <p>So let me state for the record: today's elegant code is tomorrow's drivel.&nbsp; Don't feel bad, many writers have the same problems.&nbsp; What was super amazing back in the day is now rubbish or near unreadable.&nbsp; I am thinking of the Victorian writing that Hemingway usurped, and now Hemingway himself is almost unreadable (to me anyway).&nbsp; Tastes change with the times.&nbsp; That is a simple fact.</p> <p>So what can you do about this?&nbsp; As I say that old writing sucks to read (read Washington Irvine lately?), great works of literature still abound (for instance, Hemingway is still a great writer -- even if I prefer Tolkien) .&nbsp; So take some notes from them, and from other crafts in looking for the answer.&nbsp; If you want a cliff notes version of what this is going to tell you, it is this: you must always push yourself to get better.</p> <ol> <li>Find a good teacher.&nbsp; Nothing is better than sitting at the feet of a master who can nudge you along in the right direction.&nbsp; While early mistakes can often be corrected easily with a little bit of guidance, after they have had some time to fester all bets are off.&nbsp; I believe a fare number of "Anit-patterns" were created by self taught developers (find <a href="http://elegantcode.com/2008/03/21/sql-ejaculation/">SQL Ejaculation</a> on this site).&nbsp; <li>Read.&nbsp;&nbsp; Good writer are first good readers.&nbsp; Start with Code Complete, move into a good Patterns book, get MSDN Magazine, find some bloggers your like, but keep moving.&nbsp; You will NEVER be done reading.&nbsp; I imagine that even Martin Fowler has a "to read" booklist a mile long.  <li>Read code.&nbsp;&nbsp; There are a plethora of open source project just waiting to be read -- do so.&nbsp; This is the single best way to expand your coding repertoire, find things your language can do that you didn't even know about..&nbsp;&nbsp; Scott Hanselman has recently popularized this idea, and I thank him for it.  <li>Practice.&nbsp; This means writing small applications at home.&nbsp; You get an idea that you want to try out -- do it.&nbsp; I don't mean you have to write finished applications, just have some exploring time.&nbsp; I remember talking with an 80 year old master woodworker (he lives down the street from me), who was telling me how many time he would practice making dovetail joints before he felt competent.&nbsp; It was years worth.  <li>Pay Attention.&nbsp; Being good at anything really amounts to that.&nbsp; And don't just pay attention when reading, writing, or talking about code.&nbsp; Inspiration come from everywhere, any good artist will tell you that.&nbsp; Pay attention when you cook, when you work on the car, when you are wood working, playing an instrument, whatever it is that you do.&nbsp; This will help you in the end, event if it is just learning the amount of dedication it really takes to become good at something.  <li>Beware of preferences.&nbsp; Any time I hear someone start a statement with "I prefer code to ..." you know things are going downhill.&nbsp; If you find someone who cares more about how your code is formatted then how it is written you have found yourself in a mess.&nbsp; More importantly, beware of them in yourself.&nbsp;&nbsp; Having code style standard is important, but it isn't worth loosing sleep over.&nbsp; This also pertains to inheritance, patterns, use of particular classes (e.g. always using the generic list class in C# when a Dictionary would be better).&nbsp; <li>No Sacred Cows. Believe no single source of information.&nbsp; This means not thinking that Microsoft, Sun, IBM, Richard Stallman, or anyone else will have all of the correct answers.&nbsp; Apply the scientific method and do some research, experiment, and question the authority.&nbsp;&nbsp; There should be no sacred cows in programming.  <li>Talk with others.&nbsp; Join a user group, show up every now and then, heckle the presenter, and -maybe- speak.&nbsp; You want a broad range of people who you can talk to and bounce ideas off of.&nbsp; This will help you from becoming that crazy guy in the corner who vehemently states that all your code should be in one file.&nbsp; Having a mentor (mentioned earlier) is great, but having people around to push you from multiple directions is also good.  <li>Learn languages.&nbsp; This gets back to the "read code" idea. Make that multiple types of languages as well.&nbsp; If you know C# or Java and SQL, learn Python or Ruby, get really good at JavaScript.&nbsp; If you want something really out there, learn MDX.&nbsp; This is also a repertoire thing.&nbsp; Seeing how other languages work will make you rethink your ideas about what your current code, in whatever languages you are working in, should look like.  <li>Keep Thinking!!!&nbsp; That is possibly the most important point.&nbsp; Keep thinking.&nbsp; Don't just evaluate something once and never return, go back and reevaluate. Did the technique work?&nbsp; How could it have been better?&nbsp;&nbsp; The idea is continual improvement.  <li>Switch jobs every now and then.&nbsp; When I announced I was leaving my first programming job out of college, the VP of R&amp;D had me sit down with him and talk.&nbsp; He wasn't trying to keep me (he knew I was moving to be closer to family), he wanted to give me some advice.&nbsp; He told me to change jobs every three years.&nbsp; After you have been with a place for three years you have probably learned everything you are going to learn and it time to move on.&nbsp; This doesn't mean changing companies either.&nbsp; I've been with the same company now for four years, but I've had three different jobs.&nbsp; If you have been writing commercial applications, become a consultant - or vise-versa, become a test engineer, expand your boundaries.&nbsp; Again, this is about pushing yourself to be better.  <li>Have a Hobby. But don't ask about me, I have too many.&nbsp;&nbsp; Creating software is about creating things.&nbsp; So I suggest picking a hobby that involves creating something.&nbsp; Popular hobbies amongst programmers I know include: cooking, music, woodworking, beer making, and BBQ (which is different than cooking).&nbsp; But don't discount sports (golf is always popular), computer games, and board games.&nbsp; Anything that promotes the development of skill levels can't be a bad thing. </li></ol> <p>And I'm stopping there.&nbsp; This is my beginner's guide to how to become the writer elegant code.&nbsp; If you have others you think I missed, add them too the comments.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>LINQ Framework Design Guidelines</title>
		<link>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=linq-framework-design-guidelines-2</link>
		<comments>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/#comments</comments>
		<pubDate>Wed, 19 Mar 2008 01:53:22 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[Microsoft]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/</guid>
		<description><![CDATA[Excellent article on design considerations for LINQ. Looks like these concepts will be incorporated into the 2nd Edition of Framework Design Guidelines: Conventions, Idioms, and Patterns for Reuseable .NET Libraries. Cant wait to get my hands on a copy of that!]]></description>
			<content:encoded><![CDATA[<p>Excellent article on <a href="http://blogs.msdn.com/mirceat/archive/2008/03/13/linq-framework-design-guidelines.aspx" target="_blank">design considerations for LINQ</a>. Looks like these concepts will be incorporated into the 2nd Edition of <a href="http://www.amazon.com/Framework-Design-Guidelines-Conventions-Development/dp/0321545613/ref=pd_bbs_sr_2?ie=UTF8&amp;s=books&amp;qid=1200508590&amp;sr=1-2" target="_blank">Framework Design Guidelines: Conventions, Idioms, and Patterns for Reuseable .NET Libraries</a>. Cant wait to get my hands on a copy of that!</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Boise Code Camp 2008</title>
		<link>http://elegantcode.com/2008/03/09/boise-code-camp-2008/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=boise-code-camp-2008</link>
		<comments>http://elegantcode.com/2008/03/09/boise-code-camp-2008/#comments</comments>
		<pubDate>Mon, 10 Mar 2008 02:25:35 +0000</pubDate>
		<dc:creator>Alex Mueller</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Code Camp]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Patterns and Practices]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/09/boise-code-camp-2008/</guid>
		<description><![CDATA[Boise Code Camp 2008 was awesome. Thank you to David Starr and his wife, Eleanor, for taking ownership of this event and dedicating many months of their time to make this a reality. Thank you to the presenters for having the passion and desire to present technologies and practices, both new and not-as-new. Thank you [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Boise Code Camp 2008" href="http://www.boisecodecamp.org/" target="_blank">Boise Code Camp 2008</a> was awesome. Thank you to <a title="David Starr" href="http://elegantcode.com/about/david-starr/" target="_blank">David Starr</a> and his wife, <a title="Domestic Oblivion" href="http://domesticoblivion.com/" target="_blank">Eleanor</a>, for taking ownership of this event and dedicating many months of their time to make this a reality. Thank you to the presenters for having the passion and desire to present technologies and practices, both new and not-as-new. Thank you to the many volunteers who put in countless hours to help coordinate the weekend's events. Thank you to the campers for attending and making this year's code camp a success.</p>  <p>I am sure everyone will be blogging about all the great sessions they attended at code camp. I enjoyed all of the presentations I attended, and I am excited to explore some new material. </p>  <p>I want to share a few thoughts on my presentation dealing with Model-View-Presenter in ASP.NET after listening to <a title="Scott Hanselman" href="http://www.hanselman.com" target="_blank">Scott Hanselman's</a> session on the <a title="ASP. NET MVC Project" href="http://www.asp.net/mvc" target="_blank">ASP.NET MVC Project</a>. I may have the percentage incorrect, but Scott mentioned something like it is predicted that only 10% of the ASP.NET community currently using Web Forms will adopt and use the MVC framework. The MVC framework is an addition to ASP.NET, it is not a replacement. What this means is that Model-View-Presenter will still be a viable pattern to implement with your ASP.NET applications, and it is not going away. ASP.NET Web Forms will not be going away either.</p>  <p>Use MVP to get your third party controls under test. Use MVP to provide that separation of concerns in your legacy applications. Use it entirely or in conjunction with the MVC framework. It is all about testability. <a title="Glenn Block" href="http://blogs.msdn.com/gblock/" target="_blank">Glenn Block</a> presented on the Web Client Software Factory, and what pattern does this implement? Model-View-Presenter.</p>  <p>I am providing my <a title="Presentation and Source Code" href="http://muellerdesigns.net/DasBlog/content/binary/ModeViewPresenter.AlexMueller.BoiseCodeCamp2008.rar" target="_blank">presentation and source code (3.67 MB)</a> from my talk on MVP. It will be available via the <a title="Boise Code Camp" href="http://www.boisecodecamp.org/" target="_blank">Boise Code Camp</a> site as well. </p>  <p>Again, thank the many individuals and their families who sacrificed their time to bring to the local community this years code camp.</p>  <p>Around the office's water cooler on Monday, I will be able to say, &quot;and this one time, at code camp...&quot;</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/09/boise-code-camp-2008/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Microsoft releases Unity, an IoC library</title>
		<link>http://elegantcode.com/2008/03/09/boise-code-camp-2008/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=boise-code-camp-2008</link>
		<comments>http://elegantcode.com/2008/03/09/boise-code-camp-2008/#comments</comments>
		<pubDate>Mon, 10 Mar 2008 02:25:35 +0000</pubDate>
		<dc:creator>Alex Mueller</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Code Camp]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Patterns and Practices]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/09/boise-code-camp-2008/</guid>
		<description><![CDATA[Boise Code Camp 2008 was awesome. Thank you to David Starr and his wife, Eleanor, for taking ownership of this event and dedicating many months of their time to make this a reality. Thank you to the presenters for having the passion and desire to present technologies and practices, both new and not-as-new. Thank you [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Boise Code Camp 2008" href="http://www.boisecodecamp.org/" target="_blank">Boise Code Camp 2008</a> was awesome. Thank you to <a title="David Starr" href="http://elegantcode.com/about/david-starr/" target="_blank">David Starr</a> and his wife, <a title="Domestic Oblivion" href="http://domesticoblivion.com/" target="_blank">Eleanor</a>, for taking ownership of this event and dedicating many months of their time to make this a reality. Thank you to the presenters for having the passion and desire to present technologies and practices, both new and not-as-new. Thank you to the many volunteers who put in countless hours to help coordinate the weekend's events. Thank you to the campers for attending and making this year's code camp a success.</p>  <p>I am sure everyone will be blogging about all the great sessions they attended at code camp. I enjoyed all of the presentations I attended, and I am excited to explore some new material. </p>  <p>I want to share a few thoughts on my presentation dealing with Model-View-Presenter in ASP.NET after listening to <a title="Scott Hanselman" href="http://www.hanselman.com" target="_blank">Scott Hanselman's</a> session on the <a title="ASP. NET MVC Project" href="http://www.asp.net/mvc" target="_blank">ASP.NET MVC Project</a>. I may have the percentage incorrect, but Scott mentioned something like it is predicted that only 10% of the ASP.NET community currently using Web Forms will adopt and use the MVC framework. The MVC framework is an addition to ASP.NET, it is not a replacement. What this means is that Model-View-Presenter will still be a viable pattern to implement with your ASP.NET applications, and it is not going away. ASP.NET Web Forms will not be going away either.</p>  <p>Use MVP to get your third party controls under test. Use MVP to provide that separation of concerns in your legacy applications. Use it entirely or in conjunction with the MVC framework. It is all about testability. <a title="Glenn Block" href="http://blogs.msdn.com/gblock/" target="_blank">Glenn Block</a> presented on the Web Client Software Factory, and what pattern does this implement? Model-View-Presenter.</p>  <p>I am providing my <a title="Presentation and Source Code" href="http://muellerdesigns.net/DasBlog/content/binary/ModeViewPresenter.AlexMueller.BoiseCodeCamp2008.rar" target="_blank">presentation and source code (3.67 MB)</a> from my talk on MVP. It will be available via the <a title="Boise Code Camp" href="http://www.boisecodecamp.org/" target="_blank">Boise Code Camp</a> site as well. </p>  <p>Again, thank the many individuals and their families who sacrificed their time to bring to the local community this years code camp.</p>  <p>Around the office's water cooler on Monday, I will be able to say, &quot;and this one time, at code camp...&quot;</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/09/boise-code-camp-2008/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; Design Patterns</title>
	<atom:link href="http://elegantcode.com/tag/design-patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Where to start a new program</title>
		<link>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=where-to-start-a-new-program</link>
		<comments>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/#comments</comments>
		<pubDate>Sun, 08 Jun 2008 19:50:39 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/06/08/where-to-start-a-new-program/</guid>
		<description><![CDATA[Here as an interesting question that popped up at my user group meeting last night, when you are starting a new program (green field development), where do you get started?&#160; I find these questions interesting because there really is no correct answer, but a persons answer will tell you something about how they problem solve. [...]]]></description>
			<content:encoded><![CDATA[<p>Here as an interesting question that popped up at my user group meeting last night, when you are starting a new program (green field development), where do you get started?&nbsp; I find these questions interesting because there really is no correct answer, but a persons answer will tell you something about how they problem solve.</p> <p>Depending on who you ask you will likely get one of two answers.&nbsp; If you are talking with a data guy, he will say to start with the database.&nbsp; If you are dealing with a visual guy, he will say to start with the UI.&nbsp; Historically, I did a conglomeration of those two ideas, creating both the UI and the database at the same time, letting the two drive each other.&nbsp; But I'm weird, I'm a visual/data guy.</p> <p>Lately though, I've started to thing about a third approach.&nbsp; Starting in the middle.&nbsp; No UI, no database, just the data classes.&nbsp; This approach has been spurred on buy a couple of things.&nbsp; Namely, test driven development (TDD), design patterns, and domain driven design.</p> <p>TDD really is the glue that allows all of this to happen.&nbsp; I can start in the middle because I can run the code in the middle any time I want.&nbsp; I just have to write a test.&nbsp; No having to wade through multiple layers of UI to test out some small chunk ok UI is a great productivity gain for me.</p> <p>Next is design patterns.&nbsp; This is what gives you a reference for how to approach your code.&nbsp; That said, this isn't about making everything absolutely perfect from the get go, but it is nice to know where you are going to made your comprises early on.</p> <p>But all of that does not explain where I begin.&nbsp; That is where Domain Driven Design (DDD) kicks in.&nbsp; For those not familiar with the concept, you can read up on <a href="http://en.wikipedia.org/wiki/Domain-driven_design">wikipedia</a>, buy <a href="http://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1212806974&amp;sr=8-1">The Book</a>, or buy one of the <a href="http://www.amazon.com/s?ie=UTF8&amp;tag=mozilla-20&amp;index=blended&amp;link%5Fcode=qs&amp;field-keywords=domain%20driven%20design&amp;sourceid=Mozilla-search">various other references</a>.&nbsp;&nbsp; One of the concepts in DDD (I'm still reading the book) is setting up something called a <a href="http://www.domaindrivendesign.org/discussion/messageboardarchive/UbiquitousLanguage.html">ubiquitous language</a>.</p> <p>The ubiquitous language is actually trying to solve a problem that I have seen happen on many projects I've been apart of (but not always my fault).&nbsp; At some point there is a linguistic difference between the nouns that the customer uses and the nouns that the programmers use.&nbsp; By focusing on the language first, you are attempting to head that off at the pass.</p> <p>These nouns consist of your domain classes and your tables and columns, they move everywhere...but not always consistently.&nbsp; Consistency does matter.&nbsp; Inconsistency is what leads to rather confusing conversations with people and everyone scratching their heads about "what just happened".&nbsp; So, this is the middle that I start with.</p> <p>But before you start...</p> <p>One of the other guys in the group was an actual data guy.&nbsp; The systems he has written worked with more data than yours does.&nbsp; Trust me on that.&nbsp; Historically, his biggest consideration was data input speed.&nbsp; I don't think I would start in the middle if that was my biggest concern.&nbsp; In that case you do start with the database, you tune for speed.&nbsp; You also don't use as many domain classes, and you tend to throw away your ORM solution and just hit the database layer directly via Ado.Net.</p> <p>But lets face it, that doesn't happen very often.&nbsp; Most business applications aren't going to touch what the architecture can take.&nbsp; It is amazing how many horribly written applications with very inefficient code run just fine.&nbsp; Most of my speed issues could be solved by indexing tables and changing generic lists to generic dictionaries (more on that later).&nbsp; Architect your applications for how they are going to be used.&nbsp; Most of my web applications have a max usage of 100 simultaneous users.&nbsp; If the data access is slightly slower I'll take that hit for programmer efficiency.&nbsp; If you are designing Amazon.com, you code for through-put.</p> <p>OK, someone is going to disagree with this...I can already feel it.&nbsp; Please take a moment to register your displeasure in the comments.&nbsp; I would love to hear it.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/06/08/where-to-start-a-new-program/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Learning Design Patterns</title>
		<link>http://elegantcode.com/2008/04/28/learning-design-patterns/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=learning-design-patterns</link>
		<comments>http://elegantcode.com/2008/04/28/learning-design-patterns/#comments</comments>
		<pubDate>Mon, 28 Apr 2008 19:01:17 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/28/learning-design-patterns/</guid>
		<description><![CDATA[To me this seems like an interesting way to learn about design patterns (watch the video). I wonder if it also works when I hit an ivory-tower architect with POEAA or DDD? I'll keep you posted. PS: I'm really looking forward to the next episode on code reviews and retrospectives.]]></description>
			<content:encoded><![CDATA[To me <a href="http://codebetter.com/blogs/glenn.block/archive/2008/04/28/extreme-patterns-with-oren.aspx" target="_blank">this</a> seems like an interesting way to learn about design patterns (watch the video). I wonder if it also works when I hit an ivory-tower architect with <a href="http://www.bookpool.com/sm/0321127420" target="_blank">POEAA</a> or <a href="http://www.bookpool.com/sm/0321125215" target="_blank">DDD</a>? I'll keep you posted.

PS: I'm really looking forward to the next episode on code reviews and retrospectives.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/28/learning-design-patterns/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>DTO&#8217;s or Serialized Domain Entities?</title>
		<link>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=dtos-or-serialized-domain-entities</link>
		<comments>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/#comments</comments>
		<pubDate>Sun, 27 Apr 2008 08:09:02 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/</guid>
		<description><![CDATA[I hear this question come up time and time again. It is something that I have been dealing with for a while now, and have some experience with doing both ways. I started writing this in response to a DDD thread, but it got rather large and so I turned it into a post. So [...]]]></description>
			<content:encoded><![CDATA[<p>I hear this question come up time and time again. It is something that I have been dealing with for a while now, and have some experience with doing both ways. I started writing this in response to a <a href="http://tech.groups.yahoo.com/group/domaindrivendesign/message/7286" target="_blank">DDD thread</a>, but it got rather large and so I turned it into a post. So here ya go!  <p>I think you have to write DTO's for any sort of long term Enterprise 'system of systems' solution. I fought this for some time, but since moving to an Assembler/DTO I much enjoy the freedom of being able to easily tweak my DTO "snapshot" of the aggregate root I am working with. Maintaining them is very little overhead.  <p>I tend to think of the DTO as a message, a value object. This is necessary because once an entity leaves the boundary it can no longer function within its original meaning. Its methods may be intended to only run within a given context, which is not the same once on another tier. (Things like lazy loading are a good example) Once the DTO/Message arrives to the caller, it very well may (and probably should) be translated/assembled into another model which can represent its new semantics.  <p>Some other advantages of DTO w/ Assembler or Builder:<br>-Can explicitly control the depth of the graph/tree you are serializing.<br>-Can flatten out the data where needed<br>-Can return significantly less data (maybe you only need 3 fields instead of 15)<br>-Not tightly coupled between remote boundaries (this could be a mess when working with a large team)<br>-Interoperable  <p>I wrote a post a while back showing a way to '<a href="http://elegantcode.com/2007/12/05/fun-with-generics-from-repository-to-dto/" target="_blank">templatize</a>' the process. My current implementations are much different with LINQ now, but maybe it could stimulate some ideas for whomever.  <p><em>The next thing I would like to do is remove the assembler altogether. By flagging fields with attributes or using config to specify a mapping, I could then feed <a href="http://blogs.msdn.com/meek/archive/2008/04/25/using-linq-expressions-to-generate-dynamic-methods.aspx" target="_blank">dynamic methods</a> and run a translation/builder strategy.</em>  <p>I know there are concerns about writing and maintaining assembler and DTO, but to me this is the least of the worry, a more challenging scenario would be the UnitOfWork (How to you intended to track remote state, concurrency etc) How about versioning? performance? scala.. well ilities in general?  <p>These kinds of problems lead one from Request/Response, Patterns of Enterprise Architecture &amp; DDD to a Pub/Sub Message Bus - Enterprise Integration Patterns &amp; DDDD.  <p>And to think all you want to do is send some frickin entities to a remote client&nbsp; :)</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/27/dtos-or-serialized-domain-entities/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Test Data Builders Refined</title>
		<link>http://elegantcode.com/2008/04/26/test-data-builders-refined/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=test-data-builders-refined</link>
		<comments>http://elegantcode.com/2008/04/26/test-data-builders-refined/#comments</comments>
		<pubDate>Sat, 26 Apr 2008 23:08:27 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/26/test-data-builders-refined/</guid>
		<description><![CDATA[Last year, I blogged about Test Data Builders here and here. I still use them heavily in my unit tests for creating objects with test data. Heck, I also use this pattern for fluent interfaces in production code. Here is a simple example of this approach: &#160; 1: public class CustomerBuilder 2: { 3: public [...]]]></description>
			<content:encoded><![CDATA[<p>Last year, I blogged about Test Data Builders <a href="http://vanryswyckjan.blogspot.com/2007/09/test-data-builder-versus-object-mother.html" target="_blank">here</a> and <a href="http://vanryswyckjan.blogspot.com/2007/12/test-data-builder.html" target="_blank">here</a>. I still use them heavily in my unit tests for creating objects with test data. Heck, I also use this pattern for fluent interfaces in production code. Here is a simple example of this approach:</p>  <p>&#160;</p>  <div>   <div class="csharpcode">     <pre class="alteven"><span class="lnum">   1:</span> <span class="kwrd">public</span> <span class="kwrd">class</span> CustomerBuilder</pre>

    <pre class="alteven"><span class="lnum">   2:</span> {</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     <span class="kwrd">public</span> String _firstName = <span class="str">&quot;Homer&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     <span class="kwrd">public</span> String _lastName = <span class="str">&quot;Simpson&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   5:</span>         </pre>

    <pre class="alteven"><span class="lnum">   6:</span>     <span class="kwrd">public</span> CustomerBuilder WithFirstName(String firstName)</pre>

    <pre class="alteven"><span class="lnum">   7:</span>     {</pre>

    <pre class="alteven"><span class="lnum">   8:</span>         _firstName = firstName;</pre>

    <pre class="alteven"><span class="lnum">   9:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;    </pre>

    <pre class="alteven"><span class="lnum">  10:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  11:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  12:</span>     <span class="kwrd">public</span> CustomerBuilder WithLastName(String lastName)</pre>

    <pre class="alteven"><span class="lnum">  13:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  14:</span>         _lastName = lastName;</pre>

    <pre class="alteven"><span class="lnum">  15:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven"><span class="lnum">  16:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  17:</span>         </pre>

    <pre class="alteven"><span class="lnum">  18:</span>     <span class="kwrd">public</span> Customer Build()</pre>

    <pre class="alteven"><span class="lnum">  19:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  20:</span>         <span class="kwrd">return</span> <span class="kwrd">new</span> Customer(_firstName, _lastName);    </pre>

    <pre class="alteven"><span class="lnum">  21:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  22:</span> }</pre>
  </div>
</div>

<pre class="alteven">This fluent builder class can then be used this way:</pre>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> Customer customer = <span class="kwrd">new</span> CustomerBuilder()</pre>

    <pre class="alteven"><span class="lnum">   2:</span>     .WithFirstName(<span class="str">&quot;Homer&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     .WithLastName(<span class="str">&quot;Simpson&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     .Build();</pre>
  </div>
</div>

<p>A while ago, <a href="http://codebetter.com/blogs/gregyoung/default.aspx" target="_blank">Greg Young</a> started a series of blog posts on DDDD (Distributed Domain-Driven Design), which I can highly recommend. Make sure to catch up now you still can because I think that he has a lot of stuff coming up, which I'm really looking forward to.</p>

<p>Anyhow, Greg had a couple of posts on fluent builders, which you can read <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/15/dddd-5-messages-have-fluent-builders.aspx" target="_blank">here</a>, <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/16/dddd-6-fluent-builders-alternate-ending.aspx" target="_blank">here</a> and <a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/22/dddd-8-fluent-builders-and-tests.aspx" target="_blank">here</a>. I noticed an interesting approach in the way that the target object is built. Here is an example of this approach:</p>

<p>&#160;</p>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> <span class="kwrd">public</span> <span class="kwrd">class</span> CustomerBuilder</pre>

    <pre class="alteven"><span class="lnum">   2:</span> {</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     <span class="kwrd">public</span> String _firstName = <span class="str">&quot;Homer&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   4:</span>     <span class="kwrd">public</span> String _lastName = <span class="str">&quot;Simpson&quot;</span>;</pre>

    <pre class="alteven"><span class="lnum">   5:</span>         </pre>

    <pre class="alteven"><span class="lnum">   6:</span>     <span class="kwrd">public</span> CustomerBuilder WithFirstName(String firstName)</pre>

    <pre class="alteven"><span class="lnum">   7:</span>     {</pre>

    <pre class="alteven"><span class="lnum">   8:</span>         _firstName = firstName;</pre>

    <pre class="alteven"><span class="lnum">   9:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;    </pre>

    <pre class="alteven"><span class="lnum">  10:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  11:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  12:</span>     <span class="kwrd">public</span> CustomerBuilder WithLastName(String lastName)</pre>

    <pre class="alteven"><span class="lnum">  13:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  14:</span>         _lastName = lastName;</pre>

    <pre class="alteven"><span class="lnum">  15:</span>         <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven"><span class="lnum">  16:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  17:</span>         </pre>

    <pre class="alteven"><span class="lnum">  18:</span>     <span class="kwrd">public</span> Customer Build()</pre>

    <pre class="alteven"><span class="lnum">  19:</span>     {</pre>

    <pre class="alteven"><span class="lnum">  20:</span>         <span class="kwrd">return</span> <span class="kwrd">new</span> Customer(_firstName, _lastName);    </pre>

    <pre class="alteven"><span class="lnum">  21:</span>     }</pre>

    <pre class="alteven"><span class="lnum">  22:</span>&#160; </pre>

    <pre class="alteven"><span class="lnum">  23:</span>     <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">implicit</span> <span class="kwrd">operator</span> Customer(</pre>

    <pre class="alteven"><span class="lnum">  24:</span>         CustomerBuilder builder) </pre>

    <pre class="alteven"><span class="lnum">  25:</span>     {  </pre>

    <pre class="alteven"><span class="lnum">  26:</span>         <span class="kwrd">return</span> builder.Build()    </pre>

    <pre class="alteven"><span class="lnum">  27:</span>     } </pre>

    <pre class="alteven"><span class="lnum">  28:</span> }</pre>
  </div>
</div>

<p>which results in the following usage:</p>

<p>&#160;</p>

<div>
  <div class="csharpcode">
    <pre class="alteven"><span class="lnum">   1:</span> Customer customer = <span class="kwrd">new</span> CustomerBuilder()</pre>

    <pre class="alteven"><span class="lnum">   2:</span>     .WithFirstName(<span class="str">&quot;Homer&quot;</span>)</pre>

    <pre class="alteven"><span class="lnum">   3:</span>     .WithLastName(<span class="str">&quot;Simpson&quot;</span>);</pre>
  </div>

  <br />Adding an implicit cast operator to the builder class makes that its no longer required to explicitly call the <em>Build</em> method. I keep the <em>Build</em> method around for backwards-compatibility reasons or in case I ever need it again (violating YAGNI in the process, I know, I know). I find that adding the implicit cast operator adds to the readability of the fluent interface, don't you agree?</div>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/26/test-data-builders-refined/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Name Those Patterns</title>
		<link>http://elegantcode.com/2008/04/24/name-those-patterns/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=name-those-patterns</link>
		<comments>http://elegantcode.com/2008/04/24/name-those-patterns/#comments</comments>
		<pubDate>Thu, 24 Apr 2008 16:16:13 +0000</pubDate>
		<dc:creator>Tony Rasa</dc:creator>
				<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/24/name-those-patterns/</guid>
		<description><![CDATA[In yesterday's discussion of Gang of Four Design Patterns, I said &#34;Never name your classes after the pattern itself.&#34;&#160; But then someone pointed out &#34;What about Factory classes?&#34;&#160; &#34;Oh, ok, ALWAYS name classes after the pattern.&#34;&#160; Except for Singleton or Fa&#231;ade, and maybe a few others.&#160; Ok then, NEVER name after the pattern, except when [...]]]></description>
			<content:encoded><![CDATA[<p>In yesterday's discussion of Gang of Four Design Patterns, I said &quot;Never name your classes after the pattern itself.&quot;&#160; But then someone pointed out &quot;What about Factory classes?&quot;&#160; &quot;Oh, ok, ALWAYS name classes after the pattern.&quot;&#160; Except for Singleton or Fa&#231;ade, and maybe a few others.&#160; Ok then, NEVER name after the pattern, except when you do, sometimes, and then ALWAYS name those, unless otherwise noted.</p>  <p>So, here's a list of the 23 Gang of Four patterns, along with when I'd use the name of the pattern in the implementation.&#160; Unless I didn't want to, and then I wouldn't.&#160; Feel free to agree with this list, unless you don't care to.</p>  <h3>Creational Patterns</h3>  <p><strong>Abstract Factory</strong> - After consideration, I have to say that I call my factories Factory, unless they're Repositories, Data Access Layers, DAOs, or something else.&#160; But most of the time, Factory.&#160; Because this would just be wrong:</p>  <pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> FooCreatorMakerThingy { ... }</pre>

<p><strong>Builder</strong> - Hmm, another noun. Guess I'd use this one too, for the same reason as Abstract Factory. Most of the time.&#160; </p>

<p><strong>Factory Method</strong> - I probably would not call my Factory Methods &quot;GetFooFactoryMethod()&quot; because that just looks weird.&#160;&#160; Maybe if we verbify the noun:</p>

<pre class="csharpcode"><span class="kwrd">public</span> Foo FactorialMethodizeFoo() {...}</pre>

<p>Nope, that's still dumb.</p>

<p><strong>Prototype</strong> - &quot;Specify the kinds of objects to create using a prototypical instance...&quot; So we don't care that the class is a &quot;Prototype,&quot; but we do care about the &quot;kind of object&quot; part.&#160; So don't name your prototypical classes &quot;FooPrototype.&quot;&#160; (Unless you just want to.)</p>

<p><strong>Singleton</strong> - Somebody have a case where it makes sense to name your Singleton &quot;Singleton&quot;?&#160; I can't come up with one.&#160; Anyway, you shouldn't be using singletons anymore anyway.&#160; :)</p>

<h3>Structural Patterns</h3>

<p><strong>Adapter</strong> - Adapters are pretty much called Adapters.&#160; Except when they're called Wrappers.&#160; So you can use this name too.</p>

<p><strong>Bridge</strong> - I wouldn't label a participant in a bridge pattern &quot;FooBridge,&quot; unless I was trying to impress on an interview test or something.&#160; And even then, that's a bad idea.&#160; &quot;Ohh, look, I know a complicated design pattern!&#160; Lookitme!&quot;&#160; So don't call your bridges Bridges.&#160; </p>

<p><strong>Composite</strong> - &quot;One of the goals of the Composite&#160; pattern is to make clients unaware of the specific Leaf or Composite classes they're using.&quot;&#160; So you don't want to point out that Composites are in play, that breaks the suspension of disbelief for your audience.&#160; Or something.&#160; Anyway Composites get used all over the place, if we had to label them all then it'd be FooComposite, BarComposite, TreeComposite, EverythingComposite, and what's the fun in that.</p>

<p><strong>Decorator </strong>- I think in general people don't label their Decorators Decorators.&#160; I'm sure there's a counter-example to this, but all of the Stream- and Xml-handling classes I can think of don't point out that they are Decorators.&#160; You'd probably want to stick to more descriptive names, like ASCIIStreamReader instead of cluttering things up with ASCIIStreamDecorator.&#160; We're not decorating the stream (in a literal sense), we're reading it.&#160; </p>

<p><strong>Fa&#231;ade</strong> - Don't call your Fa&#231;ade &quot;FooFa&#231;ade,&quot; that's just silly.&#160; Besides, its too much work to type that cedille into the editor if you're using an ignorant American keyboard.</p>

<p><strong>Flyweight </strong>- This is another pattern where we're going to disguise what's going on inside, in order to simplify the client's usage and make better use of resources.&#160; No reason to flaunt to the client that we're low on resources, because that's a personal and private matter - times are tough all over, and there's no shame in struggling a little bit, and hey who could have predicted that the sub-prime loan market was going to ... anyway, back to patterns.&#160;&#160; Don't call your Flyweights Flyweights, unless what you're modeling is actually a Flyweight, and then thats OK.</p>

<p><strong>Proxy </strong>- This one is standard terminology.&#160; So Proxy's ok.</p>

<h3>Behavioral Patterns (or &quot;<em>MAN this book has a lot of pages in it..</em>&quot;)</h3>

<p><strong>Chain of Responsibility </strong>- Don't even think of naming a class ChainOfResponsibility.&#160; Even with ReSharper, who'd want to have to type that?</p>

<p><strong>Command</strong> - Another convenient noun.&#160; Ok, you can create Commands.&#160; </p>

<p><strong>Interpreter </strong>- This is another pattern that doesn't have one identifying class that you could label &quot;The Interpreter.&quot;&#160; But you might create a Fa&#231;ade, and call that class Interpreter.&#160; I'm not sure if naming one pattern after another is OK or not.&#160; Makes my head hurt.&#160; </p>

<p>Iterator - Another convenient noun.&#160; Iterate away!&#160; Not that you'll have to worry too much about naming classes with this pattern if you use C#, Java, C++ or pretty much any other modern language, since this pattern is built-in.</p>

<p><strong>Mediator </strong>- This pattern promotes loose coupling, most of the time I'd call what it does a &quot;Controller,&quot; or what's being Mediated is more on a service level, so perhaps the word &quot;Service&quot; would end up in the name.&#160; Can't say I've ever called a class specifically &quot;FooMediator&quot;, but I suppose I don't have a problem with it if you want to do that.</p>

<p><strong>Memento </strong>- This pattern captures and externalizes an object's state.&#160; So I think I'd call something like it &quot;State&quot; before &quot;Memento,&quot; since it's fewer letters to type.&#160; Besides, I had to see that movie twice before I really appreciated it.&#160; </p>

<p>Ok, that was just lame, sorry.</p>

<p><strong>Observer </strong>- Oh, you mean like an Event?&#160; Or more like a delegate?&#160; :)</p>

<p><strong>State </strong>- I'd call my implementation of this pattern &quot;Memento,&quot; just to confuse you.&#160; That's just how I roll.</p>

<p><strong>Strategy </strong>- This is another pattern that it seems like we use all the time, without having to give it a fancy Ivory Tower name.&#160; As someone pointed out (sorry I lost the reference to who said that) Microsoft calls all their Strategy implementations Providers now anyway.&#160; Rather than label something a &quot;Strategy&quot; I'd probably just say what the thing does: &quot;CryptographicProvider.&quot;</p>

<p><strong>Template Method</strong> - Yet another pattern where there's no obvious place to hang the name off of, so don't try to force it in there and impress your coworkers with how smart you are.&#160; We know, you're a Design Pattern God, no need to brag about it.</p>

<p><strong>Visitor </strong>- Hmm, not sure where I stand on this one.&#160; I have used this pattern where I've named a visitor &quot;SqlGeneratingVisitor.&quot;&#160; But maybe that was a mistake?&#160; So how about we just declare this one &quot;Free Parking,&quot; and you can do what you want.</p>

<p>&#160;</p>

<p>So, there you go.&#160; A bunch of design patterns, summarized into one neat page.&#160; Now you're ready to ace that interview!&#160; </p>

<p>&#160;</p>

<p><em>disclaimer: I make no guarantees that any or all of the information will not harm you in a technical interview, and if repeating this to an interview board gets your removed from the building by security, its not my fault.&#160; This information comes without warantees or conditions of any kind, and you probably shouldn't have bothered reading it..</em></p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/24/name-those-patterns/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.Net HierarchicalDataSource&lt;T&gt;</title>
		<link>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=aspnet-hierarchicaldatasourcet</link>
		<comments>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/#comments</comments>
		<pubDate>Sun, 06 Apr 2008 06:45:44 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Extension Methods]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/</guid>
		<description><![CDATA[The Asp.net 2.0 tree control, as well as many 3rd party controls binds to hierarchical data via an IHierarchicalDataSource interface. Asp.net provides an implementation OOTB with the HierarchicalDataSourceControl! Easy, let’s hook that up to our… wait this control only takes data in xml format or from a sitemap? Hmm, we don’t want to convert our [...]]]></description>
			<content:encoded><![CDATA[<p>The Asp.net 2.0 tree control, as well as many 3rd party controls binds to hierarchical data via an IHierarchicalDataSource interface. Asp.net provides an implementation OOTB with the HierarchicalDataSourceControl! Easy, let’s hook that up to our… wait this control only takes data in xml format or from a sitemap? Hmm, we don’t want to convert our model to xml, and a sitemap seems inappropriate. We spend a lot time doing great patterns such as MVP &amp; MVC, we should just be able to bind up a list of model objects we already have.  <p>Good thing MS has exposed a set of interfaces and abstract classes in System.Web.UI for us to implement our own IHierarchicalDataSource and HierarchicalDataSourceView and IHierarchicalEnumerable and then IHierarchyData... geesh.  <p>Data? So let's see here, in order to bind our model to the tree control it needs to implement an interface in System.Web.UI? Well that's just not going to happen, so its time for some Elegant Code.  <p><strong>Story:</strong>  <p>Dude, as a software developer, needs to create a custom implementation of IHierarchicalDataSource, and the rest of its parts, so that he can bind a list of objects to a tree control in asp.net  <ul> <li>Use existing model to bind to tree control  <li>Must be able to reuse data source for future models  <li>Model must not contain a reference to System.Web.UI </li></ul> <p>In order to setup our model to address the acceptance criteria we are going to use an interface with a self-referencing generic declaration, where the type will specify itself as the concrete type. This will let us create a nice generic hierarchical model to pass into our datasource without resorting to any sort of base class.  <div><pre class="csharpcode"><span class="kwrd">namespace</span> POC.Common
{
    <span class="kwrd">public</span> <span class="kwrd">interface</span> IModelWithHierarchy&lt;T&gt;
    {
        <span class="kwrd">string</span> Name { get; set; }
        T Parent { get; set; }
        List&lt;T&gt; Children { get; set; }
    }
}</pre><em>Lets note that we have placed the interface in a Common namespace which represents a shared.dll between Web &amp; Model</em> </div>
<p>The UI part of criteria is now a bit more tricky because the HierarchicalDataSourceView expects a IHierarchicalEnumerable datasource which contains IHierarchyData. Said IHierarchyData != IModelWithHierarchy&lt;T&gt;.&nbsp; Luckily someone else has already figured this one out for us. We need an <a href="http://www.dofactory.com/Patterns/PatternAdapter.aspx" target="_blank">adapter</a>. 
<p><a href="http://elegantcode.com/wp-content/uploads/2008/04/image7.png" target="_blank"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="123" alt="image" src="http://elegantcode.com/wp-content/uploads/2008/04/image-thumb7.png" width="244" border="0"></a> 
<p>To instantiate our HierarchyData adapter objects we will use an extension method and linq 'Select' to convert our datasource IEnumerable&lt;T&gt; of IModelWithHierarchy&lt;T&gt; 's to an IHierarchicalEnumerable of IHierarchyData. 
<div><pre class="csharpcode"><span class="kwrd">namespace</span> POC.Web.HierarchyExtensions
{
    <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">class</span> HierarchyConverter
    {
        <span class="kwrd">public</span> <span class="kwrd">static</span> HierarchicalModelList
            ToHierarchicalModelList&lt;T&gt;(<span class="kwrd">this</span> IEnumerable&lt;T&gt; modelWithHierarchy)
            <span class="kwrd">where</span> T : IModelWithHierarchy&lt;T&gt;
        {
            <span class="kwrd">return</span> <span class="kwrd">new</span> HierarchicalModelList(
                modelWithHierarchy.Select(m =&gt; <span class="kwrd">new</span> HierarchyData&lt;T&gt;(m) <span class="kwrd">as</span> IHierarchyData));
        }
    }
}</pre></div>
<p>I am not going to bore you with the implementation details of the HierarachyData&lt;T&gt; adapter and the .net abstractions, but have dropped the <a href="http://elegantcode.com/wp-content/uploads/2008/04/hierarchydatasourcepoc.zip" target="_blank">source code here</a> if your interested. The end result: 
<div><pre class="csharpcode">TreeView1.DataSource = <span class="kwrd">new</span> HierarchicalModelDataSource&lt;Category&gt; { DataSource = Categories };
TreeView1.DataBind();
TreeView1.CollapseAll();</pre></div>
<p>Story complete!&nbsp; </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/06/aspnet-hierarchicaldatasourcet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is Elegant Code to me?</title>
		<link>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-is-elegant-code-to-me</link>
		<comments>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/#comments</comments>
		<pubDate>Mon, 31 Mar 2008 03:47:45 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Patterns and Practices]]></category>
		<category><![CDATA[Widgets of Wisdom]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/</guid>
		<description><![CDATA[This question keeps popping up around here ("around here" being the loose conglomeration that makes up the Elegant Code group).&#160; It isn't easy to describe.&#160; And really, the notion of what constitutes elegance in code changes over time.&#160; There is no static "this is good code" test, and I doubt there ever will be.&#160; Plus, [...]]]></description>
			<content:encoded><![CDATA[<p>This question keeps popping up around here ("around here" being the loose conglomeration that makes up the Elegant Code group).&nbsp; It isn't easy to describe.&nbsp; And really, the notion of what constitutes elegance in code changes over time.&nbsp; There is no static "this is good code" test, and I doubt there ever will be.&nbsp; Plus, what makes good code in one language, may not apply to the next.</p> <p>So let me state for the record: today's elegant code is tomorrow's drivel.&nbsp; Don't feel bad, many writers have the same problems.&nbsp; What was super amazing back in the day is now rubbish or near unreadable.&nbsp; I am thinking of the Victorian writing that Hemingway usurped, and now Hemingway himself is almost unreadable (to me anyway).&nbsp; Tastes change with the times.&nbsp; That is a simple fact.</p> <p>So what can you do about this?&nbsp; As I say that old writing sucks to read (read Washington Irvine lately?), great works of literature still abound (for instance, Hemingway is still a great writer -- even if I prefer Tolkien) .&nbsp; So take some notes from them, and from other crafts in looking for the answer.&nbsp; If you want a cliff notes version of what this is going to tell you, it is this: you must always push yourself to get better.</p> <ol> <li>Find a good teacher.&nbsp; Nothing is better than sitting at the feet of a master who can nudge you along in the right direction.&nbsp; While early mistakes can often be corrected easily with a little bit of guidance, after they have had some time to fester all bets are off.&nbsp; I believe a fare number of "Anit-patterns" were created by self taught developers (find <a href="http://elegantcode.com/2008/03/21/sql-ejaculation/">SQL Ejaculation</a> on this site).&nbsp; <li>Read.&nbsp;&nbsp; Good writer are first good readers.&nbsp; Start with Code Complete, move into a good Patterns book, get MSDN Magazine, find some bloggers your like, but keep moving.&nbsp; You will NEVER be done reading.&nbsp; I imagine that even Martin Fowler has a "to read" booklist a mile long.  <li>Read code.&nbsp;&nbsp; There are a plethora of open source project just waiting to be read -- do so.&nbsp; This is the single best way to expand your coding repertoire, find things your language can do that you didn't even know about..&nbsp;&nbsp; Scott Hanselman has recently popularized this idea, and I thank him for it.  <li>Practice.&nbsp; This means writing small applications at home.&nbsp; You get an idea that you want to try out -- do it.&nbsp; I don't mean you have to write finished applications, just have some exploring time.&nbsp; I remember talking with an 80 year old master woodworker (he lives down the street from me), who was telling me how many time he would practice making dovetail joints before he felt competent.&nbsp; It was years worth.  <li>Pay Attention.&nbsp; Being good at anything really amounts to that.&nbsp; And don't just pay attention when reading, writing, or talking about code.&nbsp; Inspiration come from everywhere, any good artist will tell you that.&nbsp; Pay attention when you cook, when you work on the car, when you are wood working, playing an instrument, whatever it is that you do.&nbsp; This will help you in the end, event if it is just learning the amount of dedication it really takes to become good at something.  <li>Beware of preferences.&nbsp; Any time I hear someone start a statement with "I prefer code to ..." you know things are going downhill.&nbsp; If you find someone who cares more about how your code is formatted then how it is written you have found yourself in a mess.&nbsp; More importantly, beware of them in yourself.&nbsp;&nbsp; Having code style standard is important, but it isn't worth loosing sleep over.&nbsp; This also pertains to inheritance, patterns, use of particular classes (e.g. always using the generic list class in C# when a Dictionary would be better).&nbsp; <li>No Sacred Cows. Believe no single source of information.&nbsp; This means not thinking that Microsoft, Sun, IBM, Richard Stallman, or anyone else will have all of the correct answers.&nbsp; Apply the scientific method and do some research, experiment, and question the authority.&nbsp;&nbsp; There should be no sacred cows in programming.  <li>Talk with others.&nbsp; Join a user group, show up every now and then, heckle the presenter, and -maybe- speak.&nbsp; You want a broad range of people who you can talk to and bounce ideas off of.&nbsp; This will help you from becoming that crazy guy in the corner who vehemently states that all your code should be in one file.&nbsp; Having a mentor (mentioned earlier) is great, but having people around to push you from multiple directions is also good.  <li>Learn languages.&nbsp; This gets back to the "read code" idea. Make that multiple types of languages as well.&nbsp; If you know C# or Java and SQL, learn Python or Ruby, get really good at JavaScript.&nbsp; If you want something really out there, learn MDX.&nbsp; This is also a repertoire thing.&nbsp; Seeing how other languages work will make you rethink your ideas about what your current code, in whatever languages you are working in, should look like.  <li>Keep Thinking!!!&nbsp; That is possibly the most important point.&nbsp; Keep thinking.&nbsp; Don't just evaluate something once and never return, go back and reevaluate. Did the technique work?&nbsp; How could it have been better?&nbsp;&nbsp; The idea is continual improvement.  <li>Switch jobs every now and then.&nbsp; When I announced I was leaving my first programming job out of college, the VP of R&amp;D had me sit down with him and talk.&nbsp; He wasn't trying to keep me (he knew I was moving to be closer to family), he wanted to give me some advice.&nbsp; He told me to change jobs every three years.&nbsp; After you have been with a place for three years you have probably learned everything you are going to learn and it time to move on.&nbsp; This doesn't mean changing companies either.&nbsp; I've been with the same company now for four years, but I've had three different jobs.&nbsp; If you have been writing commercial applications, become a consultant - or vise-versa, become a test engineer, expand your boundaries.&nbsp; Again, this is about pushing yourself to be better.  <li>Have a Hobby. But don't ask about me, I have too many.&nbsp;&nbsp; Creating software is about creating things.&nbsp; So I suggest picking a hobby that involves creating something.&nbsp; Popular hobbies amongst programmers I know include: cooking, music, woodworking, beer making, and BBQ (which is different than cooking).&nbsp; But don't discount sports (golf is always popular), computer games, and board games.&nbsp; Anything that promotes the development of skill levels can't be a bad thing. </li></ol> <p>And I'm stopping there.&nbsp; This is my beginner's guide to how to become the writer elegant code.&nbsp; If you have others you think I missed, add them too the comments.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/30/what-is-elegant-code-to-me/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>LINQ Framework Design Guidelines</title>
		<link>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=linq-framework-design-guidelines-2</link>
		<comments>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/#comments</comments>
		<pubDate>Wed, 19 Mar 2008 01:53:22 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[Microsoft]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/</guid>
		<description><![CDATA[Excellent article on design considerations for LINQ. Looks like these concepts will be incorporated into the 2nd Edition of Framework Design Guidelines: Conventions, Idioms, and Patterns for Reuseable .NET Libraries. Cant wait to get my hands on a copy of that!]]></description>
			<content:encoded><![CDATA[<p>Excellent article on <a href="http://blogs.msdn.com/mirceat/archive/2008/03/13/linq-framework-design-guidelines.aspx" target="_blank">design considerations for LINQ</a>. Looks like these concepts will be incorporated into the 2nd Edition of <a href="http://www.amazon.com/Framework-Design-Guidelines-Conventions-Development/dp/0321545613/ref=pd_bbs_sr_2?ie=UTF8&amp;s=books&amp;qid=1200508590&amp;sr=1-2" target="_blank">Framework Design Guidelines: Conventions, Idioms, and Patterns for Reuseable .NET Libraries</a>. Cant wait to get my hands on a copy of that!</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/18/linq-framework-design-guidelines-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Boise Code Camp 2008</title>
		<link>http://elegantcode.com/2008/03/09/boise-code-camp-2008/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=boise-code-camp-2008</link>
		<comments>http://elegantcode.com/2008/03/09/boise-code-camp-2008/#comments</comments>
		<pubDate>Mon, 10 Mar 2008 02:25:35 +0000</pubDate>
		<dc:creator>Alex Mueller</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Code Camp]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Patterns and Practices]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/03/09/boise-code-camp-2008/</guid>
		<description><![CDATA[Boise Code Camp 2008 was awesome. Thank you to David Starr and his wife, Eleanor, for taking ownership of this event and dedicating many months of their time to make this a reality. Thank you to the presenters for having the passion and desire to present technologies and practices, both new and not-as-new. Thank you [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Boise Code Camp 2008" href="http://www.boisecodecamp.org/" target="_blank">Boise Code Camp 2008</a> was awesome. Thank you to <a title="David Starr" href="http://elegantcode.com/about/david-starr/" target="_blank">David Starr</a> and his wife, <a title="Domestic Oblivion" href="http://domesticoblivion.com/" target="_blank">Eleanor</a>, for taking ownership of this event and dedicating many months of their time to make this a reality. Thank you to the presenters for having the passion and desire to present technologies and practices, both new and not-as-new. Thank you to the many volunteers who put in countless hours to help coordinate the weekend's events. Thank you to the campers for attending and making this year's code camp a success.</p>  <p>I am sure everyone will be blogging about all the great sessions they attended at code camp. I enjoyed all of the presentations I attended, and I am excited to explore some new material. </p>  <p>I want to share a few thoughts on my presentation dealing with Model-View-Presenter in ASP.NET after listening to <a title="Scott Hanselman" href="http://www.hanselman.com" target="_blank">Scott Hanselman's</a> session on the <a title="ASP. NET MVC Project" href="http://www.asp.net/mvc" target="_blank">ASP.NET MVC Project</a>. I may have the percentage incorrect, but Scott mentioned something like it is predicted that only 10% of the ASP.NET community currently using Web Forms will adopt and use the MVC framework. The MVC framework is an addition to ASP.NET, it is not a replacement. What this means is that Model-View-Presenter will still be a viable pattern to implement with your ASP.NET applications, and it is not going away. ASP.NET Web Forms will not be going away either.</p>  <p>Use MVP to get your third party controls under test. Use MVP to provide that separation of concerns in your legacy applications. Use it entirely or in conjunction with the MVC framework. It is all about testability. <a title="Glenn Block" href="http://blogs.msdn.com/gblock/" target="_blank">Glenn Block</a> presented on the Web Client Software Factory, and what pattern does this implement? Model-View-Presenter.</p>  <p>I am providing my <a title="Presentation and Source Code" href="http://muellerdesigns.net/DasBlog/content/binary/ModeViewPresenter.AlexMueller.BoiseCodeCamp2008.rar" target="_blank">presentation and source code (3.67 MB)</a> from my talk on MVP. It will be available via the <a title="Boise Code Camp" href="http://www.boisecodecamp.org/" target="_blank">Boise Code Camp</a> site as well. </p>  <p>Again, thank the many individuals and their families who sacrificed their time to bring to the local community this years code camp.</p>  <p>Around the office's water cooler on Monday, I will be able to say, &quot;and this one time, at code camp...&quot;</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/03/09/boise-code-camp-2008/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Microsoft releases Unity, an IoC library</title>
		<link>http://elegantcode.com/2008/02/17/microsoft-releases-unity-an-ioc-library/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=microsoft-releases-unity-an-ioc-library</link>
		<comments>http://elegantcode.com/2008/02/17/microsoft-releases-unity-an-ioc-library/#comments</comments>
		<pubDate>Mon, 18 Feb 2008 03:41:42 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Patterns and Practices]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/02/17/microsoft-releases-unity-an-ioc-library/</guid>
		<description><![CDATA[Here is a bit of interesting news out of Microsoft.&#160; They have released their first IoC container named Unity.&#160; It is out on CodePlex now: http://www.codeplex.com/unity If you are looking for a similar product with a longer lineage, you could look up: StructureMap, Spring.Net, and Castle Windsor.&#160; StructureMap is maintained by Jeremy Miller, who is [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a bit of interesting news out of Microsoft.&nbsp; They have released their first IoC container named Unity.&nbsp; It is out on CodePlex now: <a href="http://www.codeplex.com/unity">http://www.codeplex.com/unity</a></p> <p>If you are looking for a similar product with a longer lineage, you could look up: <a href="http://structuremap.sourceforge.net/Default.htm">StructureMap</a>, <a href="http://www.springframework.net/">Spring.Net</a>, and <a href="http://www.castleproject.org/container/">Castle Windsor</a>.&nbsp; StructureMap is maintained by <a href="http://codebetter.com/blogs/jeremy.miller/">Jeremy Miller</a>, who is a favorite blogger of mine (everyone should read his series on <a href="http://codebetter.com/blogs/jeremy.miller/archive/2007/07/25/the-build-your-own-cab-series-table-of-contents.aspx">Build your own CAB</a>).&nbsp; Spring.Net is a port of an existing Java project, and Castle Windsor seems to have a lot of mind share.&nbsp; Currently, Spring.Net is the only IoC library that I have used.</p> <p>OK, definitions: <br>IoC: Inversion of Control, also called Dependency Injection.<br>This is based on a pattern found in the Gang Of Four books on Design Patterns.<br>You can read more about this pattern at Martin Fowlers web site:<br><a href="http://martinfowler.com/articles/injection.html">http://martinfowler.com/articles/injection.html</a><br>Or on Wikipedia:<br><a href="http://en.wikipedia.org/wiki/Inversion_of_control">http://en.wikipedia.org/wiki/Inversion_of_control</a></p> <p>The basic principle that IoC is used for is Loose Coupling.&nbsp; If you have a multi-layered application, each layer should not have to know specific details about the other layers.&nbsp; So each object in a specific layer will implement an interface that the IoC provider knows about.&nbsp; Then, when a specific dependency is needed, you ask the IoC container for it (note: the IoC container is Tightly coupled) for the object to do the work.&nbsp;&nbsp; If you look at the Asp.Net Membership Provider, you can see a simple example of that at work.</p> <p>One of the ways that this can help you is that now all of your middle pieces can be swapped out without having to make massive code changes.&nbsp; This also makes testing easier, as now you can more easily stub/mock/fake dependencies and just test your application logic.</p> <p>Also, if you really get into the whole IoC thing, you will find that there are a number of other cool Computer Sciency things are possible.&nbsp; Like Aspect Oriented Programming (unknown if Unity supports that or not).</p> <p><em>Now with all of the other IoC libraries out there, why would I like the fact that Microsoft has created their own?&nbsp; <br></em>One of the current issues with a lot of Microsoft software, especially the samples, is that they are all VERY tightly coupled (again, tight coupling is not good), but it is a pain to get around without an IoC solution.&nbsp; And, as per Microsoft's own structure, they will not use any software that they didn't write or own.&nbsp;&nbsp; So I'm hopeful that now they can start producing some software with loose coupling in mind, and make all of our lives easier.</p> <p>All right, there are still a lot of definitions and ideas that could leave you puzzled, that happens.&nbsp; That is also why we have google.&nbsp; If you see something you don't understand, leave a comment and I'll try to clarify, or point you to someone else with a better explanation than I can give.</p> <p>Finally, if you are looking for more information on this sort of thing, check out the session on Castle Windsor at <a href="http://BoiseCodeCamp.com">Boise Code Camp</a> on March 8.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/02/17/microsoft-releases-unity-an-ioc-library/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

