Live Framework CTP #5 – .NET: Data hierarchies and LiveItem types

Hierarchies of data entries – the problem

As stated before, when programming against the Live Framework libraries in .NET, all data entries are located on one level below the according data feed. This very often doesn’t correspond with a programmer’s expectations, when he has a hierarchy of data entries, e.g. a hierarchy of data folders and files. As programmer you want to have a tree-like view on this data to work with it. Unfortunately, in the current LiveFx CTP this was not made possible directly.

Extending DataFeed and DataEntry

The solution for this is not far away. A data entry’s resource has got an Id and a ParentId as properties, over which one can identify and associate an entry’s parent and its child entries. A „root entry“ has got the ParentId "urn:uuid:00000000-0000-0000-0000-000000000000" or it is null. This information is sufficient to write some extension methods on DataFeed and DataEntry for getting hierarchical information. Thus I’m starting to make a little class library, which I will further work on and extend it. First I’ve got a little class named „MeshConstants„. There resides the ParentId for a root data entry:

public static class MeshConstants
{
    public const string RootDataEntryId = "urn:uuid:00000000-0000-0000-0000-000000000000";
}

With this it’s easy to extend DataFeed with a method GetRootDataEntries(), which delivers only the data entries on the top level of the hierarchy:

public static ICollection<DataEntry> GetRootDataEntries(this DataFeed feed)
{
    return (from entry in feed.DataEntries.Entries
            where entry.IsLoaded && entry.IsRootEntry()
            select entry).ToList();
}

This method accesses IsRootEntry() of DataEntry, which is an extension method, too and indicates, if a data entry is on the root level (has no parent) or not:

public static bool IsRootEntry(this DataEntry entry)
{
    if (String.IsNullOrEmpty(entry.Resource.ParentId))
        return true;

    return entry.Resource.ParentId.Equals(MeshConstants.RootDataEntryId);
}

Another extension method for DataEntry gives us the child entries in return:

public static ICollection<DataEntry> GetChildEntries(this DataEntry entry)
{
    return (from child in entry.GetDataFeed().DataEntries.Entries
            where child.Resource.ParentId == entry.Resource.Id
            select child).ToList();
}

This makes use of the method GetDataFeed(), which is needed for getting the data feed, this data entry is associated to. Unfortunately, the .NET libraries of the Live Framework CTP don’t come along with this possibility innately:

public static DataFeed GetDataFeed(this DataEntry entry)
{
    return (from feed in
                (from meshobj in entry.LiveOperatingEnvironment.Mesh.MeshObjects.Entries
                 select meshobj.DataFeeds.Entries).Aggregate((fl1, fl2) => fl1.Union(fl2))
                 where feed.DataEntries.Entries.Contains(entry)
            select feed).Single();
}

Example: traversing the hierarchy

Now we’ve got all together for traversing a hierarchy of data entries recursively. We just need a recursive method, which executes the wanted action for a data entry and then recursively calls itself for all child entries. The following example method builds up a WinForms TreeView structure by adding TreeNode elements recursively:

private void addTreeNodesForChildDataEntries(DataEntry parentEntry, TreeNode parentNode)
{
    foreach (var childEntry in parentEntry.GetChildEntries())
    {
        var childNode = new TreeNode(childEntry.Resource.Title + ": " + childEntry.Resource.Type);
        parentNode.Nodes.Add(childNode);
        addTreeNodesForChildDataEntries(childEntry, childNode);
    }
}

The result is shown below exemplarily:

Live Framework - Recursing TreeView example

Types of LiveItem elements

The resources of MeshObject, DataFeed and DataEntry have an associated Type property, over which you can distinguish different types of objects. Furthermore, Type is a string and thus allows you to define your own application-specific object types. But the Live Framework comes with some standard types by default, where the most important are:

MeshObject.Type:

  • "LiveMeshFolder": Top-level folder, which can contain further (data entry) folders and files.
  • "ApplicationInstance": Instance of an application, which has been installed through the Live Application Catalogue.

DataFeed.Type:

  • "LiveMeshFiles": Indicates the feed as container for file and folder data entries.

