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

前端 未结 1 1487
暖寄归人
暖寄归人 2021-01-18 02:49

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.

相关标签:
1条回答
  • 2021-01-18 03:33

    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...

    0 讨论(0)
提交回复
热议问题