locked
Strongly vs Weakly-Typed Resources RRS feed

  • General discussion

  • Hi all,

    as written in some of my earlier posts, I have loaded a data model via RDF into Zentity, then loaded data using a custom app, similar to the USPresidents example. What I've noticed is that the Zentity Data Service creates the strongly-typed entities I defined (e.g. topic and indicator). However, a Topic (.NET Type) is not seen as a Resource (.NET Type from the Zentity Data Service Assembly), but it has the same properties as a resource (Title, Uri, Description etc.)

    Now, when I'm reading instances from somewhere (e.g. from a SPARQL response in my case), I would like to be able to use these core properties without needing a cast, like so:

    Resource r = new Resource(new Guid());
    r.Uri = ReadUriFromSomewhere();
    r.Title = ReadTitleFromSomewhere();
    ...
    

    Instead, this is currently not possible, and I have to do this via Reflection like so:

    object tmpEntity = Activator.CreateInstance<object>();
    
    PropertyInfo piDateAdded = resourceType.GetProperty("DateAdded");
    PropertyInfo piDateModified = resourceType.GetProperty("DateModified");
    
    piDateAdded.GetSetMethod().Invoke(tmpEntity, 
    new object[] { DateTime.Now });
    
    piDateModified.GetSetMethod().Invoke(tmpEntity, 
    new object[] { DateTime.Now });
    
    

    What gives? Am I really overseeing something, or is there really no way of accessing your own resources as Zentity Resource objects? Shouldn't there be? It seems counterintuitive not to generate all custom entities as based on the core Resource type.

    Sunday, February 26, 2012 12:08 PM