DataEntry.Type:

  • "Folder": Data entry is a folder, which can contain further data entries.
  • "File": Data entry is a file, which may contain your data.

Based on that information (and the extension methods above), I’ve written some more extension methods on the several classes to filter the child elements accordingly:

public static class MeshObjectTypes
{
    public const string LiveMeshFolder = "LiveMeshFolder";
    public const string ApplicationInstance = "ApplicationInstance";
}

public static class DataFeedTypes
{
    public const string LiveMeshFiles = "LiveMeshFiles";
}

public static class DataEntryTypes
{
    public static string File = "File";
    public static string Folder = "Folder";
}

public static class MeshExtensions
{
    public static ICollection<MeshObject> GetApplicationInstanceMeshObjects(this Mesh mesh)
    {
        return (from meshobj in mesh.MeshObjects.Entries
                where meshobj.Resource.Type == MeshObjectTypes.ApplicationInstance
                select meshobj).ToList();
    }

    public static ICollection<MeshObject> GetFolderMeshObjects(this Mesh mesh)
    {
        return (from meshobj in mesh.MeshObjects.Entries
                where meshobj.Resource.Type == MeshObjectTypes.LiveMeshFolder
                select meshobj).ToList();
    }
}

public static class MeshObjectExtensions
{
    public static ICollection<DataFeed> GetFilesChildFeeds(this MeshObject meshobj)
    {
        return (from feed in meshobj.DataFeeds.Entries
                where feed.Resource.Type == DataFeedTypes.LiveMeshFiles
                select feed).ToList();
    }
}

public static class DataFeedExtensions
{
    public static ICollection<DataEntry> GetFileDataEntries(this DataFeed feed)
    {
        return feed.GetFileDataEntries(false);
    }

    public static ICollection<DataEntry> GetFolderDataEntries(this DataFeed feed)
    {
        return feed.GetFolderDataEntries(false);
    }

    public static ICollection<DataEntry> GetFileDataEntries(this DataFeed feed, bool onlyRootEntries)
    {
        var entries = onlyRootEntries ? feed.GetRootDataEntries() : feed.DataEntries.Entries;
        return (from entry in entries
                where entry.Resource.Type == DataEntryTypes.File
                select entry).ToList();
    }

    public static ICollection<DataEntry> GetFolderDataEntries(this DataFeed feed, bool onlyRootEntries)
    {
        var entries = onlyRootEntries ? feed.GetRootDataEntries() : feed.DataEntries.Entries;
        return (from entry in entries
                where entry.Resource.Type == DataEntryTypes.Folder
                select entry).ToList();
    }
}

public static class DataEntryExtensions
{
    public static ICollection<DataEntry> GetFileChildEntries(this DataEntry entry)
    {
        return (from child in entry.GetChildEntries()
                where child.Resource.Type == DataEntryTypes.File
                select child).ToList();
    }

    public static ICollection<DataEntry> GetFolderChildEntries(this DataEntry entry)
    {
        return (from child in entry.GetChildEntries()
                where child.Resource.Type == DataEntryTypes.Folder
                select child).ToList();
    }
}

kick it on DotNetKicks.com

Live Framework CTP #4 – .NET: LOE and data basics

Instead of creating new Silverlight applications, which run in your mesh and use the data, you can furthermore mesh-enable your existing .NET applications or create new mesh-enabled .NET applications from scratch. That’s pretty easy and in the following post I will show you some basics…

The Live Operating Environment (LOE)

First there is the Live Operating Environment, being the center point of all your operations to the mesh. With an object of type LiveOperatingEnvironment you can connect to your online mesh or to your local mesh storage sync or async and perform any operation you want. But that’s just the developer’s point of view. In fact, the LOE (every instance of it) is an agent component, responsible for providing an efficient cache and for background synchronization of data on your device to other devices (other instances of the LOE on those devices) in your mesh, including the „special“ LOE in the cloud. Running an LOE means exposing data through a RESTful interface at port 2048 of your machine – one could check the presence of this and either connect locally or to the cloud.

