Freigeben über


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.

Comments

  • Anonymous
    November 25, 2008
    PingBack from http://blog.a-foton.ru/index.php/2008/11/26/live-framework-%e2%80%93-storing-data-in-a-mesh-enabled-silverlight-web-application-in-4-steps/

  • Anonymous
    November 27, 2008
    Happy Thanksgiving everyone! In this issue: Martin Mihaylov, Tim Heuer, Katrien De Graeve, Expression

  • Anonymous
    November 29, 2008
    "a MeshObject is the highest element in your application that you would want to share" Moreover, a MeshObject is the only type of resource that you CAN share, there is no finer (or coarse come to that) grained unit of sharing. Great post Katrien. -Jamie

  • Anonymous
    November 30, 2008
    Lately I’ve been talking a lot about the Live Framework and showing some of its typical uses for Mesh-enabled

  • Anonymous
    December 01, 2008
    Thanks Jamie for the comment. You are right, it's up to the developer to decide at which level to use the MeshObject: the decision should be the element that needs to be shared (be it your whole application or a sub-element) needs to be a MeshObject. As you say, this is the only type of object that can be shared. I like to compare it to the Folder level in Live Mesh: you cannot share a sub-folder so the MeshObject is the folder.

  • Anonymous
    December 09, 2008
    译文: 最近在Los Angeles 的PDC大会,看到了Mesh-enabled web applications 所做的开发,我迫不及待使用手头的 token 去做些东西(应用).首先,如果你还未注册 Live Mesh 帐号的话,在这里进行注册: http://www.mesh.com/. 这是个公共的beta 版,您可以使用不同的客户端应用和Live Desktop去同步或共享数据.