1

I have a series of nested objects, exposed over a REST API, like so:

class Categories
{
      int id;
      string name;
      List<Subcategories> subcategories;
}

class Subcategories
{
      int id;
      string name;
      List<Products> products;
}

class Products
{
     int id;
     string name;
     List<Models> models;
}

These objects can be accessed over a REST API with the following end points

// get a list of categories
GET /categories 

// get a category                                   
GET /categories/:id

// Get subcategories for a product
GET /categories/:categoryId/subcategories

// Get a subcategory
GET //categories/:categoryId/:subcategoryId


//Get products under a subcategory
GET /categories/:categoryId/:subcategoryId/products

//Get a product
/categories/:categoryId/:subcategoryId/:productId

//Get models
/categories/:categoryId/:subcategoryId/:productId/models


Get a model under a product
/categories/:categoryId/:subcategoryId/:productId/:modelId

What design pattern do I use to design a data access layer on the client side? Please note that my client only reads these objects, it doesn't write to them. DAO? Repository? I am a bit confused between the two, not sure what would be applicable here.

5
  • Do you need them locally or do you just need to give the user an interface to browse the structure? Feb 19, 2015 at 6:50
  • I am consuming the REST API from a xamarin client. So yea, it is used to drive a user interface that drills down each level. The data is mostly static, because the catalog doesnt change very often. So I would like to cache some parts of it, if possible. Feb 19, 2015 at 7:59
  • and the interface is? For example in simple html/php you simply create one function which loads the top level: /categories. Then loop it with foreach and generate the links. When clicked you load the same script but now the starting point is not /categories but /categories/123 and you show the list again, generate the links and done. Feb 19, 2015 at 8:27
  • I am using C# which requires me to define a class for each entity (category, subcategory, etc). My problem now is that my class representation gets muddled with the code for accessing the data. Also, for all the different entity types I am basically doing the same kind of calls, i.e. a GET request. Also, the methods for deserialising the objects are very similar, so I end up with a lot of similar code. I am looking for a way to clean up this implementation. Feb 19, 2015 at 8:56
  • If that's the case an abstract class (or something alike) could solve most. Something like: class Listing { function getData(){} function parseData() {} } etc and then implement the specific classes: CategoryListing implements Listing { var url = '/categories'} SubCategoryListing { var url = /categories/123/ } etc Feb 19, 2015 at 9:07

1 Answer 1

1

Why do you want them nested in your access layer? That API exposes plain objects, not trees of them as I can see.

So, you may build your internal API mimicking that one.

class Category {
    public static Category[] getList();
    public static Category getById($id);
    public static Subcategory[] getSubcategories($id);
    public void fetchDetails();
}

class Subcategory{
    public static Subcategory getById($id);
    public static Product[] getProducts($id);
    public void fetchDetails();
}

I've declared some methods static, but just for clarity sake. In most cases static method on classes may prove testing hard.

In this example created objects are empty by default. fetchDetails() method fills them using REST API. This is to skip one API request when you want to fetch only children of a specific parent without fetching the whole parent first.

Well, as I said, it's only an example. Without the specific requirements of API consumers it's hard to suggest something really useful ;) And requirements tend to change ;)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.