Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single Web API controller per resource or less controllers with more custom actions?

I want to expose most of my business layer methods to a Web API project to allow for broader consumption.

One idea is to have one Web API controller per resource.

The other idea is to have one controller per logical business part and use attribute routing to expose the relative methods.

I like the second approach, that results to less controllers.

What are the disadvantages of NOT having one controller per resource?

Additional Info:

This API will live in an intranet and will serve support applications that need data from our 2-3 master applications (ERP,Payroll,Barcode e.t.c)

A resource for this APi would be business entities that are defined in our business layer assembly. They will be simple objects and complex ones. Examples:

  • Inventory Items
  • Customers
  • Items per Customer
  • Current loading pickslips
  • Present production data

e.t.c

Example:

I want to expose methods like GetCustomerListByArea or GetItemsListPerCategory.

What then? Should i create another controller or use custom controller actions and attribute routing and put it in the existing controllers?

A link with some more info:

REST vs. RPC in ASP.NET Web API? Who cares; it does both.

Very nice , rather old, article explaining my question. But it does not go deeper in actual saying how to orginize controllers. In areas? Or just folders?

like image 576
e4rthdog Avatar asked Aug 05 '14 21:08

e4rthdog


People also ask

How many get methods we can implement in single controller in Web API?

Usually a Web API controller has maximum of five actions - Get(), Get(id), Post(), Put(), and Delete().

Which four types of methods does an API controller typically specify?

Methods. The four main HTTP methods (GET, PUT, POST, and DELETE) can be mapped to CRUD operations as follows: GET retrieves the representation of the resource at a specified URI.

What are controllers in Web API?

Web API Controller is similar to ASP.NET MVC controller. It handles incoming HTTP requests and send response back to the caller. Web API controller is a class which can be created under the Controllers folder or any other folder under your project's root folder.


1 Answers

This boils down to a simple design principle - separation of concerns (SoC), you should have a think about what functionality from your business layer you want to expose and create controllers based on that.

From your example above, you might create a ProductController and CustomerController, which could expose the following endpoints:

GET /api/customer/1234        # gets customer by id
POST /api/customer/create     # creates a new customer
GET /api/customer/1234/items  # gets all items for a customer
GET /api/product/9876         # gets a product by id
POST /api/product/create      # creates a new product

Hope that helps...

like image 171
Tom Hall Avatar answered Oct 18 '22 03:10

Tom Hall