© 2012 HL7 ® International. Licensed under Creative Commons. HL7 & Health Level Seven are registered trademarks of Health Level Seven International. Reg. U.S. TM Office.
FHIR APIfor .Net programmers
Mirjam Baltus
FHIR Developer Days
November 24, 2014
© 2012 HL7 ® International. Licensed under Creative Commons. HL7 & Health Level Seven are registered trademarks of Health Level Seven International. Reg. U.S. TM Office.
Who am I?
Name: Mirjam Baltus-Bakker
Company: Furore, Amsterdam
Background:
ICT trainer
Furore FHIR team
Contact:
© 2012 HL7 ® International. Licensed under Creative Commons. HL7 & Health Level Seven are registered trademarks of Health Level Seven International. Reg. U.S. TM Office.
HL7.FHIR SUPPORT APIUsing the Reference Implementations
© 2012 HL7 ® International. Licensed under Creative Commons. HL7 & Health Level Seven are registered trademarks of Health Level Seven International. Reg. U.S. TM Office.
Hl7.Fhir
- Core contents
- Model – classes generated from the spec
- Parsers and Serializers
- REST functionality – FhirClient
- Validation
- Specification
NuGet “FHIR”, or GitHub “fhir-net-api”
© 2012 HL7 ® International. Licensed under Creative Commons. HL7 & Health Level Seven are registered trademarks of Health Level Seven International. Reg. U.S. TM Office.
A FHIR Resource
© 2012 HL7 ® International. Licensed under Creative Commons. HL7 & Health Level Seven are registered trademarks of Health Level Seven International. Reg. U.S. TM Office.
A FHIR Resource in C#
public partial class Observation : Hl7.Fhir.Model.Resource
{
// Codes that provides reliability information about an observation
public enum ObservationReliability {Ok, Ongoing, …}
// Codes identifying interpretations of observations
public enum ObservationInterpretation {N, A, L, H }
public partial class ObservationReferenceRangeComponent : Hl7.Fhir.Model.Element
{ … }
public CodeableConcept Name { get; set; }
public List<ObservationReferenceRangeComponent> ReferenceRange { get; set;}
public List<ResourceReference> Performer { get; set; }
}
© 2012 HL7 ® International. Licensed under Creative Commons. HL7 & Health Level Seven are registered trademarks of Health Level Seven International. Reg. U.S. TM Office.
Primitives are not really primitive…
© 2012 HL7 ® International. Licensed under Creative Commons. HL7 & Health Level Seven are registered trademarks of Health Level Seven International. Reg. U.S. TM Office.
Primitives are not really primitive…
© 2012 HL7 ® International. Licensed under Creative Commons. HL7 & Health Level Seven are registered trademarks of Health Level Seven International. Reg. U.S. TM Office.
Resource example
using Hl7.Fhir.Model;
var pat = new Patient();
pat.Name = new List<HumanName>();pat.Name.Add(HumanName.ForFamily("Baltus")
.WithGiven("Mirjam"));
pat.MaritalStatus = newCodeableConcept("http://acme.org/MStat", "M");
pat.BirthDate = "1974-01-20";
© 2012 HL7 ® International. Licensed under Creative Commons. HL7 & Health Level Seven are registered trademarks of Health Level Seven International. Reg. U.S. TM Office.
Choice elements [x]
public
Hl7.Fhir.Model.CodeableConcept
MaritalStatus { set; get; }
public Hl7.Fhir.Model.Element
Deceased { set; get; }
pat.Deceased =new FhirBoolean(true);
pat.Deceased = newFhirDateTime("2012-01-30");
© 2012 HL7 ® International. Licensed under Creative Commons. HL7 & Health Level Seven are registered trademarks of Health Level Seven International. Reg. U.S. TM Office.
Hl7.Fhir Core
Model – Resources and Datatypes in the API
Parsers and Serializers
REST functionality – FhirClient
Validation
© 2012 HL7 ® International. Licensed under Creative Commons. HL7 & Health Level Seven are registered trademarks of Health Level Seven International. Reg. U.S. TM Office.
Parsing example
using Hl7.Fhir.Serialization;
// Suppose we have a JSON string
string str = "{\"resourceType\":\"Patient\",\"name\":" +
"[{\"family\":[\"Baltus\"],\"given\":[\"Mirjam\"]}]}";
// Parse the Patient from the string
var pat = (Patient)FhirParser.ParseResourceFromJson(str);
© 2012 HL7 ® International. Licensed under Creative Commons. HL7 & Health Level Seven are registered trademarks of Health Level Seven International. Reg. U.S. TM Office.
Parsing example 2
// Create a file-based reader for Xml
XmlReader xr = XmlReader.Create(
new StreamReader(@"publish\observation-example.xml"));
// Parse the Observation from the stream
var obs = (Observation)FhirParser.ParseResource(xr);
© 2012 HL7 ® International. Licensed under Creative Commons. HL7 & Health Level Seven are registered trademarks of Health Level Seven International. Reg. U.S. TM Office.
Serializing example
// Here’s our Observation again
var obs = (Observation)FhirParser.ParseResource(xr);
// Modify some fields of the observation
obs.Status = Observation.ObservationStatus.Amended;
obs.Value = new Quantity() { Value = 40, Units = "g" };
// Serialize the in-memory observation to Json
var jsonText = FhirSerializer.SerializeResourceToJson(obs);
© 2012 HL7 ® International. Licensed under Creative Commons. HL7 & Health Level Seven are registered trademarks of Health Level Seven International. Reg. U.S. TM Office.
Hl7.Fhir Core
Model – Resources and Datatypes in the API
Parsers and Serializers
REST functionality – FhirClient
Validation
© 2012 HL7 ® International. Licensed under Creative Commons. HL7 & Health Level Seven are registered trademarks of Health Level Seven International. Reg. U.S. TM Office.
REST functionality
REST operations done with FhirClient
using Hl7.Fhir.Rest;
var client = new FhirClient( new Uri("http://acme.org/fhir"));
Operations throw a FhirOperationException
Outcome property with OperationOutcome
© 2012 HL7 ® International. Licensed under Creative Commons. HL7 & Health Level Seven are registered trademarks of Health Level Seven International. Reg. U.S. TM Office.
Using the FhirClient
var patEntry = client.Read<Patient>("Patient/1");
var pat = patEntry.Resource;var restId = patEntry.Id;var tags = patEntry.Tags;
pat.Name.Add(HumanName.ForFamily("Kramer").WithGiven("Ewout"));
client.Update<Patient>(patEntry);
var myId = ResourceIdentity.Build("Patient","31");
© 2012 HL7 ® International. Licensed under Creative Commons. HL7 & Health Level Seven are registered trademarks of Health Level Seven International. Reg. U.S. TM Office.
Using the FhirClient
var pat = new Patient() { /* set up data */ };
var patEntry = client.Create(pat);var restId = patEntry.Id;
var newEntry = client.Refresh(patEntry);
client.Delete(patEntry);var location =
new Uri("http://acme.org/fhir/Patient/34");client.Delete(location);
© 2012 HL7 ® International. Licensed under Creative Commons. HL7 & Health Level Seven are registered trademarks of Health Level Seven International. Reg. U.S. TM Office.
Bundles in C#
Abstraction on top of Atom parser
Bundle = feed, BundleEntry = entry.
Bundle result = new Bundle() { Title = "Demo bundle" };
result.Entries.Add(new ResourceEntry<Patient>()
{ LastUpdated=DateTimeOffset.Now, Resource = new Patient() });
result.Entries.Add(new DeletedEntry()
{ Id = new Uri("http://..."), When = DateTime.Now });
var bundleXml = FhirSerializer.SerializeBundleToXml(result);
© 2012 HL7 ® International. Licensed under Creative Commons. HL7 & Health Level Seven are registered trademarks of Health Level Seven International. Reg. U.S. TM Office.
Search
var criteria = new string[] { "family=Eve" };Bundle result = client.Search<Patient>(criteria);
while (result != null){
// Do something usefulresult = client.Continue(results);
}
© 2012 HL7 ® International. Licensed under Creative Commons. HL7 & Health Level Seven are registered trademarks of Health Level Seven International. Reg. U.S. TM Office.
More about search
Please attend one of the presentations in the
next quarter:
Search for Server developers
by Martijn Harthoorn
Search for Client developers
by myself
© 2012 HL7 ® International. Licensed under Creative Commons. HL7 & Health Level Seven are registered trademarks of Health Level Seven International. Reg. U.S. TM Office.
Validation
DotNetAttributeValidation validates:
Cardinality of an element
Regex patterns of the primitives code, date,
dateTime, id, instant, oid, uri, uuid
Allowed types for a choice element (value[x])
pat.Deceased = new FhirString("Died on 2012-01-30");
Does not validate:
Conformance to ValueSet bindings
Profile conformance
© 2012 HL7 ® International. Licensed under Creative Commons. HL7 & Health Level Seven are registered trademarks of Health Level Seven International. Reg. U.S. TM Office.
Validation example
using Hl7.Fhir.Validation;
var pat = new Patient() { /* set up data */ };
// Will throw a ValidationException when an error is encountered
DotNetAttributeValidation.Validate(pat);
// Alternatively, use the TryXXXX pattern
var errors = new List<ValidationResult>();
var success = DotNetAttributeValidation.TryValidate(pat, errors);
if(!success) { /* handle errors */ }
© 2012 HL7 ® International. Licensed under Creative Commons. HL7 & Health Level Seven are registered trademarks of Health Level Seven International. Reg. U.S. TM Office.
Hl7.Fhir Core
Model – Resources and Datatypes in the API
Parsers and Serializers
REST functionality – FhirClient
Validation
© 2012 HL7 ® International. Licensed under Creative Commons. HL7 & Health Level Seven are registered trademarks of Health Level Seven International. Reg. U.S. TM Office.
Questions?
http://hl7.org/fhir [email protected]