0

I have a question regarding how to structure my controllers. Let's assume I have a have a system that has stores and products and stores have products.

so the way to get stores is /api/stores/{id} and the way to get a product is /api/product/{id} or /api/stores/{id}/product/{id}.

How would you structure the Controllers? should I have a controller for products and controller for stores and every route that starts with stores in it should reside in StoresController otherwise in ProductsController?

1 Answer 1

2

This is from Microsoft: Api design guidance

In more complex systems, it can be tempting to provide URIs that enable a client to navigate through several levels of relationships, such as /customers/1/orders/99/products. However, this level of complexity can be difficult to maintain and is inflexible if the relationships between resources change in the future. Instead, try to keep URIs relatively simple. Once an application has a reference to a resource, it should be possible to use this reference to find items related to that resource. The preceding query can be replaced with the URI /customers/1/orders to find all the orders for customer 1, and then /orders/99/products to find the products in this order.

Sign up to request clarification or add additional context in comments.

2 Comments

This is very helpful thank you. but this doesn't answer my question fully. In their example where should the URI /customers/1/orders be? under CustomersController or OrdersController? same for /orders/99/products OrdersController or ProductsController?
An old question but for /customers/1/orders it's my opinion it should be under CustomersController. Here, you're not acting upon orders in general; you're acting upon orders as they relate to a specific customer.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.