Adam
Adam

Reputation: 1140

REST - Good design practice

I am completely new in Windows Phone development and I am trying to write an app that retrieves the data from a server and displays them to the user. I have several resources on the server, lets say User, Quest and Activity. I use RestSharp lib to retrieve the data from the server.

Example of Get User:

public void Get(String id, LifeHunt.MainPage.UserReady userReady)
{
   var client = new RestClient(Deployd.REST_URL);
   var request = new RestRequest(resource + "/{id}", Method.GET);

   request.AddUrlSegment("id", id);

   client.ExecuteAsync<User>(request, response =>
   {
      if (response.StatusCode == System.Net.HttpStatusCode.OK)
      {
         userReady(callback.Data);
      }

   });
}

Once the user is retrieved, I call the userReady method I passed as callback and get the user to MainPage to display it.

Now, I have to repeat the whole process for all CRUD (Insert, Get, GetAll, Update, Delete) functions for all Users, Quest and Activity. That means I will need 15 different callback methods, which I think is not a good software design.

The other way would be just one callback method and check the type of parameter passed in the callback method. However I do not think this is a nice solution as well.

I was thinking about something like creating a generic interface for CRUD, implement it by all the User, Quest and Activity classes:

interface ICRUD<T>
{
   void GetAll(GenericCallback callback);
   void Get(GenericCallback callback);
   void Add(T item, GenericCallback callback);
   void Remove(String id, GenericCallback callback);
   void Update(T item, GenericCallback callback);
}

However I do not really know how to implement it and even if it is a good way. Could somebody suggest me a good design solution?

Upvotes: 1

Views: 127

Answers (1)

Igor Kulman
Igor Kulman

Reputation: 16361

Use MVVM, create a ViewModel that would hold all the data you need and bind it to the View. Then create Service class with methods directly returning the data you need (no callbacks). Create a instance of this Service in the ViewModel and call the methods to get and fill the data when needed.

Upvotes: 1

Related Questions