Single Web API endpoint for all entities, good or bad

apiapi-designasp.netweb

Situation

We are currently developing a large web application (Web API 2) – several entities and thus require several endpoints for each. But suddenly, they changed to "one endpoint fits all" approach, something like:

/api/data?entity=entity_name

Then the usual controller for GET, POST, PUT, DELETE. We have something like this:

public class DataController : ApiController
{
    public HttpResponseMessage Get(string entity)
    {
        Type entityType = Type.GetType(entity);
        var query = HelperClass.GenerateGenericGetMethod<entityType>("GetAll").ToList();
        return Response(OK, query);
    }
}

As you can see, we created some Helper to generate a generic method for Get, GetById and same old same old queries, we will then apply another set of helpers for: stripping down properties to return to the client and other custom actions.

Question

Is this going to cause some overhead to the server? Will it be okay if we scale it all up in the future? (And I'm sure we will) Like add token authentication, user roles, mobile access and more. Would like some insights.

Thanks!

EDIT: I am fairly new to web development and would love to hear some suggestions on what approach would fit our situation best.

Best Answer

I sincerely doubt that Web API 2 would have any problem with a single endpoint. There may be considerations of which I am not aware, but it doesn't seem likely to be the limiting factor.

The real problem with this approach is what it does to the comprehensibility and maintainability of the code, as well as making it easier to trace and debug.

For the first problem is that /api/data?entity=widget is less expressive than, say, /api/widget.

The latter form as less noise and communicates the intent more clearly.

Another factor is the growth of your APIs. If you start with a single retrieval for each, the single entry point may not seem bad. But let's say you start needing to do a couple of queries: widget by ID and widget by name. Your URL string starts to look like /api/data?entity=widget&id=123 or /api/data?entity=widget&name=Frobnitz. Little by little, your Get method starts to accumulate more and more checks to figure out which query you mean, especially if we add a Foo entity that also can be looked up by ID.

In general, you would probably be well-served to read up a little on REST web services and applying some of the general principles (some of the heavier stuff is probably overkill for your situation).

Related Topic