Connecting to your online/cloud LOE means that you have to provide your credentials on connect, whereas you can connect locally without those. I like to work on my local storage, it’s fast and safe. But there are some limitations (at the moment?) there. Due to the lack of file synchronization, files as data entries are not available from the local storage, so you have to connect to your online LOE instead. Furthermore you only get your local device in the Devices collection of your LOE and it’s not possible to have a look at your contacts and profiles for instance. What I expected first or want to have for the „final“ version of the Live Framework: just one connection method, which takes local data if it’s available and instead connects to the cloud LOE. It should be no problem, because the various LOEs (loal, cloud) are taking responsibility over synchronization… or have I forgotten to consider something?

Well, let’s take a look to connecting to your (online) LOE. Just create an instance of LiveOperatingEnvironment and connect via the Connect() method (local LOE: use ConnectLocally() instead). You have to pass your credentials as a parameter, which could come from a secure source or from user input:

var creds = new NetworkCredential("username", "password", "https://user-ctp.windows.net");
var loe = new LiveOperatingEnvironment();

loe.Connect(creds);

https://user-ctp.windows.net is the fixed base URI when connecting to your cloud LOE. After you’ve been connected, you get access to your mesh items (shown before) and can start meshing 🙂

Mesh Objects, Data Feeds and Data Entries

A mesh object is a container for concrete data in your mesh. There are several types of mesh objects and you are allowed to create your own application specific types, which your applications can work with. Mesh objects can be files, folders, applications, custom objects etc.. You have access to the mesh objects via loe.Mesh.MeshObjects.Entries and have the possibililty to easily iterate over them in this way.

Every mesh object consists of DataFeeds, Members, Mappings and News, as stated before. Interesting at this point are the data feeds. Those are further containers for the real data entries. Why this abstraction? Over data feeds you can group your data together, separating logically distinct data. Every DataFeed object finally contains a DataEntries collection, which gives you access to the underlying data and information about that. Note: there is no obvious possibility to store hierarchical data entries at the moment. There is one level of data entries and a data entry can’t contain other data entries. So that’s a drawback at this point and I don’t like/understand it much. The solution for that is: every data entry contains a parent ID and over that you can identify top level data entries and those, who are child entries of another data entry. But that’s not a good solution, isn’t it? Hopefully there will be done some work on that…

Well, a nice little .NET console application would be to show us the titles of all mesh objects, of their data feeds and data entries in there. That’s no problem and leads to the following little program:

var creds = new NetworkCredential("username", "password", "https://user-ctp.windows.net");
var loe = new LiveOperatingEnvironment();
loe.Connect(creds);
if (loe.IsRunning())
{
    foreach (var meshobj in loe.Mesh.MeshObjects.Entries)
    {
        Console.WriteLine("- " + meshobj.Resource.Title);
        foreach (var feed in meshobj.DataFeeds.Entries)
        {
            Console.WriteLine("  + " + feed.Resource.Title);
            foreach (var entry in feed.DataEntries.Entries)
                Console.WriteLine("    * " + entry.Resource.Title);
        }
    }
}
Console.ReadLine();

Create, Update, Delete

The creation, update and delete of all the three entities MeshObject, DataFeed and DataEntry is made really easy. Just call the Add() and Remove() methods of the particular collections. The rest is done automatically by the Live Framework. The update process isn’t problematic, too. Just change your entity and then make sure to call the Update() method of this entity. Because everything in your Live Operating Environment has LiveItem as base type, you have an integrated and consistent CRUD process.

Little example

The following screenshot shows a little WinForms application, that connects to my mesh and over which I can perform the CRUD process on my mesh entities. Further on I can navigate through my mesh and if I got images as data entries, those are downloaded automatically as media resource stream and then shown up at the picture box on the right. In the screen below is shown the (IKEA-powered 😉 ) couch corner of my little apartment…

Live Framework .NET WinForms example application

In upcoming blog posts I want to show you more information about handling data entries, media resources and hierarchical data. So stay tuned 🙂

kick it on DotNetKicks.com