Many relationships for many in RESTful service

3

I'm starting to work with RESTful services and I'm having doubts about many-to-many relationships. For example, suppose I have two Cliente and Fornecedor entities and that Cliente has a list of providers, the providers from which it buys, and Fornecedor has a list of customers who buy from it. Something like this

public class Cliente
{
    // Propriedades da classe Cliente

   public IList<Fornecedor> Fornecedores { get; set; }
}

public class Fornecedor
{
    // Propriedades da classe Fornecedor

   public IList<Cliente> Clientes { get; set; }
}

So if you want to expose this in a RESTful service what would be the best way to do it? I thought about exposing the following URL /clientes/{idCliente}/fornecedores to get a client's vendors and then add or remove vendors from that client, but I'm finding this a bad option.

Basically because there would already be a /fornecedores url that manages suppliers in general and putting this pros out of each client seems very strange to me.

So what is the best way to manage many-to-many relationships in api's restful?

    
asked by anonymous 24.07.2014 / 22:08

2 answers

5

I do not see any problem with the suggested proposal.

The fact that there are two /clientes/id/fornecedores and /fornecedores URIs does not seem strange to me. In fact, it is a common way of managing this type of relationship, for example, /posts + /authors/id/posts , /questions + users/id/questions ...

In REST it is important that the URIs be self-descriptive. Therefore, the most natural way of describing "getting the 123 client providers" would be GET /clientes/123/fornecedores/ . Similarly, the most natural way of describing "getting the supplier's clients abc would be GET /fornecedores/abc/clientes/     

25.07.2014 / 12:44
1

I would simplify things in api: / Customers / {id} / Suppliers {id}? Client = {idclient}

Take care of many-to-many relationships. If you still do not know, you may be able to take advantage of some DDD practices, where there is a clear separation of whose life cycle are independent. In this case, you could use Aggregates to separate these parts.

    
30.07.2014 / 20:18