Live Framework – storing data in a Mesh-enabled Silverlight web application in 4 steps

In my previous post about the Live Framework and Mesh-enabled apps I described how to create your first project, publish and deploy it to the Mesh and debug using Visual Studio 2008 support tools.

However, that was without taking advantage of any of the Live Framework and Mesh functionalities. In this post that’s exactly what I’ll be doing. The sample application will be storing some data and loading it upon running on the web on or the client (Mesh). To do this we are going to use the Silverlight Toolkit for the Live Framework, this one exists for Silverlight and for client (full) .NET applications. The Live Framework .NET library is one of the toolkits you can use to call the framework, but this is actually a wrapper around the RESTful APIs that can be accessed through HTTP. Because at the base it is all HTTP, any platform can access the framework.

The toolkits that are available today are just extra help so that you do not have to deal with all the HTTP calls. Toolkits that are available today:

  • .NET Toolkit for the Live Framework
  • Silverlight Toolkit for the Live Framework
  • Live Framework JavaScript Kit

Now let’s get to our sample application. To get my data stored and synchronized through the Mesh it is important to get a good look at the Live Framework Resource Model. The base objects in the resource model are MeshObejcts, DataFeeds and DataEntry objects. I must admit I still need to read on further into the SDK documentation and understand correctly when I should use which elements. As I have understood it a MeshObject is the highest element in your application that you would want to share. If my application (instance) can be shared as a whole I will only create one MeshObject. If sub-elements can be shared than each of the sub-elements should be a MeshObject.

For now, I’ll be accessing the DataFeeds property. Nothing more than that for now. So in short, the application will contain a list of Party titles. Yes parties, guess I’m thinking about Christmas wish lists or something :)
Please note I have not gone to defining the object model for the application, it shows a simple way how to access the Mesh and feeds directly.

image

1. First of all since we are using the Silverlight Mesh-enabled web application template in Visual Studio, this contains some base code to load the Mesh application service object.

 MeshApplicationService meshApp = Application.Current.GetMeshApplicationService();
 // once mesh contents are loaded we get callback
 meshApp.LoadCompleted += new EventHandler(meshAppLoaded);
meshApp.Load();

2. The app UI consists of a ListBox, a TextBox and a Button. ListBox is bound to an ObservableCollection of DataEntryResource objects. Each of the Party entries will be stored in a DataEntryResource on the Mesh.

 ObservableCollection<DataEntryResource> partyEntries = 
              new ObservableCollection<DataEntryResource>();

3. Upon loading of the Mesh application service object we load the DataFeed in which we store the entries. This DataFeed does not exist upon first call of the application so we create in that case. I’m also inserting a default entry in the list named “My first party”.

 private void LoadDataFeed(MeshApplicationService meshApp)
{
            
        DataFeed partyFeed = meshApp.DataFeeds.Entries.FirstOrDefault
                    (pf => pf.Resource.Type == "Party_DataFeed");

       if (partyFeed == null)
       { 
           //Create the DataFeed
           partyFeed = new DataFeed();
           partyFeed.Resource.Type = "Party_DataFeed";
           meshApp.DataFeeds.Add(ref partyFeed);

           DataEntry entry = new DataEntry("My first party");
           entry.Resource.Type = "Party_DataEntry";
           entry.Resource.Title = "My first party";

           partyFeed.DataEntries.Add(ref entry);
           meshApp.Update();
       }

       //load the collection of DataEntry items
       if (!partyFeed.DataEntries.IsLoaded)
           partyFeed.DataEntries.Load();

     foreach (DataEntry entry1 in partyFeed.DataEntries.Entries)
     {
           partyEntries.Add(entry1.Resource);
     }
}

4. To finish, we create the Button event handler to add an item to the collection of DataEntries.

 private void buttonAdd_Click(object sender, RoutedEventArgs e)
{
    MeshApplicationService meshApp = Application.Current.GetMeshApplicationService();

    DataFeed partyFeed = meshApp.DataFeeds.Entries.FirstOrDefault(
                           pf => pf.Resource.Type == "Party_DataFeed");

    DataEntry partyEntry = new DataEntry();
    partyEntry.Resource.Type = "Party_DataEntry";
    partyEntry.Resource.Title = textPartyName.Text;

    partyFeed.DataEntries.Add(ref partyEntry);
    partyEntries.Add(partyEntry.Resource);

     textPartyName.Text = "";
}

And that’s it. Data in the Mesh, be it on the client desktop or the web.