Welcome to MSDN Blogs Sign in | Join | Help
Creating an ADO.NET DataService for RDF Entities in IMM

Ok, so I have been playing around with the ADO.NET DataServices to get something working for a REST based access to the RDF Store. My goal here was to create a simple REST based way to grab metadata out of the RDF store based on the ADO.NET DataServices model instead of relying on SOAP and SPARQL. I just wanted a simple way to get a specific object and some basic locked down values into a Silverlight application. 

To create an ADO.NET DataService for use with IMM you need to follow these steps. Keep in mind that the DataService will need to be locked down to a specific Entity view of objects in the IMM Store, and is mostly meant to be read only for Silverlight applications or public sites.

  1. Fire up Visual Studio 2008 SP1. (has to be SP1)
  2. Create a new ASP.NET Web Application
    clip_image001

  3. Add a new solution Folder and call it Views

    clip_image003
  4. Add a New Item to the Views folder. Select Text item and call it
    “CreateVideoItemsView.sql”

    clip_image005
  5. We’ll use this SQL script to create a new View in the IMMMetadata database for us to expose via LINQ to SQL

  6. Copy and paste this CREATE View script into the new sql script file.   

    A few notes here. This creates a new View table based on a SPARQL query. You can edit this and create as many columns as you want bound to your SPARQL query.
    In this case, I am creating a new row for every imm:VideoItem in the database and adding a column for the title, description, and proxy path.

    ONE VERY IMPORTANT NOTE HERE – YOU MUST use the column name “ID” (in caps!) for the Entity framework and ADO.NET DataServices to work properly.  That becomes the default primary key on the table and will be the value that you use to index into the REST service’s list of VideoItems. In this example below, I used a SUBSTRING on the value returned from IMM as the Subject. The reason I did this is because I wanted to use only the GUID at the end of the Subject URI as the method of indexing, so my URI looks like this -

    http://localhost:29062/SemanticMetadata.svc/VideoItems('A8FC8F13-0A34-4446-B7E0-1AD8AF9E1A49').

    Nice and simple to use instead of a full URL.

    CREATE VIEW dbo.VideoItems
    AS
    SELECT SUBSTRING(c1.Value,50,37) AS ID,
         c2.Value as Title, 
         c3.Value as [Description],
         c4.Value as [Ref], 
    FROM dbo.sparql ('
    PREFIX dc: <http://purl.org/dc/elements/1.1/>
    PREFIX imm: <http://schemas.microsoft.com/imm/2.0/core/>
    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    PREFIX did: <urn:mpeg:mpeg21:2002:02-DIDMODEL-NS#>
    PREFIX dii: <urn:mpeg:mpeg21:2002:01-DII-NS#>
    SELECT ?item ?title ?description ?ref
    FROM <http://schemas.microsoft.com/IMM/data>
    WHERE
    {
        ?item rdf:type imm:VideoItem.
        ?item dc:title ?title.
        ?item dc:title ?description.
        ?item did:ResourceCollection ?resource.
        ?resource imm:ResourceType "Proxy"^^<http://www.w3.org/2001/XMLSchema#string>.
        ?resource did:Ref ?ref.
    }
    ')
     
    GO
     
  7. With the SQL script open hit F5 to execute it. You will need to connect to the database “IMMMetadata”.
  8. Once that is successful, we now have a View in our database that contains the VideoItem data that we wanted. You can repeat this process to add Views for other Ontology class types in the RDF Store.
  9. Next we are going to add our ADO.NET DataService. Add a New ADO.NET Data Service Item to the Project.  If you don’t see that item, then you don’t have Visual Studio SP1 installed.
    Call the item something like IMMDataService.svc

    clip_image007
  10. Next we will add a LINQ to SQL class. Go to Add Item to Project and select the LINQ to SQL template. Name it IMMMetadata.dbml


    clip_image009

  11. Now we need to make sure we have a connection to the SQL database in our Server Explorer. Should have this since we connected to the SQL to run the View Script.
    Open it up and check to see if you View is in the IMMMetadata database.


    clip_image011

  12. Next you will need to drag the View out onto the LINQ to SQL designer surface.


    clip_image013

  13. Open the DataService class file – IMMDataService.svc. We need to make a few edits in this file to get stuff working.
  14. Edit the file by adding your DataContext to the base class type for the DataService. In my case below I added IMMMetadataDataContext which is the class that was generated by the LINQ to SQL tool.
    In addition, I uncommented the Access Rule in the InitializeService method and changed the name of my Entity set to “VideoItems” to expose that collection. You can adjust the security settings as needed, but AllRead is fine for now.  If you want to expose ALL entity sets that you may have views for, you can add more lines, or for testing you can set the value to “*” to expose everything.
    /// <summary>
    /// The IMM Data Service for use with ADO.NET
    /// </summary>
     
    public class IMMDataService : DataService<IMMMetadataDataContext>
    {
     
        // This method is called only once to initialize service-wide policies.
        /// <summary>
        /// Initializes the service.
        /// </summary>
        /// <param name="config">The config.</param>
        
        public static void InitializeService(IDataServiceConfiguration config)
        {
        
                 config.SetEntitySetAccessRule("VideoItems", EntitySetRights.AllRead);
                // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
        }
        
    }
  15. Now, just Build your solution and browse to the IMMDataService.svc page.  You should see the following.
    If you cannot see the output in the above way, go to Tools->Internet Options-> Content Tab. Click the Settings button in the Feeds section, an uncheck the Turn on Feed Reading View option. You may need to restart IE for that setting to take. I had to on server but not on Vista.

    clip_image015
  16. Notice that the collection is named VideoItems. It used the name of our View to create the collection name.
  17. We can now drill down into that collection.  To view the whole list of items we could hit this URL. That could be really slow on a huge database, and you may want to disable that in production by setting the EntitySetRights in the InitializeService method to allow only single items.

    http://localhost:33655/IMMDataService.svc/VideoItems

    clip_image016
  18. Note that the format of the feed is AtomPub. You can also get a JSON feed if you like – just look at the docs on DataServices).
  19. Also notice that in the feed we now have an //entry/id element that contains the full URI to the details page for each asset.
    I have the Litware RDF data loaded so I am seeing the Litware items. If you have this sample data loaded also, you should see the Billion Dollar Limited title asset as the first item.
    If you don’t have anything listed at this point, you probably don’t have any items that match your SPARQL query loaded in the database. Load some data, or check to make sure the SPARQL query you used to create the view is correct.

  20. To drill down into the details, just use the URI in the ID element, or adjust your URI on the address bar to include the GUID of the item you want.
    Notice that I now have just the single item’s details.

    clip_image017
  21. We can even go further into the item by adding the Predicate name to the end of the URI. For example to get the Ref path for the proxy, I just add /Ref to the URL.

    clip_image019
  22. I can now also use some queries to change the resultsets. Try out a few of these…

    a.) Select the Top 3 Items

    http://localhost:29062/SemanticMetadata.svc/VideoItems?$top=3

    b.) Order by Title predicate

    http://localhost:29062/SemanticMetadata.svc/VideoItems?$orderby=Title

    c.) Skip ahead by 10 items in the results and limit to 10.  Good for paging!

    http://localhost:33655/IMMDataService.svc/VideoItems?$skip=10&$top=10

    d.) Filter by the content of a predicate.

    http://localhost:33655/IMMDataService.svc/VideoItems?$filter=Title eq 'Volcano'  

    e.) More query options and examples are here and here

  23. If you want to use the REST service in your .NET code or more importantly in a Silverlight application, you must deploy the DataService to the same Domain as you Silverlight application.

    1. To use the data service from the application for Silverlight:

    a.) Generate data service classes from the running data service. These classes represent each of the entities that are defined in the data service. This step can be completed by using the Add Service Reference Dialog Box in Visual Studio. Make sure the data service is running, right-click the project, and then select Add Service Reference. Type the base URL of the data service, such as http://localhost:33655/IMMDataService.svc into the address text box and click Go.
    The output from the command is a C# or Visual Basic file that contains a class for each entity type in the data service. For more information about the client classes, see .NET Client Library (ADO.NET Data Services Framework). The System.Data.Services.Client assembly is added to the project automatically when you generate the client classes by using Add Service Reference. This assembly comes with Silverlight and contains the classes needed by the client application to access ADO.NET Data Services.

    b.) The classes that represent data service types can also be generated by using DataSvcUtil.exe. DataSvcUtil.exe is located in the directory \WINDOWS\Microsoft.NET\Framework\v3.5\. The command line takes two arguments: the name and path of an output file and the base URL to the data service for which types are to be generated.

    c.) In the page.xaml.cs, add the following using directive: using System.Data.Services.Client.

  24. You can now create an instance of the DataServiceContext in your Silverlight client code and add data binding to you user interface elements

    For more details on the ADO.NET Data Services Framework see http://msdn.microsoft.com/en-us/library/cc668792.aspx

  25. If you want to add relationships, say for example Resources under the VideoItems you just do the following.

  26. Add a new View for Resources. Here’s my sample view…
    CREATE VIEW dbo.Resources
    AS
    SELECT SUBSTRING(c1.Value,50,37) AS ID,
               c2.Value as Type,
               c3.Value as [Size],
               c4.Value as vFormat,
               c5.Value as Mime,
               c6.Value as Ref,
               c7.Value As Lang,
               SUBSTRING(c8.Value,50,37) As ParentID
    FROM dbo.sparql ('
    PREFIX dc: <http://purl.org/dc/elements/1.1/>
    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    PREFIX dii: <urn:mpeg:mpeg21:2002:01-DII-NS#>
    PREFIX did: <urn:mpeg:mpeg21:2002:02-DIDMODEL-NS#>
    PREFIX imm: <http://schemas.microsoft.com/imm/2.0/core/>
    SELECT ?s ?type ?size ?vFormat ?mime ?ref ?lang ?parent
    FROM <http://schemas.microsoft.com/IMM/data>
    WHERE
    {
        ?s rdf:type did:Resource.
        ?parent did:ResourceCollection ?s.
        OPTIONAL{
            ?s imm:ResourceType ?type.
            ?s imm:SizeInBytes ?size.
            ?s imm:VideoFormat ?vFormat.
            ?s did:MimeType ?mime.
            ?s did:Ref ?ref.
            ?s dc:language ?lang.
         }
    }
    ')
     
  27. Next in the LINQ to SQL designer, drag the new Resources View table out.
  28. Right click on the design surface and Add an Association. Set it up like this.

    clip_image021
  29. Set the Primary Key for both “ID” columns in the designer Property box.

    clip_image022

  30. Rebuild your Solution and relaunch the DataService in the browser.
  31. You will now be able to drill down into your linked Resources from your VideoItems

clip_image024

 

Have Fun!

 






John Deutscher
Group Program Manager
Microsoft
johndeu@microsoft.com

Posted: Thursday, December 04, 2008 6:46 PM by johndeu
Anonymous comments are disabled
Page view tracker