Adding An Id!

Ready to start again with Red?

Looking back at our requirements next I see that the basket should have a unique id, at the moment I have no clue about where I am going to store the baskets so there is nothing to help me generate ids, and I don’t want an incremental number appearing in the URI of our API calls as this could be a security risk.  To this end I think I will use a GUID, with the “-“ removed will make a nice unique id for the basket.

First up lets write the test, we already have a fixture calling our create method so we can simply add a new test method that checks for the existence of the id.  Starting again the same as before creating a test method.

        [Test]
public void The_returned_basket_has_an_id()
{

}

then getting the baskets id property

        [Test]
public void The_returned_basket_has_an_id()
{
_basket.Id
}

Which of cause does not exist on the basket, so we need to create it

    public class Basket
{
private IList<object> _items;

public Basket()
{
_items = new List<object>();
}

public bool IsEmpty { get { return ! _items.Any(); } }
public string Id { get; set; }
}

and now simply to assert that the id is not empty.

        [Test]
public void The_returned_basket_has_an_id()
{
_basket.Id.ShouldNotBeEmpty();
}
 
This test runs red because we are never setting the id.
 
The next phase.  Getting to Green as quickly as possible.
 
We want to set the id to a new GUID without the “-“ between each section of the string which we can achieve in the handler.
 
    public class BasketHander : NancyModule
{
public BasketHander()
{
Get["/basket"] = _ =>
{
return Response.AsJson(new Basket(){Id = Guid.NewGuid().ToString().Replace("-","")});
};
}
}
And our code goes green again. Its time to take a breath and review our code.
 
The Refactoring phase. 
 
Do we want to be doing all that id generation within the handler? Probably not, first lets use the introduce variable refactoring so we have a basket variable that is filled with a created basket.
 
    public class BasketHander : NancyModule
{
public BasketHander()
{
Get["/basket"] = _ =>
{
var basket = new Basket(){Id = Guid.NewGuid().ToString().Replace("-","")};

return Response.AsJson(basket);
};
}
}

This will allow us to move the id creation code to a method on the basket, call it from the handler and remain green.

    public class BasketHander : NancyModule
{
public BasketHander()
{
Get["/basket"] = _ =>
{
var basket = new Basket();
basket.Id = basket.CreateBasketId();

return Response.AsJson(basket);
};
}
}

This is better, we have moved the id generation to the basket, but its still not great.
To create a basket we now need to not only call new, but we also have to remember to set the id, what would be nice is to be able to call something like  “Basket.CreateWithId()” and be returned a new basket with the id set. 
 
This is a factory method, so lets implement that and see how this effects our code.
 
    public class Basket
{
private IList<object> _items;

private Basket()
{
_items = new List<object>();
}

public bool IsEmpty { get { return ! _items.Any(); } }
public string Id { get; set; }

public static Basket CreateWithId()
{
return new Basket { Id = GenerateBasketId() };
}

private static string GenerateBasketId()
{
return Guid.NewGuid().ToString().Replace("-", "");
}
}
 
Quite a bit has gone on here,
We have a new static method called CreateWithId this returns a basket, and also calls the GenerateBasketId method  to set the id, because this method is now only used within the class I have made it private so we cannot generate ids outside of the basket class.  I also made the constructor of the basket private so the only way now to create baskets is through the factory method, which means we now have encapsulated the idea of basket id generation within the basket and all baskets created will have unique ids.
 
Back to the handler which can call this new factory method to get the basket.
 
    public class BasketHander : NancyModule
{
public BasketHander()
{
Get["/basket"] = _ =>
{
var basket = Basket.CreateWithId();

return Response.AsJson(basket);
};
}
}

Our handler is back to looking clean and simple, and the basket code looks ok, I am not sure about the chaining of the “Guid.NewGuid().ToString().Replace()” there is an awful lot of dots in there, and I usually like to keep them to a minimum to avoid the whole train wreck problem, but in this case its within a method, and I am not sure splitting it out would give much benefit at the moment.
 
So I think we are done with basket creation for the minute.
 
Looking at the requirements, the next task has to be getting a basket with an id, this implies we have baskets stored somewhere so at some point soon we are going to have to deal with storage.  We will also have to cover not finding the basket, and how we are going to return errors, lots to think about and decisions to be made.
 
All the code for this post is in the branch create basket in the git hub repository
Advertisements

About Duncan Butler

Trying to be a very agile software developer, working in C# with Specflow, Nunit and Machine Specifications, and in the evening having fun with Ruby and Rails
This entry was posted in Uncategorized. Bookmark the permalink.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s