Telerik UI for Windows Phone by Progress

In many scenarios relations are created between separate business entities like for example the person-orders relation or product-category relation. The Cloud Data Sync mechanism supports a simple approach of handling such scenarios by using the CLR RelationAttribute and the ISynchronizableCloudItem.GetRelatedItems method.

Defining relations between ISynchronizableCloudItem objects

To define a data relation you need to use the RelationAttribute class and define attributes on the properties on your business entity which identify another business entity. For the purposes of demonstration, let's assume that we have the following two simple class definitions:

CopyC#
public class Employee : SynchronizableDataItem
{
    private string name;
    private int age;
    private Gender gender;
    private PositionType position;

    public string Name
    {
        get
        {
            return this.name;
        }
        set
        {
            if (this.name != value)
            {
                this.name = value;
                this.OnPropertyChanged<string>(() => this.Name);
            }
        }
    }

    public int Age
    {
        get
        {
            return this.age;
        }
        set
        {
            if (this.age != value)
            {
                this.age = value;
                this.OnPropertyChanged<int>(() => this.Age);
            }
        }
    }


    public PositionType Position
    {
        get
        {
            return this.position;
        }
        set
        {
            if (this.position != value)
            {
                this.position = value;
                this.OnPropertyChanged<PositionType>(() => this.Position);
            }
        }
    }

    public Gender Gender
    {
        get
        {
            return this.gender;
        }
        set
        {
            if (this.gender != value)
            {
                this.gender = value;
                this.OnPropertyChanged<Gender>(() => this.Gender);
            }
        }
    }
}

And a Task class which relates to the Employee class via the EmployeeID property:

CopyC#
public class Task : SynchronizableDataItem
{
    private string name;
    private string notes;
    private bool isCompleted;
    private TaskPriority priority;

    public Task()
    {
        this.Priority = TaskPriority.medium;
    }

    [Relation(typeof(Employee), "Id")]
    public Guid EmployeeID
    {
        get
        {
            return this.GetValue<Guid>("EmployeeID", Guid.Empty);
        }
        set
        {
            this.SetValue("EmployeeID", value);
        }
    }

    public string Name
    {
        get
        {
            return this.name;
        }
        set
        {
            if (this.name != value)
            {
                this.name = value;
                this.OnPropertyChanged("Name");
            }
        }
    }

    public string Notes
    {
        get
        {
            return this.notes;
        }
        set
        {
            if (this.notes != value)
            {
                this.notes = value;
                this.OnPropertyChanged("Notes");
            }
        }
    }

    public bool IsCompleted
    {
        get
        {
            return this.isCompleted;
        }
        set
        {
            if (this.isCompleted != value)
            {
                this.isCompleted = value;
                this.OnPropertyChanged("IsCompleted");
            }
        }
    }

    public TaskPriority Priority
    {
        get
        {
            return this.priority;
        }
        set
        {
            if (this.priority != value)
            {
                this.priority = value;
                this.OnPropertyChanged("Priority");
            }
        }
    }
}

The relation between a Task and an Employee is built by using the RelationAttribute on the property in the Task class which represents the identificator of the related Employee:

CopyC#
[Relation(typeof(Employee), "Id")]

If you want to retrieve all tasks of a given employee, you should use the following approach:

CopyC#
IEnumerable<Task> relatedTasks = employee.GetRelatedItems<Task>();

Note
The GetRelatedItems call will retrieve the corresponding SynchronizationContext for the Task type and will retrieve all related tasks that are currently available in it. You may want to call SynchronizeAsync to make sure the tasks are in sync before getting the related items.