Thought Cloud TDD #4: Cloud factory

I’m building Thought Cloud as a Correspondence demo for a talk in a few weeks. I hope you’ll be able to attend.

We’ve started right in the meat of the application, where the user is adding thoughts to a cloud. But before they get there, they should be able manage their individual clouds. Let’s start a new test suite for those features.

[TestClass]
public class HomeViewModelTest
{
    private Community _community;
    private Identity _identity;
    private HomeViewModel _viewModel;

    [TestInitialize]
    public void Initialize()
    {
        _community = new Community(new MemoryStorageStrategy())
            .Register<CorrespondenceModel>();

        _identity = _community.AddFact(new Identity("mike"));
        _viewModel = new HomeViewModel(_identity);
    }

    [TestMethod]
    public void CanAddACloud()
    {
        _viewModel.AddCloud.Execute(null);
        Assert.AreEqual(1, _viewModel.Clouds);
    }
}

The first test is that we can add a cloud. Let’s start by defining a new cloud fact.

fact Cloud {
key:
    unique;
    Identity creator;
}

A cloud is just a unique fact created by a user. To create a new cloud, let’s add a method to the Identity partial class.

public partial class Identity
{
    ...

    public Cloud NewCloud()
    {
        return Community.AddFact(new Cloud(this));
    }
}

The unit test executes a command to create a new cloud. So we use MakeCommand to implement the ICommand interface.

public class HomeViewModel
{
    private readonly Identity _identity;

    public HomeViewModel(Identity identity)
    {
        _identity = identity;
    }

    public ICommand AddCloud
    {
        get
        {
            return MakeCommand
                .Do(() => _identity.NewCloud());
        }
    }
}

Now the view model needs to return a list of clouds. We add a query to Identity to get all of the clouds.

fact Identity {
    ...

query:
    ...
    Cloud* clouds {
        Cloud c : c.creator = this
    }
}

Finally, we can use linq to convert the collection of cloud facts into cloud view models.

public IEnumerable<CloudViewModel> Clouds
{
    get
    {
        return
            from c in _identity.Clouds
            select new CloudViewModel(_identity, null);
    }
}

That constructor is not making much sense anymore. It takes an identity and central thought, not a cloud. That gives us a clue about the next changes we need to make. We’ll refactor that next time.

Leave a Reply

You must be logged in to post a comment.