Thought Cloud TDD #5: Backward thinking

The Thought Cloud demo is coming along nicely. Some really interesting patters are starting to emerge.

The last test produced some code that offends me. Let’s look at it again.

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

We create one cloud view model for each cloud that the user owns. But takes an identity and a central thought (which, to make matters worse, we set to null). It should take a cloud! Let’s change the constructor.

public CloudViewModel(Cloud cloud)
{
    _cloud = cloud;
    _identity = cloud.Creator;
}

Now we pass in a cloud, and the view model gets the identity from there. But where does it get the central thought? Let’s add that to the cloud as well.

fact Cloud {
key:
    unique;
    Identity creator;
    Thought centralThought;
}

So the central thought is an immutable feature of the cloud. As the compiler points out, we need to construct this thought before we construct the cloud.

public partial class Identity
{
    ...

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

public class HomeViewModel
{
    ...

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

Create a thought before you create a cloud? Seems backwards. Smells like another refactoring.

Leave a Reply

You must be logged in to post a comment.