# NHapiTools
**Repository Path**: shenhxBase/NHapiTools
## Basic Information
- **Project Name**: NHapiTools
- **Description**: No description available
- **Primary Language**: Unknown
- **License**: MIT
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2024-03-10
- **Last Updated**: 2024-03-10
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
[](https://github.com/dib0/NHapiTools/actions/workflows/build-status.yml)
# NHapiTools
The NHapiTools are tools that will make using NHapi (the open source .Net HL7 implementation) easier. NHapi has a steep learning
curve and not everything works as easy as it should. NHapiTools aims to improve that without tampering with NHapi itselves.
**Key Benefits**
- Source generation that will generate extension methods on NHapi assemblies. (some generated assemblies are provided)
- Generic extention methods.
- Easy IO tools for reading multiple files, implementing MLLP and filtering Base64 data reducing memory use and increasing speed.
- Tools to generate ACK messages based on any message
- Tools to implement custom (standard) segments (which is not possible with NHapi)
- Default validation rules to be used by the NHapi parser
- Two sets of context implementation to easily add all or configurable validation rules.
## Requirements
NHapiTools currently targets version 4.5 of the .NET Framework
## Getting Started
The easiest way to get started using nHapi is to use the [NuGet package 'NHapiTools'](https://www.nuget.org/packages/NHapiTools/):
Using the package manager console withing visual studio, simply run the following command:
```
PM > Install-Package NHapiTools
```
## Introduction
On my blog I get a lot of questions on how to set up a complete .Net system for HL7 message integration. In other words: all over the world developers create integration components from scratch to add HL7 integration to their applications. After working for a while with NHapi, the most complete and free component to support HL7 with .Net, I started to miss functionality. To make my life easier (and hopefully the life of other developers, I created the NHapiTools.
In this document the functionality that NHapiTools adds to the NHapi component will be explained.
The sources and releases of NHapiTools can be found on Github:
https://github.com/dib0/NHapiTools
Also check out HL7Fuse:
https://github.com/dib0/HL7Fuse
And, of course, The NHapi Beginner’s Guide:
http://www.smashwords.com/books/view/344824
## The Structure of NHapiTools
For the most important part NHapiTools is a set of extension methods that extend NHapi. NHapi uses a code generator to generate the HL7 message structure into classes and then assemblies. NHapiTools uses the same mechanism to generate classes with extension methods based on the NHapi assemblies. The generated classes are provided within the source, so you don’t have to do this (of course, if you want to, you can).
The extension methods make it far easier to enumerate through the messages. For example: they will allow you to use the “foreach” statement, which the basic NHapi structure doesn’t allow.
Besides the extension methods and richer validations NHapiTools provide tools for commonly used patterns within HL7 and HL7 integration. For example support for MLLP, a TCP/IP MLLP client, a method of generation ACK messages based on any method and alternative streams that filter out attached Base64 encoded documents that makes parsing large message easier and (much) faster.
## Extension Methods
NHapi, probably the part you'll never use, is actually a code generator. Based on the HL7 structure database, that can be obtained through the local HL7 organization, it will generate the classes for the data types, segments and message structures. It also forces the Hl7 standard within these classes.
Annoying about in these NHapi HL7 structures are the way repeating segments or fields are handled. NHapi generates a method to get the number of repetitions with a name like Get{property name}RepetitionsUsed(). With the methods Get{property name}(int repetition) you can get to a specific repeating field. Note that the last one can also be used to add a new repetition if you are creating a new message. The annoying part is that you always have to write the same for-loop to go through the data. I would rather be using the foreach statement, which will give you more readable and maintainable code.
The extention methods in NHapiTools do just that. NHapiTools contains a code generator that generates the extention methods based on the NHapi assemblies. NHapiTools also contain the compiled version, so you don't have to run the generator. That means by using the namespace of the extention assemblies you can use the using statements. So you can turn this:
```
for (int i=0; i< oMessage.PATIENT_RESULTRepetitionsUsed; i++)
{
PID pid = oMessage.GetPATIENT_RESULT(i).PATIENT.PID;
for (int y=0; y < pid.AlternatePatientIDPIDRepetitionsUsed; y++)
{
CX alterateId = pid.GetAlternatePatientIDPID(y);
// do something with the alternate ID
}
}
```
Into this:
```
foreach (ORU_R01_PATIENT_RESULT patient in oMessage.GetAllPATIENT_RESULTRecords())
{
PID pid = patient.PATIENT.PID;
foreach (CX alternateId in pid.GetAlternatePatientIDPIDRecords())
{
// do something with the alternate ID
}
}
```
Besides methods to easily use the foreach statement NHapiTools also provide a clearer way to add repeating fields. Instead of the NHapi method to “get” the next (non existing) enumeration NHapiTools provides a simple add extention methods to perform this operation.
## Validation
The validation of HL7 messages is embedded in the classes of NHapi. That means that the messages are validated if they are conform the HL7 specification standard. The NHapi parser utilizes a context for parsing. If you choose to use your own context you can influence the parsing. For example, by adding validation rules.
The way these validation rules are implemented and the context you have to create aren't really complicated, but since the rules are version specific and you might have to develop complex rules or many rules, maintaining them can be a hassle. NHapiTools provides two different contexts and a set of generic validation rules.
### Automated Context
The automated context allows you to develop you own set of validation rules. You will have to implement the ISpecificEncodingRule, ISpecificMessageRule or the ISpecificPrimitiveRule interface. The automated context will search through every type within a namespace and applies all the rules it can find to the parser. This will save you a lot of time maintaining and building the context. It needs a namespace:
```
MessageFieldIntOnlyRule | Checks the content of a specific field in the message and allows only integer values. |
MessageFieldMandatoryRule | Check if a specific field in the message exists and if the field isn't empty. |
MessageRegExRule | Checks the content of a specific field against a regular expression. |
MessageSegmentMandatoryRule | Checks if a given segment exists. Which is otherwise quite hard to check. |