More Related Content
Similar to FHIR API for .Net programmers by Mirjam Baltus (20)
FHIR API for .Net programmers by Mirjam Baltus
- 1. FHIR API
for .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.
- 2. Who am I?
Name: Mirjam Baltus-Bakker
Company: Furore, Amsterdam
Background:
ICT trainer
Furore FHIR team
Contact:
m.baltus@furore.com
© 2012 HL7 ® International. Licensed under Creative Commons. HL7 & Health Level Seven are registered trademarks of Health Level Seven International. Reg. U.S. TM Office.
- 3. Using the Reference Implementations
HL7.FHIR SUPPORT 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.
- 4. 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.
- 5. 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.
- 6. 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.
- 7. 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.
- 8. 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.
- 9. 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 = new
CodeableConcept("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.
- 10. 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 = new
FhirDateTime("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.
- 11. 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.
- 12. 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.
- 13. Parsing example 2
// Create a file-based reader for Xml
XmlReader xr = XmlReader.Create(
new StreamReader(@"publishobservation-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.
- 14. 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.
- 15. 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.
- 16. 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.
- 17. 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.
- 18. 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.
- 19. 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.
- 20. Search
var criteria = new string[] { "family=Eve" };
Bundle result = client.Search<Patient>(criteria);
while (result != null)
{
// Do something useful
result = 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.
- 21. 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.
- 22. 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.
- 23. 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.
- 24. 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.
- 25. Questions?
http://hl7.org/fhir m.baltus@furore.com
© 2012 HL7 ® International. Licensed under Creative Commons. HL7 & Health Level Seven are registered trademarks of Health Level Seven International. Reg. U.S. TM Office.