Creating Custom Types

Extending a HealthVault data type might not always solve your data needs. Many times there are legitimate use cases for which the application needs a unique data repository. For example, in our Quantified Self application, we need a repository to store all of the user’s self-experiments.

HealthVault provides a mechanism called an application-specific type for this purpose. This type is not shareable with other applications. Once application developers find a broader use for their data, they can work with Microsoft to create a first-class data type for their needs.

Example 4-11 shows how one can use an application-specific type to store self-experiment hypotheses for the Quantified Self application. In our application we are asking a user to create a hypothesis using a simple text box. The value of this text box is read as the hypothesis string in Line 1. In Lines 34, we create an XML document with the data for this specific type and then add it to the document using ApplicationSpecificXml in Line 5. Each application-specific type requires a SubtypeTag and Description (Lines 67). We also specify the application creating this type in Line 2. Additionally, we use the common note element to capture the status of the type in Line 8, and the When element captures the date.

Example 4-11. Writing an application-specific custom type

    protected void Btn_Submit_Hypothesis_Click(object sender, System.EventArgs e)
    {
        ApplicationSpecific appSpecific = new ApplicationSpecific();
        string hypothesis = Txt_Hypothesis.Text; 1
        appSpecific.ApplicationId = this.ApplicationConnection.ApplicationId.ToString(); 2
        XmlDocument xml = new XmlDocument(); 3
        xml.LoadXml(
            string.Format("<self-experiment><hypothesis>{0}</hypothesis>
                </self-experiment>", 
            hypothesis)); 4
        appSpecific.ApplicationSpecificXml.Add(xml); 5
        appSpecific.SubtypeTag = "self-experiment"; 6
        appSpecific.Description = hypothesis; 7
        // Default the status note to active when the hypothesis is created
        appSpecific.CommonData.Note = "Active"; 8
        appSpecific.When = new HealthServiceDateTime(DateTime.Now);
        PersonInfo.SelectedRecord.NewItem(appSpecific); 
    }

On the other hand, we can show the list of self-experiments by reading the ApplicationSpecificXml using an XPath navigator. In Example 4-12, note that in Lines 12, we assume that the document for this type contains only one element and that the first node is the hypothesis.

Example 4-12. Reading an application-specific type

    private void DisplaySelfExperiments(List<ApplicationSpecific> selfExperiments)
    {
        DataTable selfExperiment = new DataTable("SelfExperiments");
        selfExperiment.Columns.Add(new DataColumn("Date"));
        selfExperiment.Columns.Add(new DataColumn("Hypothesis"));
        selfExperiment.Columns.Add(new DataColumn("Status"));
        foreach (ApplicationSpecific s in selfExperiments)
        {
            
            DataRow row = selfExperiment.NewRow();
            row["Date"] = s.EffectiveDate.ToShortDateString().ToString();
            row["Hypothesis"] = s.ApplicationSpecificXml[0].CreateNavigator(). 1
                                SelectSingleNode("hypothesis").Value; 2
            row["Status"] = s.CommonData.Note;
            selfExperiment.Rows.Add(row);
        }
        SelfExperimentsView.DataSource = selfExperiment;
        SelfExperimentsView.DataBind();
    }

Get Enabling Programmable Self with HealthVault now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.