Mark Needham

Thoughts on Software Development

Coding: Isolate the data not just the endpoint

with 6 comments

One of the fairly standard ways of shielding our applications when integrating with other systems is to create a wrapper around it so that all interaction with it is in one place.

As I mentioned in a previous post we have been using the repository pattern to achieve this in our code.

One service which we needed to integrate lately provided data for populating data on drop downs on our UI so the service provided two pieces of data – a Value (which needed to be sent to another service when a certain option was selected) and a Label (which was the value for us to display on the screen).

Our original approach was to pass both bits of the data through the system and we populated the dropdowns such that the value being passed back to the service would be the Value but the value shown to the user would be the Label.

The option part of the drop down list would therefore look like this:

<option value="Value">Label</option>

With the data flowing through our application like so:


Although this approach worked it made our code really complicated and we were actually passing Value around the code even though our application didn’t care about it at all, only the service did.

A neat re-design idea a couple of my colleagues came up with to was to only pass the Label through the application and then just do a mapping in the Repository from the Label -> Value so we could send the correct value to the service.

The code then became much simpler:


And we had isolated the bit of code that led to the complexity in the first place.

The lesson here for me is that it’s not enough merely to isolate the endpoint, we also need to think about which data our application actually needs and only pass through the data we actually use.

Be Sociable, Share!

Written by Mark Needham

March 25th, 2009 at 11:28 pm

Posted in Coding

Tagged with ,

  • sud

    What if it expensive to lookup the Value by the Label in order to return the (Value, Label) pair?

  • Pingback: DotNetShoutout()

  • Sorry probably didn’t explain that clearly enough – we get sent both the Value and the Label by the service and we keep a cache of that data so that it’s a relatively cheap operation to go and find which Value a Label maps to later on.

  • Pingback: Reflective Perspective - Chris Alcock » The Morning Brew #315()

  • Pingback: TDD: Testing mapping code at Mark Needham()

  • SirTylerGalt

    Did this work in your case because the Label was unique? If not, wouldn’t you need some kind of “id” to make sure your code doesn’t break when the third party service returns two identical labels for different values (due to some misconfiguration or something)? In that case, should you introduce your own “id” in the Repository, use it in your application, and then map to the third-party’s “Value” on the way back? Or should you just keep using the third-party’s value (and thus conform to the third-party’s bounded context)?

    PS: I’m reading your other DDD posts, which are also very interesting. Thanks!