<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Fluent hierarchical construction</title>
	<atom:link href="http://lostechies.com/jimmybogard/2008/10/15/fluent-hierarchical-construction/feed/" rel="self" type="application/rss+xml" />
	<link>http://lostechies.com/jimmybogard/2008/10/15/fluent-hierarchical-construction/</link>
	<description>Strong opinions, weakly held</description>
	<lastBuildDate>Sun, 19 May 2013 03:22:18 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
	<item>
		<title>By: Marti Kaljuve</title>
		<link>http://lostechies.com/jimmybogard/2008/10/15/fluent-hierarchical-construction/#comment-961</link>
		<dc:creator>Marti Kaljuve</dc:creator>
		<pubDate>Sat, 18 Oct 2008 20:49:59 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2008/10/15/fluent-hierarchical-construction.aspx#comment-961</guid>
		<description>oops, don&#039;t have much experience with CommunityServer text formatting.. I&#039;ll try adding pre tags:

&lt;pre&gt;public static Menu BuildMenu()
{
    var menu =
        new Menu()
            .AddItem(&quot;Products&quot;, new Menu()
                .AddItem(&quot;Hardware&quot;, new Menu()
                    .AddItem(&quot;Computers&quot;)
                    .AddItem(&quot;Printers&quot;))
                .AddItem(&quot;Software&quot;))
            .AddItem(&quot;Services&quot;, new Menu()
                .AddItem(&quot;Consulting&quot;)
                .AddItem(&quot;On-Site Support&quot;))
            .AddItem(&quot;Support&quot;)
            .AddItem(&quot;Contact&quot;);

    return menu;
}&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>oops, don&#8217;t have much experience with CommunityServer text formatting.. I&#8217;ll try adding pre tags:</p>
<pre>public static Menu BuildMenu()
{
    var menu =
        new Menu()
            .AddItem("Products", new Menu()
                .AddItem("Hardware", new Menu()
                    .AddItem("Computers")
                    .AddItem("Printers"))
                .AddItem("Software"))
            .AddItem("Services", new Menu()
                .AddItem("Consulting")
                .AddItem("On-Site Support"))
            .AddItem("Support")
            .AddItem("Contact");

    return menu;
}</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marti Kaljuve</title>
		<link>http://lostechies.com/jimmybogard/2008/10/15/fluent-hierarchical-construction/#comment-960</link>
		<dc:creator>Marti Kaljuve</dc:creator>
		<pubDate>Sat, 18 Oct 2008 20:16:00 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2008/10/15/fluent-hierarchical-construction.aspx#comment-960</guid>
		<description>This type of chaining should also work:

public static Menu BuildMenu(){    var menu =		new Menu()			.AddItem(&quot;Products&quot;, new Menu()				.AddItem(&quot;Hardware&quot;, new Menu()					.AddItem(&quot;Computers&quot;)					.AddItem(&quot;Printers&quot;))				.AddItem(&quot;Software&quot;))			.AddItem(&quot;Services&quot;, new Menu()				.AddItem(&quot;Consulting&quot;)				.AddItem(&quot;On-Site Support&quot;))			.AddItem(&quot;Support&quot;)			.AddItem(&quot;Contact&quot;);    return menu;}

It&#039;s basically the same as the original solution, but instead of params MenuItem[] menuItems in the constructor, there is an overload for AddItem that takes a single Menu object as the last parameter in case of a submenu.

