My current project integrates with the Microsoft Dynamics CRM system for managing customer records. We already spiked out authenticating and our next step was attempting to update a record via its “REST Endpoint“.
On retrieving a record, you follow the OData style for finding something of relevant. Updating a record is interesting as we only want to send the fields that need to change remotely. Looking at their sample code, you need to:
X-HTTP-Method to MERGEAccept to application/json and the Content-Type to application/json; charset=utf-8Our end point for a contact looked something like:
https://crm.thekua.com/xrmservices/2011/organizationdata.svc/ContactSet(guid'867dc3f2-909e-e111-9912-0050569c2d72')
.
Updating simple fields off a contact is easy. We post something like:
{
"EMailAddress1":"spike.jones4@gmail.com",
"MobilePhone":"33335"
}
We receive a HTTP 204 (No Content) on a success. Posting an invalid attribute (i.e. one that does not exist on the ContactSet such as “EMailAddreXXXs1″) results in a HTTP 400 (Bad request) and a nice description about what’s wrong. You will also get a HTTP 400 if you post the wrong datatype such as sending a string where they expect a number. If you pass invalid values (but of the correct datatype) for a field, your response is a HTTP 500 with a message like The value of 'gendercode' on record of type 'contact' is outside the valid range."
Updating Option Set Values
Updating simple datatypes is easy and obvious from a JSON point of view. You have a couple of complex datatypes, such as the standard GenderCode. When you query for a record, you get back something that looks like
{"GenderCode"=>{"__metadata"=>{"type"=>"Microsoft.Crm.Sdk.Data.Services.OptionSetValue"}, "Value"=>1}}
To update something like this, you need to send a nested JSON object. The example follows
{
"EMailAddress1":"spike.jones4@gmail.com",
"MobilePhone":"33335",
"GenderCode": {
"Value" : "1"
}
}
A HTTP 204 (No Content) response indicates a success. Viola!
Another beautiful rant from Uncle Bob: Here’s what an application should look like. The use cases should be the highest level and most visible architectural entities. The use cases are at the center. Always! Databases and frameworks are details! You don’t have to decide upon them up front. You can push them off until later, once you’ve got all the use cases and business rules figured out, written, and tested.
8th light must be something else.
In the comments on my post about generating random numbers to test a function David Turner suggested that this was exactly the use case for which QuickCheck was intended for so I’ve been learning a bit more about that this week.
I started with a simple property to check that the brute force (bf) and divide and conquer (dc) versions of the algorithm returned the same result, assuming that there were enough values in the list to have a closest pair:
prop_closest_pairs xs = length xs >= 2 ==> dcClosest xs == (fromJust $ bfClosest xs)
I could then run that as follows:
> import QuickCheck.Test > quickCheck(prop_closest_pairs :: [(Double, Double)] -> Property)
It failed pretty quickly because the bf and dc versions of the algorithm sometimes return the pairs in a different order.
e.g. bf may say the closest pair is (2.0, 0.0), (2.1, 1.1) while dc says it’s (2.1, 1.1), (2.0, 0.0) which will lead to the quick check property failing because those values aren’t equal:
> ((2.0, 0.0), (2.1, 1.1)) == ((2.1, 1.1), (2.0, 0.0)) False
The best way I could think of to get around this problem was to create a type to represent a pair of points and then write a custom equality operator.
I initially ended up with the following:
type Point a = (a, a) data Pair a = P (Point a) (Point a)
instance Eq (Pair a) where P a b == P c d = a == c && b == d || a == d && b == c
Which didn’t actually compile:
qc_test.hs:41:58:
No instance for (Eq a)
arising from a use of `=='
In the second argument of `(&&)', namely `b == c'
In the second argument of `(||)', namely `a == d && b == c'
In the expression: a == c && b == d || a == d && b == c
The problem is that while we’ve made Pair an instance of the Equality type class there’s no guarantee that the value contained inside it is an instance of the Equality type class which means we might not be able to compare its values.
We need to add a class constraint to make sure that the value inside P is a part of Eq:
instance (Eq a) => Eq (Pair a) where P a b == P c d = a == c && b == d || a == d && b == c
Now we’re saying that we want to make Pair an instance of the Equality type class but only when the value that Pair contains is already an instance of the Equality type class.
In this case we’re just storing pairs of doubles inside the Pair so it will work fine.
Now if we compare the two points from above we’ll see that they’re equal:
> P (2.0, 0.0) (2.1, 1.1) == P (2.1, 1.1) (2.0, 0.0) True
I had to go and change the existing code to make use of this new but it didn’t take more than 5-10 minutes to do that.
Today, I feel like a massive RANT.
Although I’d rather book myself in for a root canal than get involved in creating proposals or responding to RFPs, I do find myself in this position frequently these days and I’ve yet to develop a suitably sustainable perspective on these things to make them enjoyable. Hell, forget enjoyable as a goal – I’d settle for a beige sort of feeling rather than the darker colours I tend to find myself wallowing in during those times.
And here’s why – proposals suck. The act of building them… sucks. The process they are a part of… sucks.
Why? Well, let me give my completely objective and not-at-all bitter take on how this stuff works.
Step 1: Company A wants some stuff done. They decide to go to market for whatever reason. Their governance mechanisms demand some form of RFP process to ensure fairness and transparency, etc, etc (hint: it doesn’t and it isn’t, but that’s another story entirely)
Step 2: Company A taps the shoulder of some poor schmuck(s) to write the documentation describing the work to be done. This document is usually 40% boilerplate with the rest being the gristle of the problem to be solved. Generally the authors of these documents are domain experts so understand the problem domain quite well. However using domain experts to explain complex problems to people who don’t share that expertise also sucks because:
Step 3: Documentation is distributed to interested parties for response by a certain time. Some RFP processes include open Q&A sessions with all the potential respondees which inevitably turn into variations of pissing contents (i.e., who can ask the questions that intimidates the other parties in terms of specificity, detail, prior knowledge, etc) or exercises in studied banality (i.e., don’t ask anything publicly which might reveal how we’re thinking about the solution).
Step 4: Respondees digest documentation and subsequently devise solution, estimates, timelines, costs, staffing models, risks, etc., etc. Now this is where things get really interesting because I’m often part of the ”devise solution plus estimates” side of things and a whole new circle of Hell must be navigated to survive this phase. The natural tensions are:
Step 5: Respondees send response to Company A who now have the unfortunate task of reading, understanding, comparing and ranking n proposals. Sometimes there is a shortlist process and you get a second bite at the cherry and refine your response. Rarely is there any formal feedback process from Company A back to the respondees apart from the tersest of communications saying you have been successful or otherwise.
If you’re unsuccessful, I believe the required behaviour is to either justify the response by saying you didn’t want the work in the first place or rationalise the decision by saying that whoever won did so by lowballing the price with a strategy to change manage their way to profitability.
If you’re successful, then the once the project gets started, the vast majority of the assumptions you made to justify your estimates will be immediately or eventually invalidated rendering the basis of your estimates and costings null and void… this part doesn’t suck so much
Next proposal – BRING IT ON!

Last week I completely re-worked the short presentation I share with nonprofit clients who want to get others in their teams excited about what agile practices can do for them. 
If you’re a staff member or volunteer on a team, this stuff can be so very helpful. Check out the presentation: Agile – What is it and why should I care?. And feel free to ask questions. Or steal it!
ThoughtWorks embraces the individuality of the people in the organization and hence the opinions expressed in the blogs may contradict each other and also may not represent the opinions of ThoughtWorks.