This is a short post for anyone doing .Net development using the Live Framework.
Ben Williams recently posted a blog entry Live fx Helper Class where he made available a class of static methods (most of them extension methods) that are useful when building Mesh aware applications. Those methods are:
- FindMeshObject(this Mesh mesh, string title, bool createIfNotFound)
- FindMeshObject(this Mesh mesh, string title)
- FindDataFeed(this MeshObject parent, string title, bool createIfNotFound)
- FindDataFeed(this MeshObject parent, string title)
- AreInvitationsPending(this MeshObject meshObject)
- GetResourceList(List<MeshObject> entries)
- GetResourceList(List<DataFeed> entries)
- GetResourceList(List<DataEntry> entries)
- FindDataEntry(this DataFeed parent, string title, bool createIfNotFound)
- FindDataEntry(this DataFeed parent, string title)
All very very useful indeed (thank you Ben) however the FindDataFeed(…) methods can’t be used if you are building a Mesh-enabled web app and you want to return a DataFeed from the application’s list of DataFeeds. The reason for this is that when building a Mesh enabled web app we don’t have a reference to the application’s MeshObject, we only have a reference to a MeshApplicationService.
Hence I’ve written* 2 more overloads for FindDataFeed(…) that get around that problem by extending the MeshApplicationService class rather than the MeshObject class. I’ve also written two more overloads that take type as a parameter thus allowing you to specify a type for the datafeed. They should all be put into a static class of their own rather than being added to Ben’s class because doing the latter would cause compilation errors in a none-Mesh enabled web app (because it wouldn’t know what a MeshApplicationService is). Here is that new static class:
using System.Linq;
using Microsoft.LiveFX.Client;
public static class LiveFrameworkHelper2
{
#region MeshApplicationServiceHelpers
/// <summary>
/// Finds a DataFeed in a MeshApplicationService and optionally creates it if not found
/// </summary>
/// <param name="parent">the MeshApplicationService to search</param>
/// <param name="title">the title of the DataFeed to find</param>
/// <param name="createIfNotFound">if true, then the object will be created if not found</param>
/// <returns>the DataFeed found or null if not found or created </returns>
public static DataFeed FindDataFeed(this MeshApplicationService parent, string title, bool createIfNotFound)
{
var query1 = from coreObject in parent.CreateQuery<DataFeed>()
where coreObject.Resource.Title == title
select coreObject;
var dataFeed = query1.FirstOrDefault<DataFeed>();
if (dataFeed == null && createIfNotFound)
{
dataFeed = new DataFeed(title);
parent.DataFeeds.Add(ref dataFeed);
}
return dataFeed;
}
/// <summary>
/// Finds a DataFeed in a MeshApplicationService
/// </summary>
/// <param name="parent">the Mesh Object to search</param>
/// <param name="title">the title of the DataFeed to find</param>
/// <returns>the DataFeed found or null if not found</returns>
public static DataFeed FindDataFeed(this MeshApplicationService parent, string title)
{
return FindDataFeed(parent, title, false);
}
/// <summary>
/// Finds a DataFeed in a MeshApplicationService and optionally creates it if not found
/// </summary>
/// <param name="parent">the MeshApplicationService to search</param>
/// <param name="title">the title of the DataFeed to find</param>
/// <param name="type">the type of the DataFeed to create if not found</param>
/// <param name="createIfNotFound">if true, then the object will be created if not found</param>
/// <returns>the DataFeed found or null if not found or created </returns>
public static DataFeed FindDataFeed(this MeshApplicationService parent, string title, string type, bool createIfNotFound)
{
var query1 = from coreObject in parent.CreateQuery<DataFeed>()
where coreObject.Resource.Title == title
select coreObject;
var dataFeed = query1.FirstOrDefault<DataFeed>();
if (dataFeed == null && createIfNotFound)
{
dataFeed = new DataFeed(title)
{
Resource =
{
Type = type
}
};
parent.DataFeeds.Add(ref dataFeed);
}
return dataFeed;
}
/// <summary>
/// Finds a DataFeed in a MeshApplicationService
/// </summary>
/// <param name="parent">the Mesh Object to search</param>
/// <param name="title">the title of the DataFeed to find</param>
/// <param name="type">the type of the DataFeed to create if not found</param>
/// <returns>the DataFeed found or null if not found</returns>
public static DataFeed FindDataFeed(this MeshApplicationService parent, string title, string type)
{
return FindDataFeed(parent, title, type, false);
}
#endregion
}
*When I say “written”, there isn’t much more going on here than copy-and-paste from Ben’s methods but nonetheless being aware of this may save some of you a few minutes of discover-and-fix work.
-Jamie