This is part of my HL7 article series. Recently, a vistor to my blog requested that I do an article for HL7 programming using .NET. It was a timely request since I had been thinking about doing one for sometime as well. It inspired me to quickly do an introductory tutorial to HL7 using .NET and C# (see HL7 Programming using .NET - A Short Tutorial) where I explained all the basics one needs to understand about what goes into HL7 2.x programming if you are new to this area. However, a lot more is required to build a real-life HL7 system which includes support for many aspects of HL7 message processing such as message parsing, encoding, validation as well as message acknowledgment. In this article, we will dive deeper into building HL7-enabled .NET applications using a .NET HL7 framework called "NHAPI" (a port of the original Java-based HAPI HL7 framework which I covered in my previous tutorials) that helps us to easily build many HL7 processing capabilities within our custom .NET applications.
NHAPI and .NET - An Overview
.NET is a development platform similar to Java which enables applications developers to develop a wide range of applications such as console, desktop, web and mobile applications. It was first released in the early 2000s, and is one of the most popular development platforms today. It offers programmers a variety of programming languages choose from such as C#, F#, Visual Basic.NET, IronRuby, IronPython, etc, but the C# language has been the most popular choice for programmers using this platform. Although the .NET platform came later than Java, it has also taken an extremely interesting evolution in terms of things such as language support (particularly through powerful languages such as C#), a thriving community of passionate software developers who actively use as well as shape it everyday, and also through the wide range of commercial and open source tooling that exists for this platform as a result. These factors have helped create a rich eco-system around this platform. This in turn enables people using it to solve a wide range of information technology-related problems.
When C# and the .NET framework first came on the scene in early 2000, it was very common to emulate or port popular Java projects into C# and .NET, and many great as well as not so great projects emerged as a result. NHapi is an example of a great project that was a port of the HAPI framework for Java that we have looked at in a number of my previous articles. NHapi has been around in some shape or form for a number of years now. The authors of this library have taken a lot of care to not just emulate the Java implementation but instead use the strength and expressiveness of the C# language where possible to make it well suited to the Microsoft platform. However, many high-level constructs within NHapi still closely follow the original implementation on which it is based. So, people with some exposure to the original Java-based HAPI project should find it easy to follow or relate to if needing to implement a .NET HL7 solution as well. NHAPI does not have the same feature set as HAPI (no built-in support for sending and receiving messages for example), but we will look at ways to mitigate these challenges by augmenting NHAPI using some additional tools and libraries. Let us begin our exploration of the NHAPI framework by looking at how to create HL7 messages. I will follow the same format as I did in the Java/HAPI version of my tutorial so it is easy to follow for beginners.
“At high tide, fish eat ants; at low tide, ants eat fish.” ~ Thai Proverb
Tools and Resources Needed
- .NET Framework 4.5 or higher
- A .NET IDE or Editor such as Visual Studio IDE or Visual Studio Code (even a text editor should suffice)
- NHAPI GitHub page is located here
- Install NHAPI Nuget Package using NuGet Package Manager
- You can also find all the code demonstrated in this tutorial on GitHub here
Quick Introduction to ADT Messages
HL7 ADT messages are one of the most popular message types used in HL7-enabled systems and are used communicate core patient information captured in the EMR (Electronic Medical Record) to other ancillary/supporting systems such as lab, radiology, etc. This message is often transmitted when the patient information is either captured initially or when it changes because of events such as admission, transfer or discharge in a clinical setting. There are many types of ADT messages including ADT A01 (used during admit), A02 (used during transfer), A03 (used during end of visit or discharge), etc. There are also other HL7 message types that pertain to orders, results, clinical reports, scheduling and billing. The NHAPI library provides built-in support to create all these message types. In this particular tutorial, I will use the NHAPI library to create a ADT A01 message. This message type or trigger event that we will create will include the following message segments that are mandatory:
- MSH segment: Every HL7 message will carry this segment if you recall from my earlier tutorial. This segment will carry information such as the sender and receiver of the message, the type of message, and the date and time information of this message
- EVN segment: This segment will include information pertaining to the event that occurred (in our case this is due to an admission of a fictional patient)
- PID segment: This segment carries information pertaining to patient demographics such as such as name of the patient, patient identifier and address, etc.
- PV1 segment: This segment typically contains information about the patient’s stay, where they are assigned and also the referring doctor
Message construction gets pretty ugly if we don't separate the various steps involved into smaller methods. The approach I show below is a suggestion only. Please feel free to create your own factory methods, builder classes and any other utility classes needed especially if you are wanting to support multiple message types in your application.
There are many variations of the factory method pattern, and most of them tend to implement a static method which is responsible for constructing an object and returning this created object. It is the static method that tends to abstract the construction process of an object. You typically define an interface for creating an object in the superclass and you then let the subclasses decide which classes to instantiate depending on the parameters passed. However, you can also delegate the construction to another class entirely. So, lots of options and approaches are available to you. Ultimately, the factory method lets the main class (or the "client") defer any instantiation to other classes based on the parameters passed. Here, I am simply using a builder class inside the factory method.
“Violence is the last refuge of the incompetent.” ~ Isaac Asimov
In the code shown below and as described earlier the delegation of the actual construction of our ADT A01 message is left to a ADT message builder class called "OurAdtA01MessageBuilder" here for the lack of better imagination on my part. This builder class shown below helps construct the various parts of the ADT A01 message step by step. Using the builder pattern will be extremely useful even when using a HL7 library like NHAPI since there is just way too many optional segments and components that you will deal with when dealing with the HL7 2.x standard. The other advantage of this approach is that you can pass in data into the constructor of the builder class to build the ADT message with the required data values from your HL7 application (not shown here). This a style I prefer. However, feel free to choose any style that suits you or your organization. This is after all a tutorial on using NHAPI and not on software design. :-)
The results of running the above program are shown above. The two output files generated by the program should give you an idea of what a pipe delimited HL7 file and XML formatted file look like. Please note that the pipe delimited output file has special unprintable characters within it that help in the "message enveloping" that is required. You can inspect this file using any Hex editor if you are curious to see where these characters are in located the file.
That concludes this little tutorial illustrating how we can create an ADT HL7 message and its various constituent parts using the NHAPI library. NHAPI provides a large library of strongly typed classes which makes the task of generating various HL7 2.x messages very easy. The various classes and their methods and other utility functions provided by the library help remove any tedium and guess work when assembling, parsing and validating HL7 messages that we often need to tackle when developing a .NET-based message processing application. However, you should note that the NHAPI library does not have all the features compared to the HAPI Java library which served as the original inspiration for this library. These include functionality such as sending and receiving messages. Fortunately, there are several additional tools that work well with the NHAPI framework and help fill these gaps in functionality. We will employ these tools in conjunction with NHAPI to create more sophisticated HL7 messages which include custom segments (also known as "Z-Segments") in subsequent tutorials. However, in the next tutorial in my HL7 article series, I will cover how we can combine NHAPI with another tool called "NHapi Tools" to transmit a HL7 message to a HL7 receiving system. See you then!