Migrating Code to IMM 2.0
Many areas of the IMM SDK have been vastly improved in version 2. The most common operations have been streamlined and simplified.
The sample below demonstrates some differences in using the new IMM SDK, as well as highlights the improvements we have made in this release. The example scenario is a method that returns the title of every video in the Semantic Metadata Store by executing a SPARQL query.
In version 1:
(Contributed by MCS Architect extraordinaire, Jackie Nichols)
private static DataTable GetAllVideoes()
{
string strQuery = @"
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX imm: <http://schemas.microsoft.com/imm/core/1.0#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?videoItem ?videoTitle WHERE {
?videoId rdf:type imm:VideoItem.
?videoId dc:title ?videoTitle.
}";
bool isSparqlXml;
XmlDocument resultXml = new XmlDocument();
DataTable returnTable = null;
using (Impersonator.ImpersonateAppPool())
{
using (DamServiceClient dsc = new DamServiceClient("DamEndpoint"))
{
QueryRequest request = new QueryRequest();
request.IsSparql = true;
request.Query = strQuery;
QueryResponse response = dsc.Query(request);
isSparqlXml = response.IsSparqlXml;
resultXml.LoadXml(response.Result);
}
}
if (isSparqlXml)
{
returnTable = SparqlResult.ToDataTable(resultXml.CreateNavigator());
}
else
{
TripleCollection trips = new TripleCollection();
trips.Load(resultXml.CreateNavigator(), null);
returnTable = trips.ToDataTable();
}
return returnTable;
}
Version 2:
private static DataTable GetAllVideoes()
{
string strQuery = @"
SELECT ?videoItem ?videoTitle WHERE {
?videoId rdf:type imm:VideoItem.
?videoId dc:title ?videoTitle.
}";
DataTable returnTable = null;
using (Impersonator.ImpersonateAppPool())
{
using (ReadClient dsc = new ReadClient("Read"))
{
Table response = dsc.Query(strQuery);
returnTable = response.ToDataTable();
}
}
return returnTable;
}
Let me highlight some of the differences:
· Prefixes - Many common namespaces are handled automatically. In this example, therefore, I can use the QNames for RDF, Dublin Core, and IMM without defining the full namespace in my query.
· Table - The Table class is an intermediary object that stores RDF values coming from the IMM Semantic Metadata Store. (Note: Powerful things can be done with a "Table." I will revisit Table soon.)
· QueryRequest - It still exists as SparqlQueryRequest. There is an overload on Query, however, that allows you to just specify the SPARQL command text.