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 . In Lines –, we create an XML document with the data for this
specific type and then add it to the document using
Application
Specific
Xml
in Line
. Each application-specific type requires a
SubtypeTag
and Description
(Lines –). We also specify
the application creating this type in Line .
Additionally, we use the common note element to capture the status of the
type in Line , 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; appSpecific.ApplicationId = this.ApplicationConnection.ApplicationId.ToString(); XmlDocument xml = new XmlDocument(); xml.LoadXml( string.Format("<self-experiment><hypothesis>{0}</hypothesis> </self-experiment>", hypothesis)); appSpecific.ApplicationSpecificXml.Add(xml); appSpecific.SubtypeTag = "self-experiment"; appSpecific.Description = hypothesis; // Default the status note to active when the hypothesis is created appSpecific.CommonData.Note = "Active"; 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
–, 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(). SelectSingleNode("hypothesis").Value; 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.