-1

Let's say you need to create an API around machines, and you want to be able to query machines many different ways:

  • Get List of all Machines
  • Get Machine By ID
  • Get Machines By Client
  • Get Machines By Type
  • Get Machines By Mfg Date
  • Get Machines By Foo
  • Get Machines By Bar

You get the idea. And keep in mind this is a very simplistic example. Don't take it too literally. It's the general theory I'm after. The way I see it, there are two ways you can go about this

  1. You can have a controller for each (with the exception of the first two options)
  2. You can put them all in the same controller

Under Option 1, each endpoint name would be the same as the HTTP method (GET in this example). Under Option 2, each endpoint name would match the query type. (Example: ByClientId)

I've always been of the opinion that #1 is better. But is there ever a case for #2? For example, if you are going to end up with hundreds of controllers each with a single endpoint inside of it?

Thanks!!


The following are only examples of what I mean by 1 and 2...

Option 1 would look like this:

MachineController.cs

[HttpGet]
public string Get()
{
    return "all machines";
}

[HttpGet]
public string Get(int id)
{
    return id.ToString();
}

and then...

MachinesByClientController.cs

[HttpGet]
public string Get(int clientId)
{
    return $"all machines for client of id " {id};
}

and then...

MachinesByTypeController.cs

[HttpGet]
public string Get(int typeId)
{
    return $"all machines with type of id " {id};
}

And so on down the line.

--- OR ---

Option Two would look like this:

MachineController.cs

[HttpGet]
public string All()
{
    return "all machines";
}

[HttpGet]
public string ByMachineId(int id)
{
    return id.ToString();
}

[HttpGet]
public string ByClientId(int id)
{
    return id.ToString();
}

[HttpGet]
public string ByTypeId(int id)
{
    return id.ToString();
}
| improve this question | | | | |
  • Option #1 sounds like a nightmare. Why so many controllers? Option #2 is how I've always seen it done. – Eric King Mar 19 at 23:05
  • Pwesonally I think that if the endpoints all access the same resource (database table, IoT network, backend API, etc) then it is the same data just filtered different ways so it should be the same controller with multiple endpoints. It's when you start talking to multiple different things that I use multiple controllers. So if all the machines are devices on the same IoT network then I would use option 2. – Jerry Jeremiah Mar 19 at 23:26
1

Honestly, you're seriously over-thinking this thing!!

The actual requirement is: "get [something] by ([some_field] or All)"

Traditionally, this is satisfied by an API which says "get by [field_name]==[value]" where, if the [field_name] and [value] are omitted, it is understood to mean "all."

If the [field_name] provided is invalid, then that's simply an error.

| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.