The indentation makes it easy to read, but the new Menu() initializers add some noise.</description>
		<content:encoded><![CDATA[<p>This type of chaining should also work:</p>
<p>public static Menu BuildMenu(){    var menu =		new Menu()			.AddItem(&#8220;Products&#8221;, new Menu()				.AddItem(&#8220;Hardware&#8221;, new Menu()					.AddItem(&#8220;Computers&#8221;)					.AddItem(&#8220;Printers&#8221;))				.AddItem(&#8220;Software&#8221;))			.AddItem(&#8220;Services&#8221;, new Menu()				.AddItem(&#8220;Consulting&#8221;)				.AddItem(&#8220;On-Site Support&#8221;))			.AddItem(&#8220;Support&#8221;)			.AddItem(&#8220;Contact&#8221;);    return menu;}</p>
<p>It&#8217;s basically the same as the original solution, but instead of params MenuItem[] menuItems in the constructor, there is an overload for AddItem that takes a single Menu object as the last parameter in case of a submenu.</p>
<p>The indentation makes it easy to read, but the new Menu() initializers add some noise.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Fernando Ar&#225;mburu</title>
		<link>http://lostechies.com/jimmybogard/2008/10/15/fluent-hierarchical-construction/#comment-959</link>
		<dc:creator>Fernando Ar&#225;mburu</dc:creator>
		<pubDate>Fri, 17 Oct 2008 15:07:21 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2008/10/15/fluent-hierarchical-construction.aspx#comment-959</guid>
		<description>I think David Alpert comment is the right one. He thought about building a menu as the builder design pattern said. 
&quot;Different View&quot; point of view sounds to me very strange as why do you need a DataBase to create objects? you just need object and their big potencial to build another objects.
By the way , and just as we are talking about Constructor and as we are also talking about object oriented paradigm, I would say we should always do one thing you mention: &quot;Create a constructor that takes all necessary properties to set up the individual item&quot;. That&#039;s the important thing. Imagine you create a Menu without a title ... what does this menu means? If you make just one Menu constructor with its name you will always get Menu instances that means something... 
Another example of good use of Constructor would be a constructor for Persons.. Imagine a Person class and empty constructor... a person without name, age, sex? that wouldn&#039;t be a person. A person should be instanciated with Name, Age, Sex and all other staff you need to have on the next statement a meaning Person.

My two object oriented cents :)</description>
		<content:encoded><![CDATA[<p>I think David Alpert comment is the right one. He thought about building a menu as the builder design pattern said.<br />
&#8220;Different View&#8221; point of view sounds to me very strange as why do you need a DataBase to create objects? you just need object and their big potencial to build another objects.<br />
By the way , and just as we are talking about Constructor and as we are also talking about object oriented paradigm, I would say we should always do one thing you mention: &#8220;Create a constructor that takes all necessary properties to set up the individual item&#8221;. That&#8217;s the important thing. Imagine you create a Menu without a title &#8230; what does this menu means? If you make just one Menu constructor with its name you will always get Menu instances that means something&#8230;<br />
Another example of good use of Constructor would be a constructor for Persons.. Imagine a Person class and empty constructor&#8230; a person without name, age, sex? that wouldn&#8217;t be a person. A person should be instanciated with Name, Age, Sex and all other staff you need to have on the next statement a meaning Person.</p>
<p>My two object oriented cents <img src='http://lostechies.com/jimmybogard/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jimmy Bogard</title>
		<link>http://lostechies.com/jimmybogard/2008/10/15/fluent-hierarchical-construction/#comment-958</link>
		<dc:creator>Jimmy Bogard</dc:creator>
		<pubDate>Fri, 17 Oct 2008 12:27:27 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2008/10/15/fluent-hierarchical-construction.aspx#comment-958</guid>
		<description>@Jacob

Collection initializer is definitely another option.  Do you find it easier to read?  Personally, the brackets seem to add more distractions than purely constructors/builder methods.</description>
		<content:encoded><![CDATA[<p>@Jacob</p>
<p>Collection initializer is definitely another option.  Do you find it easier to read?  Personally, the brackets seem to add more distractions than purely constructors/builder methods.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jacob Stanley</title>
		<link>http://lostechies.com/jimmybogard/2008/10/15/fluent-hierarchical-construction/#comment-957</link>
		<dc:creator>Jacob Stanley</dc:creator>
		<pubDate>Fri, 17 Oct 2008 06:46:51 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2008/10/15/fluent-hierarchical-construction.aspx#comment-957</guid>
		<description>Another option would be using a collection initializer:

public static MenuItem BuildMenu()
{
    var menu = new Menu()
    {
        new MenuItem(&quot;Products&quot;)
        {
            new MenuItem(&quot;Hardware&quot;)
            {
                new MenuItem(&quot;Computers&quot;),
                new MenuItem(&quot;Printers&quot;)
            },
            new MenuItem(&quot;Software&quot;)
        },
        new MenuItem(&quot;Services&quot;)
        {
            new MenuItem(&quot;Consulting&quot;),
            new MenuItem(&quot;On-Site Support&quot;)
        },
        new MenuItem(&quot;Support&quot;),
        new MenuItem(&quot;Contact&quot;)
    };

    return menu;
}

You&#039;d just need to change your AddItem method to Add, and make sure the class supports IEnumerable:

public class MenuItem : IEnumerable&lt;MenuItem&gt;
{
    private readonly List&lt;MenuItem&gt; _menuItems = new List&lt;MenuItem&gt;();

    public MenuItem(string title)
    {
        Title = title;
    }

    public string Title
    {
        get;
        set;
    }

    public void Add(MenuItem menuItem)
    {
        _menuItems.Add(menuItem);
    }

    public IEnumerator&lt;MenuItem&gt; GetEnumerator()
    {
        return _menuItems.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}</description>
		<content:encoded><![CDATA[<p>Another option would be using a collection initializer:</p>
<p>public static MenuItem BuildMenu()<br />
{<br />
    var menu = new Menu()<br />
    {<br />
        new MenuItem(&#8220;Products&#8221;)<br />
        {<br />
            new MenuItem(&#8220;Hardware&#8221;)<br />
            {<br />
                new MenuItem(&#8220;Computers&#8221;),<br />
                new MenuItem(&#8220;Printers&#8221;)<br />
            },<br />
            new MenuItem(&#8220;Software&#8221;)<br />
        },<br />
        new MenuItem(&#8220;Services&#8221;)<br />
        {<br />
            new MenuItem(&#8220;Consulting&#8221;),<br />
            new MenuItem(&#8220;On-Site Support&#8221;)<br />
        },<br />
        new MenuItem(&#8220;Support&#8221;),<br />
        new MenuItem(&#8220;Contact&#8221;)<br />
    };</p>
<p>    return menu;<br />
}</p>
<p>You&#8217;d just need to change your AddItem method to Add, and make sure the class supports IEnumerable:</p>
<p>public class MenuItem : IEnumerable<br />
<menuitem>
{<br />
    private readonly List</menuitem>
<menuitem> _menuItems = new List</menuitem>
<menuitem>();</p>
<p>    public MenuItem(string title)<br />
    {<br />
        Title = title;<br />
    }</p>
<p>    public string Title<br />
    {<br />
        get;<br />
        set;<br />
    }</p>
<p>    public void Add(MenuItem menuItem)<br />
    {<br />
        _menuItems.Add(menuItem);<br />
    }</p>
<p>    public IEnumerator</menuitem>
<menuitem> GetEnumerator()<br />
    {<br />
        return _menuItems.GetEnumerator();<br />
    }</p>
<p>    IEnumerator IEnumerable.GetEnumerator()<br />
    {<br />
        return GetEnumerator();<br />
    }<br />
}</menuitem>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jimmy Bogard</title>
		<link>http://lostechies.com/jimmybogard/2008/10/15/fluent-hierarchical-construction/#comment-956</link>
		<dc:creator>Jimmy Bogard</dc:creator>
		<pubDate>Thu, 16 Oct 2008 22:21:26 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2008/10/15/fluent-hierarchical-construction.aspx#comment-956</guid>
		<description>@Different view

Yes, this would _usually_ be put in the database.  But there are cases where I need to define hierarchies that shouldn&#039;t be in the database.  This really wasn&#039;t about menu items, rather building hierarchies.

In our case, each menu item represents new code.  Since code and menus are tightly coupled, no point in putting them in the DB, as it would be more work to re-correlate them with specific view pages/controller actions.</description>
		<content:encoded><![CDATA[<p>@Different view</p>
<p>Yes, this would _usually_ be put in the database.  But there are cases where I need to define hierarchies that shouldn&#8217;t be in the database.  This really wasn&#8217;t about menu items, rather building hierarchies.</p>
<p>In our case, each menu item represents new code.  Since code and menus are tightly coupled, no point in putting them in the DB, as it would be more work to re-correlate them with specific view pages/controller actions.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Different view</title>
		<link>http://lostechies.com/jimmybogard/2008/10/15/fluent-hierarchical-construction/#comment-955</link>
		<dc:creator>Different view</dc:creator>
		<pubDate>Thu, 16 Oct 2008 21:24:44 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2008/10/15/fluent-hierarchical-construction.aspx#comment-955</guid>
		<description>Sorry, I&#039;m not a big fan of this code.

Yes it&#039;s readable, however it has very few benefits over simply writing straight HTML... you&#039;re procedurally defining the options and there is no persistent queryable structure to build on in the future.

To create a re-usable menu structure of arbitrary depth, I usually pull it from a table like this (MySQL semantics but you get the drift):

 create table menu_items(
  id int unsigned not null auto_increment primary key,
  parent_id int unsigned
 );

 alter table menu_items add foreign key(parent_id) references menu_items(id) on delete set null on update cascade;

Then create a different table to hold titles, which may potentially be multilingual...

 create table menu_item_names(
  id int unsigned not null auto_increment primary key,
  item int unsigned not null,
  name tinytext not null,
  language int unsigned not null
 );

 alter table menu_item_names add foreign key(language) references languages(id) on delete cascade on update cascade;
 alter table menu_item_names add foreign key(item) references menu_items(id) on delete cascade on update cascade;

Just a different way to accomplish the same thing ... but more extensible / persistent (ie: tightly database-coupled, and therefore easier to build a simple interface for and delegate management to users...).

It&#039;s partly a different outlook - to me, these days, one-off code is just too hard to maintain, I tend to put the extra time in up front for peace of mind and re-usability.</description>
		<content:encoded><![CDATA[<p>Sorry, I&#8217;m not a big fan of this code.</p>
<p>Yes it&#8217;s readable, however it has very few benefits over simply writing straight HTML&#8230; you&#8217;re procedurally defining the options and there is no persistent queryable structure to build on in the future.</p>
<p>To create a re-usable menu structure of arbitrary depth, I usually pull it from a table like this (MySQL semantics but you get the drift):</p>
<p> create table menu_items(<br />
  id int unsigned not null auto_increment primary key,<br />
  parent_id int unsigned<br />
 );</p>
<p> alter table menu_items add foreign key(parent_id) references menu_items(id) on delete set null on update cascade;</p>
<p>Then create a different table to hold titles, which may potentially be multilingual&#8230;</p>
<p> create table menu_item_names(<br />
  id int unsigned not null auto_increment primary key,<br />
  item int unsigned not null,<br />
  name tinytext not null,<br />
  language int unsigned not null<br />
 );</p>
<p> alter table menu_item_names add foreign key(language) references languages(id) on delete cascade on update cascade;<br />
 alter table menu_item_names add foreign key(item) references menu_items(id) on delete cascade on update cascade;</p>
<p>Just a different way to accomplish the same thing &#8230; but more extensible / persistent (ie: tightly database-coupled, and therefore easier to build a simple interface for and delegate management to users&#8230;).</p>
<p>It&#8217;s partly a different outlook &#8211; to me, these days, one-off code is just too hard to maintain, I tend to put the extra time in up front for peace of mind and re-usability.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jimmy Bogard</title>
		<link>http://lostechies.com/jimmybogard/2008/10/15/fluent-hierarchical-construction/#comment-954</link>
		<dc:creator>Jimmy Bogard</dc:creator>
		<pubDate>Wed, 15 Oct 2008 17:53:02 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2008/10/15/fluent-hierarchical-construction.aspx#comment-954</guid>
		<description>I think I still like Scott&#039;s suggestion the best.  The idea was to have a very readable building of a hierarchy.  Method chaining has its place, but any time you have &quot;End&quot;, it&#039;s an indication that chaining isn&#039;t appropriate.  Really, it&#039;s the &quot;params&quot; keyword that enables a hierarchical code structure/object model.</description>
		<content:encoded><![CDATA[<p>I think I still like Scott&#8217;s suggestion the best.  The idea was to have a very readable building of a hierarchy.  Method chaining has its place, but any time you have &#8220;End&#8221;, it&#8217;s an indication that chaining isn&#8217;t appropriate.  Really, it&#8217;s the &#8220;params&#8221; keyword that enables a hierarchical code structure/object model.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott Bellware</title>
		<link>http://lostechies.com/jimmybogard/2008/10/15/fluent-hierarchical-construction/#comment-953</link>
		<dc:creator>Scott Bellware</dc:creator>
		<pubDate>Wed, 15 Oct 2008 17:14:42 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2008/10/15/fluent-hierarchical-construction.aspx#comment-953</guid>
		<description>David,

Why does &quot;With&quot; need to prefix what are essentially operations that describe structure?  That stuff is mostly noise.

I think it&#039;s valuable to question when building fluent interfaces whether we are cognizant of whether we&#039;re trying to create domain-specific languages, or English.  if we&#039;re trying to create English, and we believe that English is what makes program code readable, we&#039;re likely off-target.
</description>
		<content:encoded><![CDATA[<p>David,</p>
<p>Why does &#8220;With&#8221; need to prefix what are essentially operations that describe structure?  That stuff is mostly noise.</p>
<p>I think it&#8217;s valuable to question when building fluent interfaces whether we are cognizant of whether we&#8217;re trying to create domain-specific languages, or English.  if we&#8217;re trying to create English, and we believe that English is what makes program code readable, we&#8217;re likely off-target.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Alpert</title>
		<link>http://lostechies.com/jimmybogard/2008/10/15/fluent-hierarchical-construction/#comment-952</link>
		<dc:creator>David Alpert</dc:creator>
		<pubDate>Wed, 15 Oct 2008 16:15:13 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2008/10/15/fluent-hierarchical-construction.aspx#comment-952</guid>
		<description>With &quot;fluent&quot; in your title, i expected something more like the following:

public static Menu BuildMenu()
{
    var menu =
        new Menu().
                  WithSubMenu(&quot;Products&quot;).
                         WithSubMenu(&quot;Hardware&quot;).
                               WithItem(&quot;Computers&quot;).
                               WithItem(&quot;Printers&quot;).
                          End().
                          WithItem(&quot;Software&quot;).
                    End().
                    WithSubMenu(&quot;Services&quot;).
                          WithItem(&quot;Consulting&quot;).
                          WithItem(&quot;On-Site Support&quot;).
                    End().
                    WithItem(&quot;Support&quot;).
                    WithItem(&quot;Contact&quot;);

    return menu;
}

The examples tagged with &quot;fluent&quot; that i have seen floating around the blogosphere all seem to expose an API that leans on chaining (returning some helper object), reads like English, and provides contextual intellisense as you build your expression.

In the above example, i&#039;d probably execute as an extension method on MenuItem that operates on it&#039;s collection and returns the updated MenuItem.  Hmmm, how to handle the submenu chaining?  perhaps returning an intermediate helper object to ensure that the return type would notify you if you had forgotten an End()</description>
		<content:encoded><![CDATA[<p>With &#8220;fluent&#8221; in your title, i expected something more like the following:</p>
<p>public static Menu BuildMenu()<br />
{<br />
    var menu =<br />
        new Menu().<br />
                  WithSubMenu(&#8220;Products&#8221;).<br />
                         WithSubMenu(&#8220;Hardware&#8221;).<br />
                               WithItem(&#8220;Computers&#8221;).<br />
                               WithItem(&#8220;Printers&#8221;).<br />
                          End().<br />
                          WithItem(&#8220;Software&#8221;).<br />
                    End().<br />
                    WithSubMenu(&#8220;Services&#8221;).<br />
                          WithItem(&#8220;Consulting&#8221;).<br />
                          WithItem(&#8220;On-Site Support&#8221;).<br />
                    End().<br />
                    WithItem(&#8220;Support&#8221;).<br />
                    WithItem(&#8220;Contact&#8221;);</p>
<p>    return menu;<br />
}</p>
<p>The examples tagged with &#8220;fluent&#8221; that i have seen floating around the blogosphere all seem to expose an API that leans on chaining (returning some helper object), reads like English, and provides contextual intellisense as you build your expression.</p>
<p>In the above example, i&#8217;d probably execute as an extension method on MenuItem that operates on it&#8217;s collection and returns the updated MenuItem.  Hmmm, how to handle the submenu chaining?  perhaps returning an intermediate helper object to ensure that the return type would notify you if you had forgotten an End()</p>
]]></content:encoded>
	</item>
</channel>
</rss>
