ucloudy

This is the time of a project completely dedicated to Cloud but always with the goal of providing additional functionality and capabilities to an embedded system.

uCloudy is a library that aims to provide a variety of clients for. NET Micro Framework to connect to the main cloud services. The first and for now only service supported in this first release is Windows Azure Mobile Services.

As we all know, Microsoft provides a set of SDK to take advantage of this service in Windows applications store, Windows Phone, iOS, Android and the Web I have tried to bring this functionality in embedded systems with. NET Micro Framework. All this has been made ​​possible thanks to the RESTful interface that the mobile services provide for interfacing to them. Thanks to the HTTP client that I recently integrated into uPLibrary,  I made a series of classes through which you can perform the four basic operations on the tables of mobile services: insert, update, delete, and query.

The client is implemented through the class MobileServiceClient which you must provide the URI of the application (eg <myapplication>. Azure-mobile.net) and one of the keys (master key or key application) or an authentication token, in order to determine the permissions with which the client can perform the same operations.

The only method provided by the client is GetTable() which you must provide the name of the table on which you want to perform a task; it returns an object that implements the IMobileServiceTable interface and you can invoke the following methods on it :

  • Insert : to insert a table raw with an object that implements IMobileServiceEntity interface;
  • Update : to update a table raw with an object that implements IMobileServiceEntity interface;
  • Delete : to delete a table raw by ID;
  • Query : to execute a query;

Each object that is mapped to a row in a table must implement the IMobileServiceEntity  , whose main method is ToJSON() which should return a JSON representation of the object including its ID. I have deliberately not made ​​a base class (abstract) with an implementation of this method that was based on reflection, to avoid unnecessary delays at runtime. In these cases, it is more performant implement the method in its class knowing the fields directly.

Imagine that our object is to be mapped as follows:

   1: public class MockEntity : IMobileServiceEntity
   2: {
   3:     public int Id { get; set; }
   4:  
   5:     public string FieldA { get; set; }
   6:     public int FieldB { get; set; }
   7:  
   8:     public string ToJson()
   9:     {
  10:         return "{ \"FieldA\" : \"" + FieldA + "\", \"FieldB\" : " + FieldB + "}";
  11:     }
  12:  
  13:     public void Parse(string json)
  14:     {
  15:         throw new NotImplementedException();
  16:     }
  17: }

As you can see, the method ToJSON() is simple and easy to perform, since it does not use the reflection, because the fields are absolutely known at development time.

   1: public static void Main()
   2: {
   3:     Uri appUri = new Uri("http://myapp.azure-mobile.net");
   4:  
   5:     MobileServiceClient client = new MobileServiceClient(appUri, "applicationkey");
   6:     
   7:     // insert records
   8:     client.GetTable("mock").Insert(new MockEntity() { FieldA = "Temp1", FieldB = 100 });
   9:     client.GetTable("mock").Insert(new MockEntity() { FieldA = "Temp2", FieldB = 200 });
  10:     client.GetTable("mock").Insert(new MockEntity() { FieldA = "Temp3", FieldB = 300 });
  11:     client.GetTable("mock").Insert(new MockEntity() { FieldA = "Temp4", FieldB = 400 });
  12:  
  13:     // delete id = 1
  14:     client.GetTable("mock").Delete(1);   
  15:      
  16:     // update record id = 2
  17:     MockEntity mock = new MockEntity() { Id = 2, FieldA = "Temp2_update", FieldB = 201 };
  18:     client.GetTable("mock").Update(mock);
  19:  
  20:     // query
  21:     client.GetTable("mock").Query("$orderby=FieldB");
  22: }

From the example, it is clear that the client reflects in part the corresponding version present in the SDK provided by Microsoft.

 

Obviously, the history of this library has just started and the goal is to integrate it over time with additional clients to access other Cloud services!