Patterns: Fluent Interface - make your code easier to read

I never heard of the Fluent Interface pattern until I saw Randy Patterson’s blog post titled “How to design a Fluent Interface. When I first saw the title of the post I thought he was talking about design a user interface, but upon further inspection I found out that using the Fluent Interface pattern in your code will make it easier to read.

Randy posts an example of how to use the Fluent Interface pattern, but I thought I would also write up a short example. My example below shows how to implement a small class to generate Sql scripts using the Fluent Interface pattern.

Example Usage:<FONT color=#2b91af size=2>

SQLQuery</FONT> sql = <FONT color=#0000ff size=2>new</FONT> <FONT color=#2b91af size=2>SQLQuery</FONT>();
sql.Select(
<FONT color=#a31515 size=2>”Id”</FONT>).Select(<FONT color=#a31515 size=2>”FirstName”</FONT>).Select(<FONT color=#a31515 size=2>”LastName”</FONT>).From(<FONT color=#a31515 size=2>”Person”</FONT>).Where(<FONT color=#a31515 size=2>”Id = 1”</FONT>).Where(<FONT color=#a31515 size=2>”FirstName = ‘Chris’”</FONT>).OrderBy(<FONT color=#a31515 size=2>”LastName”</FONT>).OrderBy(<FONT color=#a31515 size=2>”FirstName”</FONT>);

<FONT color=#808080 size=2>///</FONT><FONT color=#008000 size=2> Get the string representation of our Sql query
</FONT><FONT color=#0000ff size=2>string</FONT> strSqlString = sql.ToString();

<FONT color=#2b91af size=2>Console</FONT>.WriteLine(strSqlString);

Code for the SQLQuery class:<FONT color=#0000ff size=2>

public</FONT> <FONT color=#0000ff size=2>class</FONT> <FONT color=#2b91af size=2>SQLQuery
</FONT>{
<FONT color=#0000ff size=2>private</FONT> <FONT color=#2b91af size=2>ArrayList</FONT> _SelectItems = <FONT color=#0000ff size=2>new</FONT> <FONT color=#2b91af size=2>ArrayList</FONT>();
<FONT color=#0000ff size=2>private</FONT> <FONT color=#2b91af size=2>ArrayList</FONT> _WhereItems = <FONT color=#0000ff size=2>new</FONT> <FONT color=#2b91af size=2>ArrayList</FONT>();
<FONT color=#0000ff size=2>private</FONT> <FONT color=#2b91af size=2>ArrayList</FONT> _OrderByItems = <FONT color=#0000ff size=2>new</FONT> <FONT color=#2b91af size=2>ArrayList</FONT>();
<FONT color=#0000ff size=2>private</FONT> <FONT color=#0000ff size=2>string</FONT> _FromTable = <FONT color=#0000ff size=2>null</FONT>;

<FONT color=#0000ff size=2>public</FONT> <FONT color=#2b91af size=2>SQLQuery</FONT> Select(<FONT color=#0000ff size=2>string</FONT> select)
{
_SelectItems.Add(select);
<FONT color=#0000ff size=2>return</FONT> <FONT color=#0000ff size=2>this</FONT>;
}

<FONT color=#0000ff size=2>public</FONT> <FONT color=#2b91af size=2>SQLQuery</FONT> From(<FONT color=#0000ff size=2>string</FONT> from)
{
_FromTable = from;
<FONT color=#0000ff size=2>return</FONT> <FONT color=#0000ff size=2>this</FONT>;
}

<FONT color=#0000ff size=2>public</FONT> <FONT color=#2b91af size=2>SQLQuery</FONT> Where(<FONT color=#0000ff size=2>string</FONT> where)
{
_WhereItems.Add(where);
<FONT color=#0000ff size=2>return</FONT> <FONT color=#0000ff size=2>this</FONT>;
}

<FONT color=#0000ff size=2>public</FONT> <FONT color=#2b91af size=2>SQLQuery</FONT> OrderBy(<FONT color=#0000ff size=2>string</FONT> orderby)
{
_OrderByItems.Add(orderby);
<FONT color=#0000ff size=2>return</FONT> <FONT color=#0000ff size=2>this</FONT>;
}

<FONT color=#0000ff size=2>public</FONT> <FONT color=#0000ff size=2>override</FONT> <FONT color=#0000ff size=2>string</FONT> ToString()
{
<FONT color=#2b91af size=2>StringBuilder</FONT> sb = <FONT color=#0000ff size=2>new</FONT> <FONT color=#2b91af size=2>StringBuilder</FONT>();

sb.Append(
<FONT color=#a31515 size=2>”SELECT “</FONT>);
<FONT color=#0000ff size=2>for</FONT>(<FONT color=#0000ff size=2>int</FONT> i = 0; i < _SelectItems.Count; i++)
sb.Append(_SelectItems[i] + ((i == _SelectItems.Count - 1) ?
<FONT color=#a31515 size=2>” “</FONT> : <FONT color=#a31515 size=2>”, “</FONT>));

sb.Append(
<FONT color=#a31515 size=2>”FROM “</FONT>);
sb.Append(_FromTable);

sb.Append(
<FONT color=#a31515 size=2>” WHERE “</FONT>);
<FONT color=#0000ff size=2>for</FONT>(<FONT color=#0000ff size=2>int</FONT> i = 0; i < _WhereItems.Count; i++)
sb.Append(_WhereItems[i] + ((i == _WhereItems.Count - 1) ?
<FONT color=#a31515 size=2>” “</FONT> : <FONT color=#a31515 size=2>” AND “</FONT>));

sb.Append(
<FONT color=#a31515 size=2>”ORDER BY “</FONT>);
<FONT color=#0000ff size=2>for</FONT>(<FONT color=#0000ff size=2>int</FONT> i = 0; i < _OrderByItems.Count; i++)
sb.Append(_OrderByItems[i] + ((i == _OrderByItems.Count - 1) ?
<FONT color=#a31515 size=2>” “</FONT> : <FONT color=#a31515 size=2>”, “</FONT>));

<FONT color=#0000ff size=2>return</FONT> sb.ToString();
}
}

Conclusion

As you can see the Fluent Interface pattern is rather simple to implement and it can really make your code easier to read.** **