Skip to content

Question: Multiple business operations on the same resource? #937

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
hainguyenthanh opened this issue Feb 1, 2021 · 3 comments
Closed
Labels

Comments

@hainguyenthanh
Copy link

Hi there, in one of the projects using JADNC, I need to support various business operations on a single resource. For example, given an Article resource, besides the basic operations such as Create, Edit, Get that nicely map to the POST, PATCH, GET HTTP methods, I also need to do various operations on this resource such as Approve, Publish, Unpublish, etc.

Is there any way to do this using JADNC or JSON API in general?

Thank you,

@bart-degreed
Copy link
Contributor

Hi @hainguyenthanh,

I'm afraid there is no single obvious answer to this question.

JADNC is quite flexible in this regard. You can add non-json:api controllers with custom routes (for example: POST /actions/publish-article) to the same project (derive from BaseController instead of JsonApiController) and execute custom code. JADNC will just ignore them. Alternatively, you can add extra action methods to a json:api controller and execute custom logic for alternative combinations of route and verb (for example: PUT /api/articles/1/publish). Both approaches kinda clash with the REST principle that json:api is built on.

In case the "operations" you're referring to solely represent data changes, they can simply be modelled as attributes on an existing resource. In your example, that would mean adding:

[Attr]
public bool IsApproved { get; set; }

[Attr]
public bool IsPublished { get; set; }

to the Article class.

On the other hand, if 'approval' refers to initiating an async workflow (like sending an email, waiting for an authorized user to receive that, login and click Approve in some UI, then notify the requester of the state change, etc.) this may be best handled by another software component higher in the architecture. In microservices terminology, a saga would drive such a workflow and send multiple simple state changes to a json:api endpoint over time. The json:api endpoint could enqueue a notification message on the service bus after saving the changed resource, allowing multiple interested systems to subscribe to that.

From my point of view, the REST style (even at lower levels without hypermedia) is conceptually similar to the world wide web: a collection of documents (http resources) with relationships (http urls) that link them. Http requests then are concerned with managing the content of documents, instead of executing functions. If your operations represent synchronous commands/rpc/functions, but you want to expose them via the REST style, then the challenge becomes to model them as changes to documents and the relationships between them. See https://www.smashingmagazine.com/2016/09/understanding-rest-and-rpc-for-http-apis/ for more details on these conflicting styles.

Some attempts for this kind of modelling can be found at https://softwareengineering.stackexchange.com/questions/338666/how-does-a-rest-api-fit-for-a-command-action-based-domain and https://stackoverflow.com/questions/22322468/is-bad-practice-to-mix-rest-and-rpc-together.

Hope this helps.

@hainguyenthanh
Copy link
Author

Hi @bart-degreed, thanks for your informative reply. I'm closing the question now.

@wayne-o
Copy link
Contributor

wayne-o commented Feb 17, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants