Extending HealthVault Data TypesWhen building your application, you may find that you need to store additional information with an existing data type. For example, you are using the Height data type, but you want to keep track of whether people were sleeping immediately before height was measured because the spine compresses during the day. The question is whether to:
To determine the appropriate course of action, inquire in the HealthVault Forum. If the response to your inquiry is that the additional information should be stored as an extension of an existing type, you use HealthRecordItem.CommonData.Extensions, which is a collection of HealthRecordItemExtension instances. By default, there's nothing in this list. If you have an instance and you add an extension instance to it, the framework saves that instance to the server and returns it when you read that instance back out. HealthVault data types can be extended by inserting XML into the HealthRecordItemExtension or by using a custom extension class. In the first method, you work with the XML details, which can be a bit clumsy. In the second method, you encapsulate the information in a class. Using HealthRecordItemExtensionNOTE: Remember that this is a LIST of extensions, so you need to write your code so that it works with extensions that may have been put there by other users. Saving data to HealthVaultHere's the code to add the extension data: The first line defines the source string. This is the "unique identifier" that differentiates your data from all the other data, so it's a good place to put a company name or some other unique string. Next, create an instance of HealthRecordItemExtension with that name, and finally add some XML to the extension. Note that the string in the extension source attribute has to be identical to the source you specified when you created the HealthRecordItemExtension instance. When, for example, the height instance is saved, that data is persisted. Pulling the data back outTo get the data out, you need to find your extension among the other extensions. Assuming, for example, that you have a Height instance, you can write the following: And note that the source string you’re looking for is the same one you specified when you added the extension. There are a few other properties that you can set on the extension: Transform Logo Version Using a Custom Extension ClassThere are two constructors. The empty one is used by the system when it is creating an instance for you. The other one takes in the value that you want to store and then sets the sources appropriately. In ParseXml(), find the appropriate node, pull out the value, and store it in your own variable. In WriteXml(), add in the XML for your spine state value. Note that the parse and write methods are not symmetrical. In the write method, you are already inside the <extension> tag, while in the parse one, you are up one level, pointing at the <extension> tag. Finally, the RegisterExtensionHandler() method registers the type with the system so it knows where to find it. That method needs to be called someplace before you try to use the type. Using the custom extensionTo use the extension is very simple. To add an extension, use this code: And to get it back, here's what you write: |