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:
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?
Usually a Web API controller has maximum of five actions - Get(), Get(id), Post(), Put(), and Delete().
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.
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.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With