Jeff Sanders Technical Blog

I am a Microsoft employee that has worked on all aspects of the Web Stack for a long time. I hope these blogs are useful to you! Use this information at your own risk.


<< Go Back

Move Your Client Logic To The Server Windows Azure Mobile Services

- 12 Jun 2013

The Client Libraries for Windows Azure Mobile Services are powerful and allow you to embed logic within your client application.  The quickstart and sample applications give you a great overview on how to implement logic on the client to get the view of data you would like in the client application.  A different architecture for you would be to move this view logic to the server using the Server Scripting capability of Windows Azure Mobile Services so that you do not have to duplicate the client side logic for each platform that you target as a client! 

NOTE: There is nothing wrong with having your query logic client side but sometimes it makes sense to have it all on the server.  For example if it is a complicated query and every platform needs to implement it, consider moving it server side.

For this example I will to take the C# Windows Store app Todo sample project that is generated for you from the Azure Management Dashboard and move the client side logic into the server.  In this case we never want any view besides the one that excludes the complete items so I will modify the client code to remove the filtering logic and add it to the server read script.  Then I will show you how to modify the C# Windows Phone sample to take advantage of these views.

Follow along with me to get started!

Create a new Mobile Service and follow the steps to create your TodoItem table and download and run a new Windows Store app (C#).

Ensure you can add items and mark several of them complete.

Create a new Windows Phone app and ensure you can see the data you see in the store app and it is the same (completed items should be hidden).

 

Modify your code in the Windows Store app to remove the client logic

Open Mainpage.xaml.cs and remove the filtering logic when you read the table in RefreshTodoItems:

  private async void RefreshTodoItems()
        {
            MobileServiceInvalidOperationException exception = null;
            try
            {
                // This code refreshes the entries in the list view by querying the TodoItems table.
                // The query excludes completed TodoItems
                items = await todoTable.ToCollectionAsync();
            }
         

Run your app to verify that even the items in your list appear briefly but then go away!  Put a breakpoint on CheckBoxComplete_checked to see why they appear briefly.   Leave this breakpoint in place to verify after you modify the server read script, you are not retrieving completed items (you will not hit this breakpoint after the server side change).

Now, go to your DATA tab in your mobile service, click on your TodoItem table and choose the SCRIPT tab.  Choose the Read script from the dropdown and we will move the login to only return completed items (since that is the only thing we need to read in our app anyhow).

function read(query, user, request) {
    query.where({ complete: false });
    request.execute();
}

Test you Windows Store app with the breakpoint still set on CheckBoxComplete_checked to verify you are not longer retrieving complete items.

Now go to your Phone application and remove the filter from RefreshTodoItems() like you did in your Windows Store app and test this code!

        private async void RefreshTodoItems()
        {
            // This code refreshes the entries in the list view be querying the TodoItems table.
            // The query excludes completed TodoItems
            try
            {
                items = await todoTable.ToCollectionAsync();
            }

 

You have now successfully moved your client logic server side!

Obviously this is a super simple example of moving Client Logic server side.  You would want to do this whenever you have a complex query as well so that you do not burden each of the client platforms (I only showed you two) with implementing the same logic.  In my next post I will show you how to implement the same Server side logic to access complete items only in the read form TodoItem but in a dummy table so that you do not modify the TodoItem table.

 

Let me know if this was useful to you!

 

-Jeff

<< Go Back