Written by Irmak Tevfik on 12 - Oct - 2014

Basic Repository for using Universal Apps with Azure Mobile Services

If we are working with Mobile Services, it will be better to have a single entry point for our client applications to the service. Lets say if we are working with the default template (TodoItems). We will be having our default controller (TodoItemController) as:


Now lets go ahead and add a Folder​ named Repository  to our Shared Project​ and add a BaseRepositoty class as:

public abstract class BaseRepository<T>
{
    public async Task<T> Insert(T item)
    {
        IMobileServiceTable<T> itemTable = App.MobileService.GetTable<T>();
        await itemTable.InsertAsync(item);
        return item;
    }
 
    public async Task<IMobileServiceTable<T>> Get()
    {
        MobileServiceInvalidOperationException exception = null;
        try
        {
            var returndata = App.MobileService.GetTable<T>();
            return returndata;
        }
        catch (MobileServiceInvalidOperationException e)
        {
            exception = e;
            throw new Exception("There is a problem on getting the items");
        }
    }
 
    public async Task<MobileServiceCollection<T,T>> GetByQuery(Expression<Func<T, bool>> predicate)
    {
        try
        {
            var returndata = await App.MobileService.GetTable<T>().Where(predicate).ToCollectionAsync();
            return returndata;
        }
        catch (Exception e)
        {
            throw new Exception("There is a problem on getting the items");
        }
    }
 
    public async Task Update(T item)
    {
        await App.MobileService.GetTable<T>().UpdateAsync(item);
    }
 
}

We will be holding all of the CRUD  operatiıons on our base. If we like, we can pass our expression tree to filter the lists. So lets add a repository for TodoItems ​ as an example to follow.
Go ahead and add a new class called ​TodoRepository.cs​ as followed:

public class TodoRepository : BaseRepository<TodoItem>
{
    public async Task<TodoItem> InsertTodoItem(TodoItem item)
    {
        return await base.Insert(item);
    }
 
    public async Task<MobileServiceCollection<TodoItem, TodoItem>> RefreshTodoItems(bool isComplete)
    {
        var param = Expression.Parameter(typeof(TodoItem), "p");
        var body = Expression.Equal(Expression.PropertyOrField(param, "Complete"), Expression.Constant(isComplete));
        var lambda = Expression.Lambda<Func<TodoItem, bool>>(body, param);
        var data = base.GetByQuery(lambda);
        if (data != null)
        {
            var returndata = await data;
            return returndata;
        }
        else
            return null;
    }
 
    public async Task UpdateTodoItem(TodoItem item)
    {
        await base.Update(item);
    }
}

As you can see, basic Update or get methods can be executed from our base direcyly. On the other hand, on the ​RefreshTodoItems ​method, we will be passed the parameters and Expression tree will be formed for filtering. Another approach will be to pass parameter direcly to the server and filter the list accordingly. This approach can be used like:

Repository.TodoRepository repo = new Repository.TodoRepository();
  private async Task RefreshTodoItems()
  {
      items = await repo.RefreshTodoItems(false);
      ListItems.ItemsSource = items;
      this.ButtonSave.IsEnabled = true;
  }
 
  private async Task UpdateCheckedTodoItem(TodoItem item)
  {
      await repo.UpdateTodoItem(item);
      items = await repo.RefreshTodoItems(false);
      ListItems.Focus(Windows.UI.Xaml.FocusState.Unfocused);
  }

And our solution will look like



If you have any questions please go ahead and ask.

Enjoy Coding:)


comments powered by Disqus