无法访问FHIR模型

时间:2015-12-16 00:50:56

标签: c# json asp.net-mvc hl7-fhir dstu2-fhir

我正在使用带有DSTU2的Fhir-net-api将JSON对象解析为C#模型。除了我无法访问资源类型过程 Reason 元素外,一切都运行良好。作为示例,我使用 FhirParser 将以下JSON对象解析为Procedure模型:

{
"resourceType": "Procedure",
"identifier": [
    {
        "system": "https://mrd2.melanoma.org.au/fhir",
        "value": "100200199664802"
    }
],
"subject": { "reference": "Patient/10101000001733" },
"status": "completed",
"category": {
    "coding": [
        {
            "system": "https://mrd2.melanoma.org.au/fhir/RootType",
            "code": "3004"
        }
    ],
    "text": "Primary Surgery"
},
"bodySite": [
    {
        "coding": [
            {
                "system": "http://snomed.info/sct",
                "code": "7771000"
            }
        ],
        "text": "Left Forearm, Anterior"
    }
],
"reasonReference": { "reference": "/Condition/10106000001807" },
"performedDateTime": "1968-03-11",
"report": [ { "reference": "/DiagnosticReport/100200199664828" } ]
}

并且生成的对象具有以下条目(摘录): Procedure

我可以访问Report[0].Reference,但它不适用于Reason.Reference。我的JSON对象中的数据是错误的吗? 我已经看到 Reason 类型为 Hl7.Fhir.Model.Element 报告,类型为 Hl7.Fhir.Model。 ResourceReference 。有没有办法将 Reason 更改为 Hl7.Fhir.Model.ResourceReference ,然后访问 Reference 元素?

对任何提示都会感激不尽。感谢。

此致

Trammy

1 个答案:

答案 0 :(得分:2)

如您所见,reasonReference的类型为Model.Element,而report的类型为ResourceReference。这种差异源于FHIR specification for Procedure中这些元素的定义,其中report固定为Reference类型,但reason(或更确切地说reason[x])可以是CodeableConceptReference

当元素可以是多种类型时(我们将其称为“选择元素”,并且您可以识别它们,因为它们的名称在规范中以[x]结尾),我们创建了一个类型为C#的成员Model.Element(Reference和CodeableConcept的基类)。

现在,根据刚解析或收到的实例,reason成员的内容可以是两种类型之一。所以,你必须检查你的代码:

if(Reports[0].reason is ResourceReference)
{
    var reference = (ResourceReference)Reports[0].reason;
    //handle the case where this is a reference
    //doing reference.Reference will now work as expected
}
else if(Reports[0].reason is CodeableConcept)
{
    var concept = (CodeableConcept)Reports[0].reason;
    //handle the case where this is a codeable concept
}
else
{
    // Oops! Should not happen unless the standard has changed
}

当然,如果您确定只能接收ReasonReference的实例,您可以直接进行投射:

var myReference = (ResourceReference)Reports[0].Reference;
// myReference.Reference and myReference.Display will now work