3
I’m starting to work with Restful Services and I’m having doubts about many relationships for many. For example, suppose I have two entities Cliente
and Fornecedor
and that Cliente
has a list of suppliers, the suppliers from whom he buys and Fornecedor
own 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 to expose the following URL /clientes/{idCliente}/fornecedores
to get suppliers from a customer and then add or remove suppliers from that customer, but I’m finding this a bad option.
Basically because the url would already exist /fornecedores
that manages suppliers in general and putting this to the suppliers of each customer seems very strange to me.
That way the most indicated way to manage many relations for many in restful api’s?
Thank you for the @dcastro reply. And when implementing this in the ASP.NET Web API is it recommended to put the methods to deal with the objects related to a certain object in the controller of that object or make a new controller? In this case, this would put the methods to deal with suppliers in the
ClientesController
instead of creating a different controller.– SomeDeveloper
@user1620696 Hm have seen it both ways... Personally, if the methods are simple, I would create a method
GetFornecedores
in the controllerClientesController
to the Unionclientes/id/fornecedores
. If logic is complex, it might be worth separating the "nested Resource" methods for your own controller (e.g.,FornecedoresDeClientesController
).– dcastro