Fluent interface refactoring example

Joey Beninghove has an excellent example in his blog post Refactoring towards a DRY fluent interface

I like the way he first shows the need for the refactor and the reasons why. I especially like the use of the anonymous delegates. I’ve been a big fan of anonymous delegates in my querying of generic collections with the List in C#. I typically use an inline anonymous delegate – his example shows both.

Notice the use of the injection from the IoC container (I’m fairly certain he is using Windsor).

The first time I really have been able to work with the fluent interfaces is with the ICriteria in NHibernate. The ability to chain together the queries offers a powerful and and easy to comprehend syntax.

The example given in the above link:

IList cats = sess.CreateCriteria(typeof(Cat))
    .Add( Expression.Like("Name", "Fritz%") )
    .Add( Expression.Or(
        Expression.Eq( "Age", 0 ),
    ) )

Joey’s article gives a good explanation and example of how to do this in your own solutions.

Go check out the article, it’s good to see these examples!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s