Setting Sail with the Smuggling Pattern :: Part 1

Maybe because I’m often entertained by the geekiest of things, I’ve started usage of the Smuggling Pattern named after a fellow software architect Matt Davis’s terminology of smuggling data into and out of Excel.  It is something that I’ve lately needed to do on a regular basis, get data into and out of Excel without Excel, or at least the evil COM Interop Model, really knowing what is going on.  In other words, I want total programmatic control over data so I can trust it versus Excel mangling my poor data up.  This is what the Smuggling Pattern is going to do for me, and it is doing it well.

Now you might think, “WTF!”  But just to clarify, the Smuggling Pattern is really the Builder Pattern that we architects know and love with a cooler more fun more pirate like name!  So with that, here’s some of that code, and the TDD process I undertook to get this sucker built to set sail.

First I created a C# Class Project and created the following file folder structure and files.

Once I completed that I jumped right into testing.  Of course with ReSharper it makes life so much friggin simpler, in addition to TDD making life simpler.  I do still admit, that if you’re not practiced at TDD, then doing tests in whatever order you are comfortable with still keeps you at peak performance.  But eventually you should work toward TDD and toss in a great tool like ReSharper.  The difference in speed and quality is almost like getting 2 of the 3 engineering axioms instead of just 1 (i.e. speed, quality, & cost).

The first tests I started on where the individual builder tests, or in my case the Smuggler Object.  I always create a check for null just to make sure that I named the file correctly, kind of a double check, and it gives me the attribute, category, and other parts that I can copy to the other tests easily.

using Production.Smugglers.Builders;
using NUnit.Framework;

namespace Production.Tests
{
    [TestFixture]
    public class SmugglerTests
    {
        [Test, Category("Builder Tests")]
        public void TestsSmugglerIsNotNull()
        {
            var smuggler = new Smuggler();
            Assert.IsNotNull(smuggler);
        }
    }
}

This test I ran and it proved that the smuggler does instantiate appropriately, albeit not doing a single useful thing yet.  Next test…

[Test, Category("Builder Tests")]
public void TestsSmugglerOrderCompletionResult()
{
    var smuggler = new Smuggler();
    smuggler.CompleteOrder();
    SmuggledClassForTest smuggled = smuggler.GetSmuggledSwag();
    Assert.AreEqual(smuggled.StuffTestValue, Resources.ValueForTestingSmugglerSmuggled);
}

Now this test, if you view the picture, didn’t color code so well.  ReSharper points out that the instantiated class smuggler doesn’t have a CompleteOrder or GetSmuggledSwag method.

image 2

Move the curser (and new character marker over the red method) and hit Alt+Enter (IntelliJ Keymapping) and select the Create method ‘Smuggler.CompleteOrder’.

Now we have no more red, and the class Smuggler now has the method CompleteOrder.  The same code that is displayed in the image is directly below it so it can be copied and pasted easily.  Also note, I went ahead and added the IBuilder interface to the class since it will need to meet the contractual obligations of the builder interface.

namespace Production.Smugglers.Builders
{
    public class Smuggler : IBuilder
    {
        public void CompleteOrder()
        {
            throw new NotImplementedException();
        }
    }
}

I then wrote the following additional test, below is the additional test and the ones I already completed in total.

[TestFixture]
public class SmugglerTests
{
    [Test, Category("Builder Tests")]
    public void TestsSmugglerIsNotNull()
    {
        var smuggler = new Smuggler();
        Assert.IsNotNull(smuggler);
    }

    [Test, Category("Builder Tests")]
    public void TestsSmugglerSmuggledIsNotNull()
    {
        Smuggler smuggler = new Smuggler();
        smuggler.CompleteOrder();
        SmuggledClassForTest smuggled = smuggler.GetSmuggledSwag();
        Assert.IsNotNull(smuggled);
    }

    [Test, Category("Builder Tests")]
    public void TestsSmugglerOrderCompletionResult()
    {
        Smuggler smuggler = new Smuggler();
        smuggler.CompleteOrder();
        SmuggledClassForTest smuggled = smuggler.GetSmuggledSwag();
        Assert.AreEqual(smuggled.StuffTestValue, Properties.Resources.ValueForTestingSmugglerSmuggled);
    }
}

After that I went ahead and fleshed out the following classes to get the smuggler tests to green light.  First I stuck a resource variable in for my “test variable value”.  One could do this another million ways but I figured this quick n’ easy.

This resource file is located in the Properties of the project.

Find the resources by clicking the tab…

Then finished the interface (for now of course).

namespace Production.Smugglers.Builders
{
    public interface IBuilder
    {
        void CompleteOrder();
        SmuggledClassForTest GetSmuggledSwag();
    }
}

Then finished the Smuggler Class (i.e. Builder).

namespace Production.Smugglers.Builders
{
    public class Smuggler : IBuilder
    {
        private readonly SmuggledClassForTest smuggledClassForTest = new SmuggledClassForTest();

        public void CompleteOrder()
        {
            smuggledClassForTest.StuffTestValue = Resources.ValueForTestingSmugglerSmuggled;
        }

        public SmuggledClassForTest GetSmuggledSwag()
        {
            return smuggledClassForTest;
        }
    }
}

Then the little SmuggledClassForTests class, which is basically the product of the builder.

namespace Production.Smugglers.Smuggled
{
    public class SmuggledClassForTest
    {
        public string StuffTestValue { get; set; }
    }
}

Note the namespaces, I make a point to follow the standard of maintaining folder and namespace integrity.  I promise keeping the integrity makes getting to and finding code way faster than getting those things out of sync.

I then ran the tests again…

…green lights!

kick it on DotNetKicks.com