# Tuesday, August 21, 2007

Here's a quick example of how to build a simple "Fluent Interface" in C#:

What if you've never heard of a Fluent Interface? Then I'd recommend reading this overview article.

Let's propose we have 2 classes for an e-commerce website: Order and OrderItem.

Let's also propose we want to mark the order as having free shipping, include an item in the order (with an ItemID of 15), and make sure the purchaser is not charged tax for this particular item (15).

This might typically be done with the following theoretical code:
order.FreeShipping = true;
OrderItem item = new OrderItem(15);
item.SuppressTax = true;
order.AddItem(item);

However, the intention of a Fluent Interface is to accomplish the same thing, but with a more "fluent" syntax, such as:

order
    .AddFreeShipping()
    .IncludeItem(15)
        .SuppressTax();

So, how is this accomplished in code?  Well, here is the code for both the Order and the OrderItem classes:
Order.cs
using System;
using System.Collections.Generic;

namespace TestFluentInterface
{

    public class Order
    {

        private List _orderItems = new List();
        private bool _freeShipping = false;
        
        public List OrderItems
        {
            get
            {
                return _orderItems;
            }
        }

        public bool FreeShipping
        {
            get
            {
                return _freeShipping;
            }
        }

        public OrderItem IncludeItem(int itemID)
        {
            OrderItem item = new OrderItem(itemID);
            _orderItems.Add(item);
            return item;
        }

        public Order AddFreeShipping()
        {
            _freeShipping = true;
            return this;
        }

    }
}

OrderItem.cs
using System;

namespace TestFluentInterface
{
    public class OrderItem
    {

        private bool _hasTax = false;
        private int _itemID;


        public OrderItem(int itemID)
        {
            _itemID = itemID;
        }

        public int ItemID
        {
            get
            {
                return _itemID;
            }
        }

        public bool HasTax
        {
            get
            {
                return _hasTax;
            }
        }

        public OrderItem SuppressTax()
        {
            _hasTax = false;
            return this;
        }

    }
}

And how do we prove that the code behaved as we intended?

By using a unit test, of course:
NUnit Test
using System;
using NUnit.Framework;
using TestFluentInterface;

namespace Test
{
    [TestFixture]
    public class Test
    {

        [Test]
        public void MainTest()
        {

            Order order = new Order();

            order
                .AddFreeShipping()
                .IncludeItem(15)
                .SuppressTax();

            Assert.That(order.OrderItems.Count == 1);
            Assert.That(order.OrderItems[0].HasTax == false);
            Assert.That(order.FreeShipping == true);

        }

    }
}

I hope this article has helped to give you a feel for an example code structure behind a fluent interface.

If you found this post helpful, please "Kick" it so others can find it too:

kick it on DotNetKicks.com