All replies

  • Hi WizMan,

    I am working on this query and update you once done. I will get back to you within 24 hrs.

    -Regards

    Monday, February 27, 2012 7:54 AM

  • To create a resource you need to have its ResourceType otherwise it will be created as a instance of the default resource in the Zentity.Core
    which is Resource. As you might have noticed in the USPresident sample to create a new resource of type USPresident you write code

    using PivotDemo = ZenDataService.PivotDemo.Model;

    .
    .
    PivotDemo.USPresidentpresident = PivotDemo.USPresident.CreateUSPresident(Guid.NewGuid());
    president.Title = presItem.Name;
    president.Uri = presItem.Uri;
    president.Description = presItem.Description;

    .
    .

    Similarly to create a resource of type chapter which is in the ScholarlyWorks data module we the following code

    using ScholarlyDemo = ZenDataService.Zentity.ScholarlyWorks;

    .
    .
    ScholarlyDemo.Chapterchapter = ScholarlyDemo.Chapter.CreateChapter(Guid.NewGuid());

    chapter.BookTitle = "Service Oriented Architecture";
    chapter.Edition = "first";
    .
    .

    Resource class is generally used to manipulate specific resource efficiently.Example below shows how to perform basic Create, Read, Update and Delete (CRUD) operations on a resource.

    using Zentity.Core;
        using System;
        using System.Linq;
        using System.Data.EntityClient;
        using System.Data;
        
        namespace ZentitySamples
        {
            public class Program
            {
                static Guid pubId = Guid.Empty;
                static string connStr = @&quot;provider=System.Data.SqlClient;
                        metadata=res://Zentity.Core;
                        provider connection string='Data Source=.;
                        Initial Catalog=Zentity;Integrated Security=True;
                        MultipleActiveResultSets=True'&quot;;
        
                public static void Main(string[] args)
                {
                    CreateResource();
                    RetrieveUsingLinqToEntities();
                    UpdateResource();
                    RetrieveUsingEntitySQL();
                    DeleteResource();
                }
        
                private static void DeleteResource()
                {
                    using (ZentityContext context = new ZentityContext(connStr))
                    {
                        Resource pub = context.Resources.Where(tuple =&gt; tuple.Id == pubId).FirstOrDefault();
                        context.DeleteObject(pub);
                        context.SaveChanges();
        
                        // Verify that the resource is deleted.
                        int i = context.Resources.Where(tuple =&gt; tuple.Id == pubId).Count();
                        Console.WriteLine(&quot;Count of resources with id [{0}] = [{1}]&quot;, pubId, i);
                    }
                }
        
                private static void RetrieveUsingEntitySQL()
                {
                    using (ZentityContext context = new ZentityContext(connStr))
                    {
                        EntityCommand cmd = (EntityCommand)context.Connection.CreateCommand();
                        cmd.CommandText = &quot;SELECT Pubs.Uri, Pubs.Title&quot; +
                            &quot; FROM ZentityContext.Resources AS Pubs &quot; +
                            &quot; WHERE Pubs.Id = @Id&quot;;
                        EntityParameter param = new EntityParameter(&quot;Id&quot;, DbType.Guid);
                        param.Direction = ParameterDirection.Input;
                        param.Value = pubId;
                        cmd.Parameters.Add(param);
                        if (context.Connection.State == System.Data.ConnectionState.Closed)
                            context.Connection.Open();
                        EntityDataReader rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
                        rdr.Read();
                        Console.WriteLine(&quot;Resource details:&quot;);
                        Console.WriteLine(&quot;Uri: [{0}]&quot;, rdr.GetString(0));
                        Console.WriteLine(&quot;Title: [{0}]&quot;, rdr.GetString(1));
                    }
                }
        
                private static void UpdateResource()
                {
                    using (ZentityContext context = new ZentityContext(connStr))
                    {
                        Resource pub = context.Resources.Where(tuple =&gt; tuple.Id == pubId).FirstOrDefault();
                        pub.Title = &quot;New Title&quot;;
        
                        context.SaveChanges();
                        Console.WriteLine(&quot;Updated title of the resource with id: [{0}]&quot;, pubId);
                    }
                }
        
                private static void RetrieveUsingLinqToEntities()
                {
                    using (ZentityContext context = new ZentityContext(connStr))
                    {
                        Resource pub = context.Resources.Where(tuple =&gt; tuple.Id == pubId).FirstOrDefault();
                        Console.WriteLine(&quot;Resource details:&quot;);
                        Console.WriteLine(&quot;Uri: [{0}]&quot;, pub.Uri);
                        Console.WriteLine(&quot;Title: [{0}]&quot;, pub.Title);
                    }
                }
        
                private static void CreateResource()
                {
                    using (ZentityContext context = new ZentityContext(connStr))
                    {
                        // Create a new resource.     
                        Resource pub = new Resource();
                        pub.Title = &quot;Some paper on global warming.&quot;;
                        pub.Uri = &quot;urn:zentity-samples:publications:global-warming&quot;;
        
                        // Add the resource to context.     
                        context.AddToResources(pub);
        
                        // Save the context.     
                        context.SaveChanges();
                        pubId = pub.Id;
                        Console.WriteLine(&quot;Created Resource with id: [{0}]&quot;, pubId);
                    }
                }
            }
        }

    Please let me know if this helps you in resolving your query.

    -Regards

    Tuesday, February 28, 2012 6:36 AM
  • Hi Kalnemi and thanks for the detailed response! I wanted to update any resource by URI, Title and Description, regardless of what type they are while using data web services. In that regard I think I have not expressed correctly what I wanted to achieve. I know how to use the individual objects (i.e. USPresident), I needed a way to update the common properties of a Resource, without knowing what type it is (e.g. Publication or Person). I can try doing that using SQL, sure, I was just curious if it was possible using the web service.


    Tuesday, February 28, 2012 7:48 PM
  • This can be achieved the same way.

    using ZenDataService;


    ZenDataService.Resource res = Resource.CreateResource(new Guid());
    res.RelationshipsAsObject = new System.Data.Services.Client.DataServiceCollection<Relationship>();
    res.ResourceProperties = new System.Data.Services.Client.DataServiceCollection<ResourceProperty>();
    res.Uri = "someuri";

    • Edited by Kalnemi Wednesday, February 29, 2012 6:17 AM code edit
    Wednesday, February 29, 2012 6:16 AM