1

ASP.NET Web API, is it good practice to have a controller that is used for ALL business objects?

For example, I have the following route in my controller:

[Route("{resource}/{id}")]
[HttpGet]
public HttpResponseMessage Get(string resource, int id)
{
    // code not shown for brevity...
}

Based on this, I have about 50 business objects, and each one has a property named "Resource", which is basically the name of the class as a string (e.g. "Customer", "Order"). In my controller action above, I retrieve a resource using the route parameters "resource" and "id". For example:

GET /Customer/1234 and GET /Order/5678

Is this bad practice? Is it better to create a controller for each business object? What are the pros and cons of each approach?

ekad
  • 14,436
  • 26
  • 44
  • 46

1 Answers1

0

I like Apple's way of doing it.

Every view is controlled by only one view controller. ~ View Controller Programming Guide for iOS The idea is that you should be able to easily swap out Views. IMO, by only having 1 Controller per View it makes it easier to accomplish this.

Although a general rule of thumb, the number of required controllers depend on a number of modules and sub-modules in the Web app.

As a complementary, it would be helpful to organize the controllers into Areas. The concept of Areas is build into the ASP.NET MVC framework and it simplifies the organization of controllers that serve one module.

There are a number of related discussions:

MVC Architecture

MVCController And View directories organization

What does MVC class organization look like for multiple views and controllers?

ASP.NET MVC controller actions design

Community
  • 1
  • 1
Asad
  • 498
  • 7
  • 21