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:
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(); } }
I have linked your blog post to Live Framework Forum on MSDN Forums
http://social.msdn.microsoft.com/Forums/en-US/liveframework/thread/828d9a48-239a-4af8-8239-35931e514d37