0

I have multiple Models(Models in MVC), These models are injected into repositories and repositories are injected into Controllers. I need to create an api for several endpoints. The responses for these endpoints will use repositories services. Response will be similar to this;

{
"user": {
    "userId": 1,
    "id": 1,
    "name": "John",
    "surname": "Doe"
},
"posts": [{
        "id": "1",
        "title": "Lorem",
        "body": "test-2"
    },
],
"products": [{
        "id": "1",
        "name": "productA"
    },
    {
        "id": "2",
        "name": "productB"
    }
],
"services":[{
        "id": "1",
        "name": "a"
    },
    {
        "id": "2",
        "name": "b"
    }
],
"settings":[{
        "id": "1",
        "name": "settingA",
        "value" "valueA"
    },
    {
        "id": "2",
        "name": "settingsB",
        "value" "valueB"
    }
],
}

Some of these keys(models + service responses) don't have a direct relation with each other. When I get models from DB I want to remove unnecessary columns(I can't hide while querying because I need them while generating a response) I want to modify some column values, I want to send some column values to some services and get response from these services and use this response for api response.

The question is that; Where should I do all these (calling all these services, repositories, making mappings, some calculations, and generating api response.)

If I do that in controller I will have a fat controller If I do that in repository, then it will not be a repository anymore

Should I create another service inject all required services there, generate response, and inject this new service into controller ?

1

When you have an MVC API that produces HTML documents (a web-app with server-generated pages), it is commonly accepted that the code for creating the HTML documents belongs to the View component of MVC.

The simple fact that an API generates documents in JSON (or XML) format does not really change anything. From the perspective of the server, you are still generating a View.

If you would argue that JSON is not a View because it can't be viewed by the user, then my reply is that the same holds for HTML. Both need to be interpreted by a separate application on the client's machine to get something that is usable for the use.

  • I agree on that they are both views (in the perspective of client side) - so where should all these mappings go ? In controller ? – Bold P. Jan 8 '18 at 22:49
  • @BoldP.: For web-MVC, it is common that the View is a class (or component) that gets invoked by the Controller with data retrieved from the Model. – Bart van Ingen Schenau Jan 9 '18 at 7:09

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.