Exploring FHIR value set expansion in .Net

By | March 20, 2018

There’s a few ways to work with a FHIR terminology server in C#. My goal in these examples is to find a code (and return the preferred term) with the term “inr” from the SNOMED CT-AU refset 1072351000168102. For all these, I’ll be using the same endpoint:

const string Endpoint = "https://ontoserver.csiro.au/stu3-latest";
const string ValueSetURL= "http://snomed.info/sct?fhir_vs=refset/1072351000168102";

Since it’s a REST service, RestSharp seems like a reasonable start.

var client = new RestClient(Endpoint);
var request = new RestRequest("/ValueSet/$expand", Method.GET);
request.AddQueryParameter("url", ValueSetURL);
request.AddQueryParameter("filter", "inr");
var response = client.Execute(request).Content;

var result = JsonConvert.DeserializeObject<ValueSet>(response);
Console.WriteLine(result.expansion.contains.FirstOrDefault().display);

Simple enough. Although to deserialise the JSON response a custom ValueSet class needs to be created.


Alternatively, there’s the official FHIR.Net API. There’s a couple of way to achieve the same thing.

var client = new FhirClient(Endpoint);
var filter = new FhirString("inr");
var url = new FhirUri(ValueSetURL);
var result = client.ExpandValueSet(url, filter);
Console.WriteLine(result.Expansion.Contains.FirstOrDefault().Display);

This is much the same as the first example. But there is no need for creating custom classes. The client, this time, is a specific FhirClient. The ExpandValueSet() handles the REST call and returns an HL7.Fhir.Model.ValueSet object.

Alternatively, you can use TypeOperation<T>(). The example below does the same as those above, but this approach supports a range of other parameters being passed through with the call.

var client = new FhirClient(Endpoint);
var parameters = new Parameters
    {
        Parameter = new List<Parameters.ParameterComponent>
         {
          new Parameters.ParameterComponent
          {
            Name = "identifier",
            Value = new FhirUri(ValueSetURL)
          },
          new Parameters.ParameterComponent
          {
            Name = "filter",
            Value = new FhirString("inr")
          }
         }
    };

var result = (ValueSet)client.TypeOperation<ValueSet>("expand",parameters);
Console.WriteLine(result.Expansion.Contains.FirstOrDefault().Display);

I had originally tried deserialising my RestSharp responses to HL7.Fhir.model.ValueSet, but it’s not possible (at least in any simple way). Using the official FHIR API for .Net is certainly far easier, but it’s important to go all the way and use it’s built-in REST functionality too.

Leave a Reply