FHIR Chat · Develop FHIR API with MVC Web API and Hl7.Fhir.STU3 package · dotnet

Stream: dotnet

Topic: Develop FHIR API with MVC Web API and Hl7.Fhir.STU3 package


view this post on Zulip hannah (Apr 11 2019 at 13:48):

Hi,

I am new to FHIR and I am trying to developing my own FHIR API (server side) with MVC Web API and Hl7.Fhir.STU3 package. I got some problem when posting FHIR object with JSON format from client (using Postman) to my API. The following is what I've done to create the API.

(1) created a ASP.NET MVC Web API project in visual studio
(2) installed Hl7.Fhir.STU3 package by typing "Install-Package Hl7.Fhir.STU3 -Version 1.2.0" in the package manager console
(3) created a ApiController called "PatientController.cs" and created a post method in it (see below code):
using Hl7.Fhir.Model;
using System.Web.Http;
namespace FHIR.API.Controllers
{
public class PatientController : ApiController
{
// POST api/values
public void Post(Hl7.Fhir.Model.Patient patientObj)
{
string dateOfBirth = patientObj.BirthDate;
}
}
}
(4) To test this API, in postman, send a http post request to my FHIR API with a sample "Patient" FHIR JSON object (The "Patient" FHIR object is copied from FHIR website)
(5) Debug the API PatientController "Post" Method, all properties in "patientObj" is null. It seems that the "Patient" FHIR object sent from Postman is not mapped into the API Hl7.Fhir.Model.Patient object.

Could you please advise if I am doing anything wrong when using Hl7.Fhir.STU3 package to develop my own FHIR API server? I am using vs 2013 and .net 4.5.

Sample Patient FHIR Object*
{
"resourceType": "Patient",
"id": "xcda",
"identifier": [
{
"use": "usual",
"type": {
"coding": [
{
"system": "http://terminology.hl7.org/CodeSystem/v2-0203",
"code": "MR"
}
]
},
"system": "urn:oid:2.16.840.1.113883.19.5",
"value": "12345"
}
],
"active": true,
"name": [
{
"family": "Levin",
"given": [
"Henry"
]
}
],
"gender": "male",
"birthDate": "1932-09-24",
"managingOrganization": {
"reference": "Organization/2.16.840.1.113883.19.5",
"display": "Good Health Clinic"
}
}
******************

Thanks a lot.

Hannah

view this post on Zulip Kenneth Myhra (Apr 11 2019 at 17:12):

There is not a 1-1 mapping between the POCO representation in the fhir-net-api and the representation in XML/JSON. You need to do some parsing in your request pipeline.

view this post on Zulip hannah (Apr 11 2019 at 19:09):

Ok I see. Thank you for the information, Kenneth.

Actually I see the source cod specifically marked the properties defined in FHIR spec to be not mapped, please see below code from "Hl7.Fhir.Model" in the package.
For example, the property "BirthDate" of Patient FHIR object is decorated with two attributes "IgnoreDataMemberAttribute" and "NotMapped", however, the property "BirthDateElement" is marked as DataMember to be serialized. Not sure why it's designed as this and if there is an easy way for us to turn it off or we have to customize the source code to remove those two attributes?

/// <summary>
/// The date of birth for the individual
/// </summary>
[FhirElement("birthDate", InSummary=true, Order=140)]
[DataMember]
public Hl7.Fhir.Model.Date BirthDateElement
{
get { return _BirthDateElement; }
set { _BirthDateElement = value; OnPropertyChanged("BirthDateElement"); }
}

    private Hl7.Fhir.Model.Date _BirthDateElement;

    /// &lt;summary&gt;
    /// The date of birth for the individual
    /// &lt;/summary&gt;
    /// &lt;remarks&gt;This uses the native .NET datatype, rather than the FHIR equivalent&lt;/remarks&gt;
    [NotMapped]
    [IgnoreDataMemberAttribute]
    public string BirthDate
    {
        get { return BirthDateElement != null ? BirthDateElement.Value : null; }
        set
        {
            if (value == null)
              BirthDateElement = null; 
            else
              BirthDateElement = new Hl7.Fhir.Model.Date(value);
            OnPropertyChanged("BirthDate");
        }
    }

view this post on Zulip Yunwei Wang (Apr 11 2019 at 21:55):

Add a media formatter and using XmlSerializer/JsonSerializer in the FHIR nuget package serialize the inbound xml/json steam to FHIR instance. The same for outbound messages.

view this post on Zulip Yunwei Wang (Apr 11 2019 at 22:03):

Because FHIR resource classes are generated from FHIR xml schema. All data types in FHIR are inherited from Element type. FHIR .NET package wraps around Element type to simplify developer's work. In your example, BirthDate in FHIR xml schema is Date type which inherited from Element. So the genereated class has public Date BirthDateElement property. Most c# developers use either string or .NET DateTime type for date/dateTime. To simplify that, the code generator also creates public string BrithDate wrapping around BirthDateElement property. But when serialized to xml/json stream, only the BirthDateElement is serialized/deserialized since the Date property is what specified in the standard, not the string property.

view this post on Zulip Brian Postlethwaite (Apr 12 2019 at 08:01):

There is a project on github that provides an implementation of the facade you can use.
https://github.com/brianpos/fhir-net-web-api
Theres an example project in there to show how to use it.
Comes in either full net framework or aspnet core.

view this post on Zulip Brian Postlethwaite (Apr 12 2019 at 08:03):

That handles all the controller and formatting stuff for you, and you implement the storage logic.

view this post on Zulip Brian Postlethwaite (Apr 12 2019 at 08:03):

(all the stuff that Yunwei mentions)

view this post on Zulip Brian Postlethwaite (Apr 12 2019 at 08:07):

I'm planning to push it as a NuGet package, but not there just yet.

view this post on Zulip hannah (Apr 12 2019 at 15:54):

Hi Brian and Yunwei,

Thanks a lot for the explanation, I will try that. much appreciated.


Last updated: Apr 12 2022 at 19:14 UTC