RDF - Object Mapping for RDF in .net

MakoLab open-source component

Your first entity

Creating an entity type

Each entity type must be an interface derived from IEntity. For example if we want to use an entity representing people, we could create an IPerson interface

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
using RomanticWeb.Mapping.Model;

public interface IPerson : IEntity
{
    string Name { get; set; }

    string LastName { get; set; }

    ICollection<IPerson> Friends { get; set; }
}

Mapping to RDF terms

The entity type above is not yet complete. In order to use it to manipulate RDF data it has to be mapped to RDF classes and properties. In this example let’s use the Friend of a Friend vocabulary, or foaf. Foaf defines various terms, which describe people and their relationships. Please refer to the project’s documentation for more information.

The three properties declared by the IPerson type each have a foaf counterpart: foaf:givenName, foaf:familyName and foaf:knows. Now we need a way to define that a given property corresponds to an RDF property. This can be done in two ways. One is by using attributes and the other is by using a fluent interface.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
using RomanticWeb.Mapping.Attributes;
using RomanticWeb.Mapping.Model;

public interface IPerson : IEntity
{
    [Property("http://xmlns.com/foaf/0.1/givenName")]
    string Name { get; set; }

    [Property("http://xmlns.com/foaf/0.1/familyName")]
    string LastName { get; set; }

    [Property("http://xmlns.com/foaf/0.1/knows")]
    ICollection<IPerson> Friends { get; set; }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
using RomanticWeb.Mapping.Fluent;

public class PersonMap : EntityMap<IPerson>
{
    public PersonMap()
    {
        Property(p => p.Name).Term.Is(new Uri("http://xmlns.com/foaf/0.1/givenName"));
        Property(p => p.LastName).Term.Is(new Uri("http://xmlns.com/foaf/0.1/familyName"));
        Collection(p => p.Friends).Term.Is(new Uri("http://xmlns.com/foaf/0.1/knows"));
    }
}

The above examples show how to map the IPerson’s properties to RDF predicates. Note that absolute URIs are used. Later in this tutorial you’ll learn how to use prefixed names (aka QNames).

Attributes are a simpler way to define mapping, but their disadvantage is that they have to be static. Fluent mappings on the other hand can be dynamic. Term names can be computed at runtime. You will also want to use fluent mappings if the mapped types are defined in another assembly.

NOTE It is possible to mix both mapping styles, even for a single type.

Please click the link on the left to read more about to learn more about different mapping options.

RDF classes

An entity type can optionally be mapped to one or more RDF classes.

1
2
3
4
5
6
7
8
using RomanticWeb.Mapping.Attributes;
using RomanticWeb.Mapping.Model;

[Class("foaf", "Person")]
[Class("foaf", "Agent")]
public interface IPerson : IEntity
{
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
using RomanticWeb.Mapping.Fluent;

public class PersonMap : EntityMap<IPerson>
{
    public PersonMap()
    {
        Class.Is("foaf", "Person");
        Class.Is("foaf", "Agent");
    }
}

When an entity type is mapped to an RDF class and rdf:type triple will be added whenever its type is changed. Please find out more on the entity context page.