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

</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><FONT size=2> strSqlString = sql.ToString();

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

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

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

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

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

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

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

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

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

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

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

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

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

Conclusion

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